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

點選檔案下載卻直接執行開啟

點選檔案下載卻直接執行開啟

遇到一個情況。 使用者開啟瀏覽器點選某個連結檔案時,不會執行下載而是直接開啟檔案。 原本如果只是圖片...

如何將mysql table auto_increment重設

如何將mysql table...

photo555 資料表裡的資料加加減減,一不小心突然驚覺當初的欄位長度設定的不夠⊙⊙ 當然,你可以...

ubuntu 18.04  apache 目錄啟用並開啟.htaccess 覆寫

ubuntu 18.04 ...

.htaccess是在Apache HTTP Server這款服務器架設軟件下的一個對於系統目錄進行...

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