PHP redirect mechanism is used to navigate the user from one page to another without clicking any hyperlinks. This will be helpful in such circumstances where the redirect should be done in the background. For example, when the user is accessing payment gateway, the redirect should automatically be taken place to notify URL using PHP script.
PHP provides predefined function, named header(),for URL redirection. Using this header() function, we need to send location header by specifying URL to which the page should be redirected.
PHP Redirect Syntax
In the above syntax of PHP redirect, we need to replace with a valid URL to which we want to move. We can specify either absolute URL or relative URL for this location header. If we specify relative URL, it will search for the page in our domain where we exist.
Note: Before specifying page URL for location header, we should make sure that the page exists.
Caution before Redirect
Before executing PHP redirect, we should ensure about, no output is sent to the browser before the line where we call the header() function. For example,
echo "PHP Redirect"; header("Location: f5craft.in");
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().
PHP Functions used for Printing Data
print() – The print() is used to create PHP print statement to print given data to the browser. It accepts single data and prints it on the browser. It is a PHP language construct and not a function. So, we can use ‘print’ without parenthesis for creating a print statement. The code shows an example for using print with and without parenthesis where both will print same data.
print "Apple"; //(or) print("Apple");
echo() – The echo() is also not a PHP function. It is also a construct as like as print(). The difference is that the echo() will accept multiple data separated by commas. While sending multiple values to the echo() statement, we have to use parenthesis to enclose the values. If we use single data in an echo() statement, we can ignore parenthesis. In the code, I use echo() statement for printing multiple data and stated the output using PHP comment line.
echo "Apple"; //(or) echo("Apple"); // Output AppleOrangeGrapes echo "Apple","Orange","Grapes"; // not a valid statement echo ("Apple","Orange","Grapes");
printf(string_format, values) – This method is used print the formatted output by using the values passed as the parameter of this function. So, this functions accepts the output string format and the values to be added to be added. For example,
printf('We are expected to score above %d%% for distinction', 85); //Output: We are expected to score above 85% //for distinction
sprintf() – This is similar to the printf() function except that it can return the formatted string instead of printing it to the browser. Then we can store it into a variable.
print_r() – This function is used to print the compound data like PHP array or objects.
var_dump() – var_dump() also prints array data in structured manner. It gives additional data, like, the data type, the length, values and more. For example,
var_dump(false) // prints bool(false) print_r(false) // returns empty string
Safety Measures from output being Sent before PHP Redirect
- Since HTML content should be sent before the redirect, we can separate PHP logic from HTML content.
- For being in the safety side we can put exit command after redirect statement of PHP file. For example,
header("Location: f5craft.in"); exit;
- We can enable PHP output buffering to stop sending output to the browser and stored into a buffer instead. For example,
ob_start(); // Output Buffering on header("Location: f5craft.in"); exit;
In interactive web applications, users can navigate from one page to another. Such navigation is possible by the use of hyperlinks, functions of client and server side scripts. For example, in PHP, the header() function is used to redirect by specifying location URL.
This property of window object indicates current page URL. And we can switch over to another page by setting this property to required URL. For example,
By setting this windows to navigate property, we can perform page redirect. But, this property works only for Internet Explorer. And, an URL can be assigned to this property, as like as we have seen to set it with window’s location property above.
For example, we can replace the following line,
This property works as same as windows.location.half except for the pages that include frames. When we use this property within the frame to set location, then the content of the frame will be changed with the specified location’s content. For example, save the following HTML content as redirect.html.
When we open this file with the browser, the redirect button click event will navigate us to the URL, https://f5craft.in. In another case, if we use this file as a source of a frame like,
then, then the button click event will replace the frame content with the content of the page https://f5craft.in.
Like the location properties we have seen above, we can use top.location.href, parent.location.href based on the place from where the redirection is going to take place. When we use simply location.href, then, by default this will be set to the window object.
If we want to go back to the previous page after the redirect, we can refer history by using back() function. For example,