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)
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.
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
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.
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.
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;
Trending Tutorials