Doing page redirection in PHP

Hi,

In php header() function is used to redirect page.
Syntax:-
header(“Location:newpage.php”);

header() must be used before sending any output either by HTML, blank lines or from PHP.
Otherwise error message “header already sent” error shown.

To overcome this problem, we can use  javascript for doing page redirection as below

window.location=”newpage.php”;

But if the JavaScript is disabled by the browser, then it will not work.

So we need can write a function in PHP like below,

<?php
function redirect($url=””)
{
if (!headers_sent()) header(“Location:$url”);
else
echo “<script>window.location=\”$url\”; </script>
<noscript>Automatic redirection didn’t work.<br />
<a href=\”$url\”>Click here to go to newpage.</a> </noscript>”;
}
$url=’newpage.com’;
redirect($url);
?>

Advertisements

PHP Developer..

Tagged with: ,
Posted in javascript, PHP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: