Header function in php

The header() function sends a raw HTTP header to a client. It is important to notice that header() must be called before any actual output is sent (In PHP 4 and later, you can use output buffering to solve this problem):

header(string,replace,http_response_code)

File name : index.php

<?php
// The output above is before the header() call
header('Location: http://www.itechxpert.in/');
exit; ?>

File name : index.php

<?php
header("Content-type:application/pdf");

// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='downloaded.pdf'");

// The PDF source is in original.pdf
readfile("original.pdf");
?>

header('Refresh:')

if you wanted to give the user a little bit of time to see a message or something before you sent them elsewhere. Redirecting a user to another page can be confusing for them if there is no warning at all. Using a header refresh you can give some time for the user to see a message before it sends them off to another page.

File name : index.php

<?php
header('Refresh:10; url=http://www.itechxpert.in')
echo 'You will be redirected to itechxpert in 10 seconds.';
?>

Output :-

The redirect above will refresh the page in ten seconds and when it refreshes, it will send the user to itechxpert. It also echos out a message to the user that they will be redirected.

header('Content-Type:')

You see it in the meta tag of your HTML document that tells the browser what type of document it is and what to expect.

With a PHP Content-Type header, you can change how you want the browser to read the page. For normal HTML pages, the Content-Type will be the normal text/html. But, you could change that in the header to be text/plain and the browser will display the source code of your site. You can also use it to display PDF's and other documents. The code looks

File name : index.php

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<?php
header('Content-Type: text/html')
?>
<!DOCTYPE html PUBLIC "-//ittutorialC//DTD XHTML 1.0 Transitional//EN"
"http://www.ittutorial.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

header('Content-Disposition:')

With the Content-Type, you tell the browser what type of document to expect. With Content-Disposition, you tell the browser how to handle the document. If you have a PDF that you want users to download, you can use this Content-Disposition to make the browser display a save dialog. You set the Content-Disposition to attachment and then you must put the filename so that it can be downloaded.

File name : index.php

<?php
header('Content-Disposition: attachment; filename="example.pdf"')
?>
<!DOCTYPE html PUBLIC "-//ittutorialC//DTD XHTML 1.0 Transitional//EN"
"http://www.ittutorial.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

header('Cache-Control:')

File name : index.php

<?php
header('Cache-Control: no-cache, must-revalidate');
?>
<!DOCTYPE html PUBLIC "-//ittutorialC//DTD XHTML 1.0 Transitional//EN"
"http://www.ittutorial.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

header('Expires:')

File name : index.php

<?php
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
?>
<!DOCTYPE html PUBLIC "-//ittutorialC//DTD XHTML 1.0 Transitional//EN"
"http://www.ittutorial.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Caution before Redirect

Before executing PHP redirect, we should ensure about, no output is sent to the browser before the line where we call header() function. For example,
echo "PHP Redirect";
header("Location: gmaxlifesciences.com");
This script will display the following warning notice to the browser.
Warning: Cannot modify header information - headers already sent by (...
It is not only applicable for header function, rather for all the PHP functions like set_cookie(), session_start() and etc., whatever can modify the header. For that, we should remove all content which will stop sending location header to the browser.

Possible Ways of Sending Output

  • HTML content like text or tags.
  • Unnecessary white spaces before PHP delimiters.
  • PHP error or warning notices that occur before calling redirect.
  • PHP print statements, like, echo(), print().
  • Safety Measures from output being Sent before PHP Redirect

    Since html content should be sent before redirect, we can separate PHP logic from HTML content.
    For being in safty side we can put exit command after redirect statement of PHP file. For example.
    header("Location: phppot.com");
    exit;

    We can enable PHP output buffering to stop sending output to the browser and store into a buffer instead. For example.
    ob_start(); // Output Buffering on
    header("Location: phppot.com");
    exit;





    Previous Next


    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here