Loading...

PHP如何設定HTTP安全標頭?

Temperature: 0 °C

Mark ChangMark Chang
author_tools

PHP該如何將以下安全標頭添加到網站?

X-Frame-Options  防止點擊劫持攻擊
X-XSS-Protection  減輕跨站腳本(XSS)攻擊
X-Content-Type-Options  防止可能的網絡釣魚或XSS攻擊

以下透過2種方式添加:
(1)在.htaccess文件中加入

<IfModule mod_headers.c>
Header set X-Frame-Options "DENY"
Header set X-XSS-Protection "1; mode=block"
Header set X-Content-Type-Options "nosniff"
</IfModule>

這方式是一勞永逸。
(2)直接添加在PHP檔案裡:
header('X-Frame-Options: DENY');
header('X-XSS-Protection: 1; mode=block');
header('X-Content-Type-Options: nosniff');

麻煩的是在每支PHP的表頭都得添加,所以還是使用.htaccess設定或是直接設定在Apache裡比較省工。

另外,以上方式選擇一種設定即可,重複設定似乎反而會失效。

PHP如何設定HTTP安全標頭?
若不知道自己的安全標頭不知道是否有設定正確,可以使用Mozilla Observatory做測試:Mozilla Observatory

以上紀錄~
 

#故事  #PHP  #XContentTypeOptions  #XFrameOptions  #XXSSProtection  #htaccess  
https://innstory.com/story-PHP如何設定HTTP安全標頭-2701
PHP

Prev
 設定Postfix使用SMTP_Server當作relay主機

Next
PHP如何移除_xpoweredby_資訊 

About the Author

Mark Chang

離不開電腦的宅男

Visitor message

Leave some footprints to prove that you visited me

Recommended reading

Author's other related stories

Android 禁止螢幕旋轉

Android 禁止螢幕旋轉...

首先找到AndroidManifest.xml檔案 位置在app/manifests/路徑底下 在 ...

使用jQuery阻止input直接按Enter就送出表單

使用jQuery阻止inpu...

使用表單是傳送資料裡經常會用到的方式。 這時當使用者輸入表單後習慣上按下Enter,則會將表單內容送...

設置屬性 PHP PDO setAttribute

設置屬性 PHP PDO s...

PDO::ATTRCASE:強制列名為指定的大小寫。 PDO::CASELOWER:強制列名小寫。 ...

Please select an option

error

Hi, thank you for your participation, but you cannot vote repeatedly~

Join innstory now and start recording your story.

"Innstory" is a place to store stories. We are committed to becoming a warm platform. Deepening the bonds between people is our direction.
We are convinced that the blockchain between people is not just a cold calculation. Join us now.

Wrong format