With PHP it is possible to redirect to another page using the header() network function. The header() function is used to send a raw HTTP header why it can easily be used transfer a user to a new page without clicking on a link. If you are not familiar with the header() function you need to be aware that just like in any other web programming language it is required to write the headers before the actual page content is sent. So if you experience any errors keep in mind that it is a common error to read code with e.g. include() or require() functions and have spaces or empty lines that are output before header() is called. In this tutorial I will show you in detail how to use header() to perform redirects in PHP.
PHP redirect to another page is in some cases useful and it is easy to do as well using one line of code. To do it right however I recommend you add and exit(); also to stop any execution of the rest of the page.
<?php /* Remember header() must be called before any actual output is sent */ /* Redirect browser */ header("Location: http://www.tripwiremagazine.com/"); /* For good order don’t risk the code below the redirect to be executed when we redirect. */ exit; ?>
In order to make it absolutely clear what is meant by “header() must be called before any actual output is sent” I have added an example that will not work.
<?php /* The echo construct output text directly to the page */ echo "This is written to the page!!!"; /* Remember header() must be called before any actual output is sent (normal HTML tags, blank lines in a file, or from PHP) */ header("Location: http://www.tripwiremagazine.com/"); exit; ?>
Both the <html> and the use of echo before the header redirect will make this code fail.
It is important to note that headers are actually sent when the first byte is output to the browser. If you are replacing headers in your scripts, this means that the placement of echo/print statements and output buffers may actually impact which headers are sent. In the case of redirects, if you forget to terminate your script after sending the header, adding a buffer or sending a character may change which page your users are sent to.
This redirects to 2.html since the second header replaces the first.
<?php header("location: 1.html"); header("location: 2.html"); //replaces 1.html ?>
This redirects to 1.html since the header is sent as soon as the echo happens. You also won’t see any “headers already sent” errors because the browser follows the redirect before it can display the error.
<?php header("location: 1.html"); echo "send data"; header("location: 2.html"); //1.html already sent ?>
Wrapping the previous example in an output buffer actually changes the behavior of the script! This is because headers aren’t sent until the output buffer is flushed.
<?php ob_start(); header("location: 1.html"); echo "send data"; header("location: 2.html"); //replaces 1.html ob_end_flush(); //now the headers are sent ?>
Please share your experience and thoughts on this “php redirect to another page” tutorial in a comment.
Thanks for visiting us.