Skip to main content
  1. Posts/

Menambahkan Security Headers pada PHP

1 min
php php

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');

Related

Error PHP - move_uploaded_file() Unable to move file from tmp to dir
1 min
php php
Kirim Email dengan PHPMailer
3 mins
php php
Mengamankan Server dari Eksploitasi PHP Shell
1 min
php linux php
Setup Apache dan PHP-FPM di Docker
7 mins
apache docker apache php
Phalcon Error: Dispatcher has detected a cyclic routing causing stability problems
1 min
phalcon phalcon php
Install Redis di Linux
1 min
linux php linux