how can I redirect to the previous page in PHP ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aneuryzma
Forum Contributor
Posts: 106
Joined: Sat May 17, 2008 7:03 am

how can I redirect to the previous page in PHP ?

Post by aneuryzma »

How can I ask to redirect to the previous page using PHP ?

My user login and the login-submit page is loaded:

Code: Select all

<?php
 
session_start();
 
session_destroy();
 
//header("Location: -- previous page --);
 
?>
Last edited by Benjamin on Wed May 06, 2009 12:07 pm, edited 1 time in total.
Reason: Changed code type from text to php.
aneuryzma
Forum Contributor
Posts: 106
Joined: Sat May 17, 2008 7:03 am

Re: how can I redirect to the previous page in PHP ?

Post by aneuryzma »

ups sorry, it was very easy

Code: Select all

 
<?php
 
session_start();
 
session_destroy();
 
 
$ref = $_SERVER['HTTP_REFERER'];
header( 'refresh: 2; url='.$ref);
echo "You are going to be redirected..";
 
?>
 
Last edited by Benjamin on Wed May 06, 2009 12:07 pm, edited 1 time in total.
Reason: Added code tags.
aneuryzma
Forum Contributor
Posts: 106
Joined: Sat May 17, 2008 7:03 am

Re: how can I redirect to the previous page in PHP ?

Post by aneuryzma »

I've one more question:

can I remove the refresh delay ?
If no, what's the minimum value I should assign ?

thanks
Defiline
Forum Commoner
Posts: 59
Joined: Tue May 05, 2009 5:34 pm

Re: how can I redirect to the previous page in PHP ?

Post by Defiline »

It seems to me that Apple have already replied to you :)
However ... you want to set a time delay?
You can use sleep($time) to set a delay, but be noticed that you cannot redirect user to a previous page if you have flushed any content.
Therefore delaying, I think, is very useless.

But you can use HTML Meta redirection (it may be set on flushed page)
aneuryzma
Forum Contributor
Posts: 106
Joined: Sat May 17, 2008 7:03 am

Re: how can I redirect to the previous page in PHP ?

Post by aneuryzma »

the delay is already set with "refresh: 2" in the header line.

Can I set it to 0 ? Or should I leave some delay in order to allow data to process ?
Defiline
Forum Commoner
Posts: 59
Joined: Tue May 05, 2009 5:34 pm

Re: how can I redirect to the previous page in PHP ?

Post by Defiline »

Can I set it to 0 ?
Yes, you can.

Code: Select all

<?php
 
session_destroy();
// Logging out
// Closing of sessions
// $_SESSION['logged'] = false;
 
$ref = $_SERVER['HTTP_REFERER'];
 
header( 'refresh: 0; url='.$url);
echo "You are going to be redirected..";
 
?>
Never believe in $_SERVER['HTTP_REFERER'] :)
aneuryzma
Forum Contributor
Posts: 106
Joined: Sat May 17, 2008 7:03 am

Re: how can I redirect to the previous page in PHP ?

Post by aneuryzma »

what's better to use than $_SERVER['HTTP_REFERER']; ?

thanks
Defiline
Forum Commoner
Posts: 59
Joined: Tue May 05, 2009 5:34 pm

Re: how can I redirect to the previous page in PHP ?

Post by Defiline »

The best way is sessions.
Set this value on every page:
$_SESSION['current_page'] = $_SERVER['PHP_SELF'];

We can modify your script:

Code: Select all

<?php
 
$previous_page = $_SESSION['current_page']; // this is our previous page
 
session_destroy();
// Logging out
// Closing of sessions
// $_SESSION['logged'] = false;
 
header( 'refresh: 0; url='.$previous_page);
echo "You are going to be redirected..";
 
?>
This variant is more suitable.
aneuryzma
Forum Contributor
Posts: 106
Joined: Sat May 17, 2008 7:03 am

Re: how can I redirect to the previous page in PHP ?

Post by aneuryzma »

ok thanks!
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: how can I redirect to the previous page in PHP ?

Post by McInfo »

$_SERVER['PHP_SELF'] is not safe either.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 3:55 pm, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: how can I redirect to the previous page in PHP ?

Post by pickle »

Use

Code: Select all

header("Location: http://fully.qualified.url");
rather than a refresh header.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply