Redirecting users to another page

Updated cwarn23 4 Tallied Votes 2K Views Share

In php/html/javascript there are two different ways you may redirect a user to another page however there are a few things to consider before choosing with method to use. As you should know by now php has the ability to send html and javascript to the web browser and php can contribute to html and javascript functionality by generating scripts on the fly. So first I shall demonstrate php's ability to generate scripts on the fly.

#method 1

<?php
$page='http://www.google.com/';
// $page=''; //will disable redirect
// some massave code above here
?>
<head>
<title>A test page</title>
<?php
if (strlen($page)>0) {
echo <<< DATA
<meta http-equiv="refresh" content="0;url={$page}">
<script>
window.location="{$page}";
</script>
DATA;
}
?>
</head>

Now in the above script you will see that it uses client side scripting for the redirect. In this case it uses both the meta redirect and javascript redirect so that it if javascript is disabled the browser can meta redirect or if meta redirect is disabled then the browser can javascript redirect. So if you want client side redirect that is the basic way to go about it keeping in mind client side redirecting isn't always the best option. Keep in mind that with client side redirecting, you are not guaranteed to make the user redirect. In some cases both javascript and meta redirect will be disabled. Luckily php has it's own solution. So lets see what php has installed for us.

#method 2

<?php
$page='http://www.google.com/';
// $page=''; //will disable redirect
if (strlen($page)>0) {
    header('Location: '.$page); //no browser output before this line
    exit;
    }
?>

In this script you will see that it does exactly the same thing as the client side script except it's server side. To understand how this script works you simply insert the page you want to redirect to in the $page variable. If the $page variable is empty then the if statement will not pass meaning it will not execute the redirect. If however you have specified a page then it will add to the headers a redirect provided you haven't fed the browser any output or text. That means before the header function you can't even have the <html> tag outputted yet. It still has to be pure php at that point. And after the header redirect you should always follow it by the exit; statement as it will save php from executing the rest of the script (and you don't want that).

One might ask what the benefits of server side scripting a redirect are. Well the main reason is that it cannot be disabled and is more widely supported. Also header redirects require a lot less code provided they are used properly and if you really need html output before it then put your html output into a variable and echo that variable after the header function.
For example:

<?php
$text=<<< DATA
<head>
<title>Processing...</title>
</head>
<body>
This page is now processing...
DATA;
//html stuff in $text variable

$page='http://www.google.com/';
// $page=''; //will disable redirect
if (strlen($page)>0) {
    header('Location: '.$page);
    exit;
    }

echo $text; //display html
?>
</body>

But as one may know that is not always the solution because you want to display something before something processes. That is where php has a neat function named popen(). I'm not going to say too much about the popen() function here as it is a redirect tutorial but what I will say is if you want to do a lot of processing an not want the user to wait for the redirect then you can open a background script with popen() and the user will have a nicer experience as they can browse around while they wait since they don't see a the page that is been executed. It's much like a cron job.

The conclusion:
Try to always use the header() function whenever possible even if it comes down to opening another thread with popen(). But only use javascript and meta tags as a last resort.

tutorials4web 0 Newbie Poster

Hi Dear,

If you want to redirect on to another page then you have two option,

you can choose client side scripting language or you may choose server side scripting scripting language.


1) By using client side scripting language...

A) HTML

<html>
<head>
<meta http-equiv="refresh" content="5;url="http://www.example.com">
</head>
<body>
xxxxxxx
xxxxxxxx

</body>
</html>

--------------------------------------
B) JAVASCRIPT

---------------------------------------

<html>
<head>

<script>


<script>
window.location="http://www.example.com";
</script>
</script>

</head>
<body>
xxxxxxx
xxxxxxxx

</body>
</html>

In php/html/javascript there are two different ways you may redirect a user to another page however there are a few things to consider before choosing with method to use. As you should know by now php has the ability to send html and javascript to the web browser and php can contribute to html and javascript functionality by generating scripts on the fly. So first I shall demonstrate php's ability to generate scripts on the fly.

#method 1

<?php
$page='http://www.google.com/';
// $page=''; //will disable redirect
// some massave code above here
?>
<head>
<title>A test page</title>
<?php
if (strlen($page)>0) {
echo <<< DATA
<meta http-equiv="refresh" content="0;url={$page}">
<script>
window.location="{$page}";
</script>
DATA;
}
?>
</head>

