Headers in PHP

Discussion in 'PHP' started by pradeep, Oct 25, 2006.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in

    Background



    I've needed the header function in php so many times I figured I'd write a tutorial on it. This tutorial should help you do most of the things that the header function is capable of. Simply put, the php header function will send a http header to the browser.

    Headers in PHP



    If you are using the header function in between the code, you have to use ob_start() in the beginning of the code, because you have to send the headers before you send the content, so ob_start() buffers the code.It's similar to ASP's Response.Buffer = True.

    The following steps should teach you most of the things you need to know about php headers.

    The http header allows you to send many messages to the browser, the first of these that I'll explain is the location header.
    PHP:
    <?php
    header
    ("Location: http://www.go4expert.com");
    ?>
    This will send a new location to the browser and it will immediately redirect. It's recommended to put a full url there, however almost all browsers support relative urls.

    You can also control the content type that the browser will treat the document as:
    PHP:
    <?php
    header
    ("Content-Type: text/css");
    ?>
    You can now link to this file as css.php in your link to your css and dynamically create your css document depending on what browser or resolution the viewer has. This can be really helpful when designing css that works in every browser.

    You can also force the browser to display the download prompt and have a recommended filename for the download.
    PHP:
    <?php
    header
    ("Content-Type: image/jpeg");
    header("Content-Disposition: attachment; filename=file.jpg");
    ?>
    This will not show the file as it usually would in a browser, but as I mentioned before, display the downloads prompt and the filename will automatically be set to file.jpg regardless the filename of the php file. You can also force the page to be displayed inline by changing the content-disposition from attachment to inline.

    You can also send specific errors to the browser using the header function. It's important to remember the different error messages and what they mean.
    PHP:
    <?php
    header
    ("HTTP/1.0 404 Not Found");
    ?>
    Finally, I'd like to suggest that immediately following using a header, you use exit to make sure none of the code after it is executed (unless of course that code is used to make an image or needed in the file):
    PHP:
    <?php
    header
    ("Location: http://www.example.com/");
    exit;
    ?>
    If you need any help with this tutorial, I suggest before asking on the forums, looking at php's documentation of the function here.
     
  2. aiza julieta

    aiza julieta New Member

    Joined:
    Dec 19, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
  3. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Was that a query or a greeting?
     
  4. daworm

    daworm New Member

    Joined:
    Dec 13, 2006
    Messages:
    5
    Likes Received:
    1
    Trophy Points:
    0
  5. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    LOL! Well, I can't read minds. Try again in a human approach.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice