Lesson 7 PHP Headers

In this lesson you learn about PHP Headers or Hypertext Pre Processor there are two types of headers.Request headers that your browser sends to the server when you request a file, and then we have Response headers that the server sends to the browser when it serves the file.

Sub Objective

This lesson will consist of PHP headers

You should have a moderate knowledge of html and php .

Make sure to have no spaces at the start of your headers so have them look like this

<?php
          session_start();
	  	  echo "Welcome";
		  ?>
		  

Try Hovering after <?php notice there is no space there make sure there is never anything before

Get the code for this lesson download here this same link will be at the bottom. This code will have obvious mistakes for you to fix as well it's just a way for you to work on your trouble shooting .

header('Location:');

The 'Location' PHP header is used in redirecting users to another page after the url has completed loading. This could be used in the event of a website migration to lead the user to the appropriate page with a new domain.


		   <?php
			header("HTTP/1.1 301 Moved Permanently");
			header("Location: http://danielmentzer.aisites.com/cbt2/");
            exit;
			?>
		   

header('Refresh:');

With header refresh you are able to have the page wait a certain amount of time before it goes to the page you are sending it to.


            <?php
            $seconds = 5; //will redirect after 5 seconds
            header("Refresh: $seconds; url=http://danielmentzer.aisites.com/cbt2/");
            exit;
            ?>
            

Make sure at the end of each header your exiting the script to make sure that you don't waste unnecessary resources.

header('Content-Type:');

The PHP Header 'Content-Type' is used to determine the format of the information you want to display to the visitor. You can use different types of data other than HTML, for example, you can parse an image, psd, zip etc from PHP. If you do that, you need to tell the browser that the content i am sending is not HTML but something else. So what are we going to tell the browser. Why not tell it we are sending it plain text so were going to make a christmas list.


     <?php
   
    header ( "Content-type: text/plain" );
    
 
    ?>
    
    <html>
    <head>
    
    <title>Christmas List </title>
    
    
    </head>
    <body>
    <ul>
    <li>Wii U</li>
    <li>New Phone</li>
    <li>Deadpool The Game</li>
    <li>New Computer</li>
    </ul>
	</body>
    </body>
     
     

header('Content-Disposition:');

So with this header it allows us to create those download windows we see all the time when we follow the link

Download The Square

<?php
header('Content-Type: image/jpeg'); 
header('Content-Disposition: attachment;filename="square.jpg"');
readfile('lib/img/square.jpg');
exit;



?>
              
              

header('Cache-Control:');

This Header allows us to keep the page from cache the page.


            <?php
            
           
            header('Cache-Control: no-cache, no-store');
            //HTTP/1.0
            header("Pragma: no-cache");
            ?>
            

header('Expires:');

With the expire header it gives us the ability to tell our users browsers that they have old copies of are site so they get new copies of it.


             <?php

header("Expires: Mon, 20 Feb 2004 20:12:03 GMT");


header("Last-Modified: ". gmdate("D, d M Y H:i:s"). "GMT");


header("Pragma: no-cache");
?>
             
             

Check out W3 Schools for more PHP Header.