23 Ocak 2012 Pazartesi

PHP HTTP Header (Başlık) Örnekleri

Sürekli kullandığımız ve her daim elimizin altında bulunması gereken Header örnekleri. Çok sık lazım olmayan ama olduğunda da yazılışını unuttuğumuz alet çantasında bulunması gereken kodlar.

// Durum (status) kodları

// Erişim başarılı
header('HTTP/1.1 200 OK');

// Sayfa bulunamadı:
header('HTTP/1.1 404 Not Found');

// Yetki yetersiz:
header('HTTP/1.1 403 Forbidden');

// Dosya taşınmış.
// Arama motorları için önemli bir detay
header('HTTP/1.1 301 Moved Permanently');

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

// Yöneldnri
header('Location: http://www.example.org/');

// 10 sn sonra belirtilen URL'ye yönlendir:
header('Refresh: 10; url=http://www.example.org/');
print '10 sn sonra yönlendirileceksiniz';

// Yukarıdaki örneğin HTML ile yapılması:
// <meta http-equiv="refresh" content="10;http://www.example.org/ />

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

// içerik dili (en = İngilizce)
header('Content-language: en');

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

// içerik değiştirilmedi
header('HTTP/1.1 304 Not Modified');

// içerik boyutu (cache için):
header('Content-Length: 1234');

// Dosya indirme başlıkları:
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="example.zip"');
header('Content-Transfer-Encoding: binary');
// Gönderilen dosyası yükle:
readfile('example.zip');

// Belirli bir dosya/sayfa için cache uygulatmamak:
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');

// içerik dili:
header('Content-Type: text/html; charset=iso-8859-9');
header('Content-Type: text/html; charset=utf-8');
header('Content-Type: text/plain'); // text
header('Content-Type: image/jpeg'); // JPG resmi
header('Content-Type: application/zip'); // ZIP dosyası
header('Content-Type: application/pdf'); // PDF dosyası
header('Content-Type: audio/mpeg'); // MPEG (MP3,...) ses dosyası
header('Content-Type: application/x-shockwave-flash'); // Flash animasyon dosyası

// Tarayıcı ile açılan kullanıcı girişi için:
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Bilgilerinizi Giriniz"');
print 'Bu mesaj giris iptal edildiginde veya ';
print 'hatali bilgi girildiginde ekrana yazdirilir';

Kaynak: JonasJohn

0 yorum:

Yorum Gönder