Page 1 of 1

How to return any previous site in PHP

Posted: Thu May 22, 2008 11:25 am
by tua1
Hello!

For example I have cart.php, and in this file:

<a href="products.php"><< Go back</a>

What I have do in PHP if I want to return always to the previuos site, without specify previous site, I mean return always to the location where cart.php is calling from.

Could anyone help?


Regards

Re: How to return in previous site in PHP

Posted: Thu May 22, 2008 11:33 am
by aditya2071990
use header(location:products.php) function, result I don't know thoroughly...better to test before you go ahead...and oh, use sessions to store all your variables, to make sure they are not destroyed...

Re: How to return in previous site in PHP

Posted: Thu May 22, 2008 1:47 pm
by tua1
aditya2071990 wrote:use header(location:products.php) function, result I don't know thoroughly...better to test before you go ahead...and oh, use sessions to store all your variables, to make sure they are not destroyed...

Thanks for your reply, but my goal is to return to any previuos page.

header(location:any_previous_page.php) is this possible??


Regards

Re: How to return any previous site in PHP

Posted: Thu May 22, 2008 2:26 pm
by VladSun
You may use $_SERVER['HTTP_REFERER'], though it's no reliable.

Re: How to return in previous site in PHP

Posted: Thu May 22, 2008 7:41 pm
by whiterabbit
tua1 wrote:
aditya2071990 wrote:use header(location:products.php) function, result I don't know thoroughly...better to test before you go ahead...and oh, use sessions to store all your variables, to make sure they are not destroyed...

Thanks for your reply, but my goal is to return to any previuos page.

header(location:any_previous_page.php) is this possible??


Regards
This is possible. All you need to do is on the page before you go to your cart, you do

$_SESSION['previous_page'] = http://www.yoursite.com/current_directory/$_SERVER['PHP_SELF']; // this will store in the users session variable the current page.

Then the user goes to the cart and does whatever it is that they do.

When you want to have your customer go back to the page that they came from you do a

header('Location: ' . $_SESSION['previous_page']);

Don't forget to put the $_SESSION['previous_page'] = http://www.yoursite.com/current_directory/$_SERVER['PHP_SELF']; on every page that they could come from.

Re: How to return any previous site in PHP

Posted: Thu May 22, 2008 8:16 pm
by califdon
Won't window.history.back() work?

Re: How to return any previous site in PHP

Posted: Thu May 22, 2008 8:28 pm
by whiterabbit
califdon wrote:Won't window.history.back() work?
This would definitely be the easiest if...

1. We are going to assume that javascript is enabled for the clients
2. There is only 1 page in the cart.

Right??

Re: How to return any previous site in PHP

Posted: Thu May 22, 2008 10:17 pm
by califdon
whiterabbit wrote:
califdon wrote:Won't window.history.back() work?
This would definitely be the easiest if...

1. We are going to assume that javascript is enabled for the clients
2. There is only 1 page in the cart.

Right??
1. Yes, absolutely. If that's an issue, then I guess you'd need to use HTTP_REFERER.
2. I don't see that that makes any difference, it always goes back just one URL, and I thought that's what the OP asked for.

Re: How to return any previous site in PHP

Posted: Fri May 23, 2008 6:16 pm
by tua1
Yes it is, thank you for your help

Regards