Redirect PHP To Another Page

Redirect PHP To Another Page

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.

Related Posts


3 thoughts on “Redirect PHP To Another Page

  1. kore dizileri izle

    Nice 1 blog proprietor triumph blog site put up fantastic sharings within this webpage constantly have entertaining

    Reply
  2. Cecila

    Good a single web site operator results blog site put up fantastic sharings in this weblog usually have pleasurable

    Reply
  3. Sterling Hilyard

    I simply want to say I am new to blogging and site-building and absolutely liked this web site. Almost certainly I’m want to bookmark your website . You absolutely have great writings. Thanks a bunch for sharing with us your web-site.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *


*