The truth of a theory is in your mind, not in your eyes.

PHP Header Lengkap dengan Contoh

Berikut adalah implementasi header menggunakan PHP. Untuk mengetahui detail HTTP Header status code, bisa mendalaminya List of HTTP Header Status Code .

 

 

 

1. Just OK :)

header('HTTP/1.1 200 OK'); 


2. Page was not found:

header('HTTP/1.1 404 Not Found');


3. Access forbidden: Header ini muncul ketika kita tidak memiliki otorisasi untuk membuka halaman web tertentu karena terproteksi password .htaccess

header('HTTP/1.1 403 Forbidden');


4. The page moved permanently. Header ini sangat berguna untuk memberi tahukan kepada search engine bahwa page kita yang lama sudah tidak dipakai. Jadi search engine bisa lebih fokus ke page yang baru hasil redirect.

header('HTTP/1.1 301 Moved Permanently');
header("Location: http://logcode.net");


5. Server error

header('HTTP/1.1 500 Internal Server Error');


6. Redirect halaman web:

header('Location: http://www.logcode.net/');


7. refresh otomatis:

header('Refresh: 10; url=http://www.logcode.net/');
print 'You will be redirected in 10 seconds';


8. Fefresh menggunakan meta syntax

<meta http-equiv="refresh" content="10;http://www.logcode.net/ />


9. override nilai X-Powered-By

header('X-Powered-By: PHP/4.4.0');
header('X-Powered-By: Brain/0.6b');


10. content language (en = English)

header('Content-language: en');


11. last modified (baik untuk caching)

$time = time() - 60; // or filemtime($fn), etc
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');


12. Memberitahukan kepada search engine bahwa content tersebut tidak ada perubahan.

header('HTTP/1.1 304 Not Modified');


13. set content length (baik untuk caching):

header('Content-Length: 1234');


14. Header untuk download:

header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="contoh.zip"');
header('Content-Transfer-Encoding: binary');


15. load file untuk di kirim:

readfile('contoh.zip');


16. Disable Caching:

header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Pragma: no-cache');


17. set content type:

header('Content-Type: text/html; charset=iso-8859-1');
header('Content-Type: text/html; charset=utf-8');
header('Content-Type: text/plain'); // text file
header('Content-Type: image/jpeg'); // Gambar JPG
header('Content-Type: application/zip'); // File ZIP
header('Content-Type: application/pdf'); // File PDF
header('Content-Type: audio/mpeg'); // File Audio MPEG (MP3,...)
header('Content-Type: application/x-shockwave-flash'); // Animasi Flash


18. tampilkan box sign in

header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="logcode"');
print 'Hehehe, pergi dari sini, anda tidak berhak untuk masuk';

Your rating: None Average: 1.2 (79 votes)