Friday, 26 June 2015

PHP: Redirect Web Page to Another Web Page: PHP Redirect Script

How to redirect from one page to another ? It is a common question from beginner developers in PHP. You can easily transferred to the new page without having to click a link to continue by using a default power of PHP which is also useful for search engines.,
 
header();
PHP Redirect Script

You can easily redirect using following code:

   
/*
|-----------------
| Redirect
|------------------
*/

header ( "Location: https://thiscode4u.blogspot.com" );

/*
|-----------------
| Stop Execution
|------------------
*/

exit;
Stop Execution

It is very important to stop the script after sending header command. “exit” will ensure to stop the execution of next code or algorithm before the completion of header command.

/*
|-----------------
| Stop Execution
|------------------
*/

exit;
Very Important

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

   
/*
|-----------------
| Bad Code Example
| Some output before header()
|------------------
*/

echo "Some Output: Hello World!";

/*
|-----------------
| Redirect
|------------------
*/

header ( "Location: https://thiscode4u.blogspot.com" );

/*
|-----------------
| Stop Execution
|------------------
*/

exit;

0 comments:

Post a Comment