Now in the above script you will see that it uses client side scripting for the redirect. In this case it uses both the meta redirect and javascript redirect so that it if javascript is disabled the browser can meta redirect or if meta redirect is disabled then the browser can javascript redirect. So if you want client side redirect that is the basic way to go about it keeping in mind client side redirecting isn't always the best option. Keep in mind that with client side redirecting, you are not guaranteed to make the user redirect. In some cases both javascript and meta redirect will be disabled. Luckily php has it's own solution. So lets see what php has installed for us.

#method 2

<?php
$page='http://www.google.com/';
// $page=''; //will disable redirect
if (strlen($page)>0) {
    header('Location: '.$page); //no browser output before this line
    exit;
    }
?>

In this script you will see that it does exactly the same thing as the client side script except it's server side. To understand how this script works you simply insert the page you want to redirect to in the $page variable. If the $page variable is empty then the if statement will not pass meaning it will not execute the redirect. If however you have specified a page then it will add to the headers a redirect provided you haven't fed the browser any output or text. That means before the header function you can't even have the <html> tag outputted yet. It still has to be pure php at that point. And after the header redirect you should always follow it by the exit; statement as it will save php from executing the rest of the script (and you don't want that).

One might ask what the benefits of server side scripting a redirect are. Well the main reason is that it cannot be disabled and is more widely supported. Also header redirects require a lot less code provided they are used properly and if you really need html output before it then put your html output into a variable and echo that variable after the header function.
For example:

<?php
$text=<<< DATA
<head>
<title>Processing...</title>
</head>
<body>
This page is now processing...
DATA;
//html stuff in $text variable

$page='http://www.google.com/';
// $page=''; //will disable redirect
if (strlen($page)>0) {
    header('Location: '.$page);
    exit;
    }

echo $text; //display html
?>
</body>

But as one may know that is not always the solution because you want to display something before something processes. That is where php has a neat function named popen(). I'm not going to say too much about the popen() function here as it is a redirect tutorial but what I will say is if you want to do a lot of processing an not want the user to wait for the redirect then you can open a background script with popen() and the user will have a nicer experience as they can browse around while they wait since they don't see a the page that is been executed. It's much like a cron job.

The conclusion:
Try to always use the header() function whenever possible even if it comes down to opening another thread with popen(). But only use javascript and meta tags as a last resort.

tutorials4web 0 Newbie Poster

Hi Dear,

If you want to redirect on to another page then you have two option,

you can choose client side scripting language or you may choose server side scripting scripting language.

1) By using client side scripting language... A) HTML

<html>
<head>
<meta http-equiv="refresh" content="5;url="http://www.tutorials4web.in">
</head>
<body>
xxxxxxx
xxxxxxxx

</body>
</html>

-------------------------------------- B) JAVASCRIPT

---------------------------------------

<html>
<head>

<script>


<script>
window.location="http://www.tutorials4web.in";
</script>
</script>

</head>
<body>
xxxxxxx
xxxxxxxx

</body>
</html>


2) By USing Server side scripting language

<?php
header('location:www.example.com');
?>
rajeevphp2011 -3 Newbie Poster
php

header('location:www.google.com');

javascript :

window.location = 'www.google.com';
Member Avatar for diafol
diafol

Can't see a reference to this above:

<?php
    header("Refresh: 6; url='http://www.google.com'");
    echo "<p>You will be redirected to <em>Goooooogle</em> in 6 seconds...</p>"; //do any display here
?>

Similar to the meta refresh with delay. Better than client side. Then 'popen' solution may cause headaches on Win machines. You may need to run as admin.

gaza331 0 Newbie Poster

thx
it's usefull

:)

itsPHP.net 0 Newbie Poster

im going to show the same method by keeping it very simple and easy:

First Create a function on the name of redirect_to($location) and put a receiving parameter in it:

code is:

function redirect_to($location=NULL){
    if($location != NULL){
        header("Location: $location");
        exit;
    }
}




redirect_to("register.php");   // call the function by passing the page adress.
OsaMasw 13 Loving Helper

I use the Meta method because every time I use Header I saw this message
header already sent .........etc

Member Avatar for diafol
diafol

header already sent .........etc

In that case ensure that you don't have any output/BOMs before calling the header. I'd always suggest the header() redirect method before anything else.

Webville312 14 Newbie Poster

You can alternatively simply do this;

header("Location: page_to_redirect.php");

That should work for you.

coreyavis 13 Dev Poster

In javascript, it might be best to use:

window.location.replace('page_to_redirect.php');
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.