SQL PHP HTML ASP JavaScript articles and free scripts to download
 

Redirect PHP script in pages & types of redirect


Redirecting a visitor or the browser is required in different situations. If a a page is temporarely down then we can use HTTP 302 redirect. Same way permanent redirection is HTTP 301 redirection. All the 3xx are different types of redirection and out of them 301 and 302 are common. HTTP 301 permanent redirection is well accepted by search engines also. We will discuss PHP and JavaScript redirection methods here.

We will be using Location Header function to redirect a page. This script works when we have not send any php or html command to out put any thing in the browser. Care is to be taken not to send any other thing to the browser after the header redirection using location command. Here is the code.
Related Tutorial
Conditional Redirect
IP address in PHP
PHP file upload
PHP paging
PHP drop down list

header ("Location: http://www.starjokes.com");

/* Redirect browser to starjokes.com  web site */

exit;    // Closes further script execution .

You can see this code above will redirect the page to a new url.

Now we will write the code for 301 redirection (page permanently moved )

header("HTTP/1.1 301 Moved Permanently");
header ("Location: http://www.allkm.com/km-basics/data.php");
exit;


If we send any html tag or any other data to the browser before the redirection command, then we will get an error message like this

Warning: Cannot modify header information - headers already sent by (output started at L:\php_files\t\test6.php:13) in L:\php_files\t\test6.php on line 14


By using JavaScript also the redirection can be done. Here is the code printing the javascript code for the browser to redirect. But this will work if JavaScript is enabled in the browser.

print "<script>";
print " self.location='mynewpage.html';";
print "</script>";

You can read about Meta Redirect here

PHP Redirect
Header Redirect & warning messages in PHP
Conditional Redirection
Include or calling files inside PHP pages
Include Once a file
PHP IP


Sections
PHP
JavaScript
ASP
HTML
SQL
Photoshop
Articles SEO
PHP Tutorials
PHP Monthly Planner
PHP Introduction
Loops & structure
Array
Date & Time
Functions
Form Handling
File Handling
Math Functions
String Functions
GD Functions
Comment Posting
Content Management
PHP & Ajax
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.