Doing page redirection in PHP


In php header() function is used to redirect page.

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


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,

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

Tagged with: ,
Posted in javascript, PHP

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: