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

PHP Caching Headers

PHP Caching He...

pexelsphotospeed 最近測試平台系統執行速度,老實說測試的有點無力~ 總覺得能做的可以...

在PHP中通過POST發送JSON

在PHP中通過POST發送J...

這回,我遇到需要通過POST請求發送JSON到API。 剛好利用這次的機會紀錄一下: //API U...

PDO bindParam 和 bindValue 差別在哪裡?

PDO bindParam ...

首先 bindParam 是綁定變數、bindValue  是綁定值。 以下範例: bindPara...

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