php Header tips

Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

php Header tips

Post by Neo » Wed Mar 10, 2010 11:06 pm

  1. 301 moved permanently
  2. 302 moved temporarily
  3. 404 page not found
  4. Service not available
  5. CSS
  6. Javascript
  7. Images(JPEG,BMP,PNG)
  8. PDF
  9. Force browsers not to cache pages
  10. Download dialog
  11. Authentication
PHP is not limited to outputting html. PHP can output images, pdf, javascript files, etc. Browsers determine what type of content is by analysing the headers sent. In this tutorial I will present you with some examples of how to send headers. To send PHP header use the function header(). You must call this function before any output. Use the function headers_sent() to check whether the headers have been sent and output started.


301 moved permanently (redirect)

Code: Select all

<?php 
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://www.example.com');
die();
?>

302 moved temporarily(redirect)

Code: Select all

<?php 
header('Location: http://www.example.com');
die();
?>

404 Page Not Found

Code: Select all

<?php 
header('HTTP/1.1 404 Not Found');
?>

Service not avaliable

Code: Select all

<?php 
header('HTTP/1.1 503 Service Temporarily Unavailable');
header('Status: 503 Service Temporarily Unavailable');
header('Retry-After: 60');
?>

CSS

Code: Select all

<?php
header('Content-Type: text/css');
?>

JavaScript header

Code: Select all

<?php 
header('Content-Type: application/javascript');
?>

Images
For JPEG(jpg):

Code: Select all

<?php 
header('Content-Type: image/jpeg');
?>
For PNG:

Code: Select all

<?php 
header('Content-Type: image/png');
?>
For BMP:

Code: Select all

<?php 
header('Content-Type: image/bmp');
?>

PDF (output pdf with php)

Code: Select all

<?php 
header('Content-Type: application/pdf');
echo file_get_contents('filename.pdf');
?>

Cache (force browsers not to cache files)

Code: Select all

<?php 
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: pre-check=0, post-check=0, max-age=0');
header ('Pragma: no-cache'); 
? 
>


Download dialog

Code: Select all

<?php 
header('Content-Disposition: attachment; filename=' . urlencode($f));   
header('Content-Type: application/force-download');
header('Content-Type: application/octet-stream');
header('Content-Type: application/download');
header('Content-Description: File Transfer');            
header('Content-Length: ' . filesize($f));
echo file_get_contents($f);
?>

Authentication
(force the browser to pop up a Username/Password input window) - only available when PHP is running as an Apache module:

Code: Select all

<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
    header('WWW-Authenticate: Basic realm="The Realm"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'If cancel is pressed this text shows';
    die();
} else {
//always escape your data//
$user='user';
$pass='pass';
   if($_SERVER['PHP_AUTH_USER']==$user && $_SERVER['PHP_AUTH_PW']==$pass){
    echo 'Authorized';
}
}
?>
Post Reply

Return to “PHP & MySQL”