Page 1 of 1

how can I redirect to the previous page in PHP ?

Posted: Wed May 06, 2009 4:44 am
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 --);
 
?>

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

Posted: Wed May 06, 2009 4:48 am
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..";
 
?>
 

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

Posted: Wed May 06, 2009 4:51 am
by aneuryzma
I've one more question:

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

thanks

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

Posted: Wed May 06, 2009 8:14 am
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)

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

Posted: Wed May 06, 2009 8:20 am
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 ?

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

Posted: Wed May 06, 2009 8:29 am
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'] :)

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

Posted: Wed May 06, 2009 8:37 am
by aneuryzma
what's better to use than $_SERVER['HTTP_REFERER']; ?

thanks

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

Posted: Wed May 06, 2009 8:43 am
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.

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

Posted: Wed May 06, 2009 9:10 am
by aneuryzma
ok thanks!

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

Posted: Wed May 06, 2009 9:43 am
by McInfo
$_SERVER['PHP_SELF'] is not safe either.

Edit: This post was recovered from search engine cache.

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

Posted: Wed May 06, 2009 2:35 pm
by pickle
Use

Code: Select all

header("Location: http://fully.qualified.url");
rather than a refresh header.