How to return any previous site 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
tua1
Forum Commoner
Posts: 28
Joined: Thu May 15, 2008 9:30 pm

How to return any previous site in PHP

Post 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
Last edited by tua1 on Thu May 22, 2008 1:51 pm, edited 1 time in total.
User avatar
aditya2071990
Forum Contributor
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India
Contact:

Re: How to return in previous site in PHP

Post 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...
tua1
Forum Commoner
Posts: 28
Joined: Thu May 15, 2008 9:30 pm

Re: How to return in previous site in PHP

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: How to return any previous site in PHP

Post by VladSun »

You may use $_SERVER['HTTP_REFERER'], though it's no reliable.
There are 10 types of people in this world, those who understand binary and those who don't
whiterabbit
Forum Newbie
Posts: 14
Joined: Thu May 22, 2008 6:58 pm

Re: How to return in previous site in PHP

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: How to return any previous site in PHP

Post by califdon »

Won't window.history.back() work?
whiterabbit
Forum Newbie
Posts: 14
Joined: Thu May 22, 2008 6:58 pm

Re: How to return any previous site in PHP

Post 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??
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: How to return any previous site in PHP

Post 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.
tua1
Forum Commoner
Posts: 28
Joined: Thu May 15, 2008 9:30 pm

Re: How to return any previous site in PHP

Post by tua1 »

Yes it is, thank you for your help

Regards
Post Reply