Panduan ini akan membahas cara menambahkan security header pada PHP. Ini dapat digunakan untuk alternatif dari postingan sebelumnya yaitu menambah security header di cloudflare
Anda dapat menambahkan security header ke situs web menggunakan PHP dengan fungsi header()
.
<?php
// Add security headers
header('strict-transport-security: max-age=2592000; preload');
header('referrer-policy: same-origin');
header('x-content-type-options: nosniff');
header('x-frame-options: SAMEORIGIN');
header('x-xss-protection: 1; mode=block');
header('content-security-policy: frame-ancestors https://example.com/');
// Add CORS headers (optional)
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: origin, x-requested-with, content-type');
header('Access-Control-Allow-Methods: GET, POST');
// example website content
phpinfo();
?>
Pastikan fungsi header berada dipaling atas atau sebelum website content
Jika ingin menghapus header tertentu dapat menggunakan fungsi header_remove()
header_remove('X-Powered-By');