1,105,225 Community Members

Redirecting users to another page

Member Avatar
Reputation Points: 387 [?]
Q&As Helped to Solve: 281 [?]
Skill Endorsements: 15 [?]
 
4
 

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.

Member Avatar
tutorials4web
Newbie Poster
5 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
tutorials4web
Newbie Poster
5 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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');
?>
Member Avatar
rajeevphp2011
Light Poster
26 posts since Nov 2011
Reputation Points: -3 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 1 [?]
 
0
 
php

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

javascript :

window.location = 'www.google.com';
Member Avatar
diafol
Where are my eyes?
12,941 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 90 [?]
Moderator
Featured
Sponsor
 
2
 

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.

Member Avatar
gaza331
Newbie Poster
2 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

thx
it's usefull

:)

Member Avatar
itsPHP.net
Newbie Poster
1 post since Sep 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.
Member Avatar
OsaMasw
Posting Whiz in Training
212 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
diafol
Where are my eyes?
12,941 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 90 [?]
Moderator
Featured
Sponsor
 
1
 

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.

Member Avatar
Webville312
Posting Whiz
363 posts since Feb 2012
Reputation Points: 14 [?]
Q&As Helped to Solve: 34 [?]
Skill Endorsements: 0 [?]
 
0
 

You can alternatively simply do this;

header("Location: page_to_redirect.php");

That should work for you.

Member Avatar
coreyavis
Junior Poster in Training
65 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 2 [?]
 
0
 

In javascript, it might be best to use:

window.location.replace('page_to_redirect.php');
You
Post:
Start New Discussion
View similar articles that have also been tagged: