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

使用手機將網址分享到 Line時,如何強制使用預設(外部)瀏覽器開啟?

使用手機將網址分享到 Lin...

如標題,當厭惡了每次在line上面分享demo網址給客戶時點選網址就會使用line的瀏覽器開啟。 還...

Android Fragment及Activity 中使用sharedpreferences

Android Fragme...

在Android下想要儲存執行資料的方式有好幾種,例如存在手機空間或者是資料庫空間。 這裡紀錄一下使...

改變input placeholder 字體顏色

改變input placeh...

colorphoto55 通常我們會使用 placeholder 來作為欄位提醒文字~ 通常 pla...

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