To get previous page URL

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
bryansu
Forum Newbie
Posts: 9
Joined: Mon May 08, 2006 2:53 am

To get previous page URL

Post by bryansu »

When people click and visit my website, i want to know where they come from. Just want to know their previous URL when visitor visit my website.

I try to use HttpRequest::getUrl ( ), but the information provided is not enough. So i don't know how to use it. Can this command do it? Anyway i can get previous URL?
User avatar
dxm
Forum Newbie
Posts: 8
Joined: Fri Jun 29, 2007 5:18 pm
Location: Wales

Post by dxm »

Use: $_SERVER['HTTP_REFERER']

Or you could use Google Analytics for more detailed information about referrers.
bryansu
Forum Newbie
Posts: 9
Joined: Mon May 08, 2006 2:53 am

Post by bryansu »

I forget something. I cant use $_SERVER['HTTP_REFERER'] as i am using redirect show below.

header("location : example.php");
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Set the information in a session variable.
bryansu
Forum Newbie
Posts: 9
Joined: Mon May 08, 2006 2:53 am

Post by bryansu »

Currently i am using session to do so. But when the file get more and more, is very hard to maintain it. I prefer to use HTTP_REFERER.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Depending on what you're wanting, previous page information can be fairly automatic in setting by calling a specifically built function.
bryansu
Forum Newbie
Posts: 9
Joined: Mon May 08, 2006 2:53 am

Post by bryansu »

The condition is this. Said i have

page01.php
page02.php
page03.php
page04.php and so on......


page01.php - login page
page02.php - check login name to databasse and do many other stuff....
page03.php - after page02, then page02 will redirect to page03.php.......


page01.php and page03.php can be access direct without causing problem. But page02.php cannot be access directly without going through page01.php......or it will cause problem....

So in order for page02.php to check whether it;s from page01.php, page02.php need to check previous page that access to page02.php....That is why i am using http_referer.

Currently i am using session, everything is fine. but i feel problem with the session, where i need to set at page01.php.....

Any way $_SERVER['HTTP_REFERER'] can detect previous page if the previous page is using header("location:").
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

HTTP_REFERER is very unreliable. Some browsers don't send it, some firewalls and routers filter it out, some companies strip it. That is why you need to rely on something you've built which is independent of the user requests.
bryansu
Forum Newbie
Posts: 9
Joined: Mon May 08, 2006 2:53 am

Post by bryansu »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have found something very interesting here, Said there is 4 page:

page_01.php
page_02.php
page_03.php
page_04.php

Below are the php file contents. Basically it's a test of :
page_01.php --> page_02.php --> page_03.php --> page_04.php

page_01.php
=========
[syntax="html"]<p>Page 1</p>
<p><a href="t2.php">Click Here</a></p>
page_02.php
=========[/syntax]

Code: Select all

if ($_SERVER['HTTP_REFERER'] == "http://localhost/t1.php"):
	header("location:t3.php");
else:
	echo "wrong";
endif;

page_03.php
=========

Code: Select all

header("location:t4.php");
if ($_SERVER['HTTP_REFERER'] == "http://localhost/t1.php"):
	header("location:t4.php");
else:
	echo "wrong";
endif;

page_04.php
=========

Code: Select all

echo $_SERVER['HTTP_REFERER'];


FINDING
======
If i want the sequence to be page01-->02-->03-->04, on 02 and 03 all i need to do is to check the http_referer equal to first page, which is page01.php.

The logic is here, if page is automatically going from 02 to 03 and 04, then all this file only need to check on page_01. Instead of 03 check on 02, and 04 check on 03...., all the 02,03,04 will check on 01 will do.

I have try to set highest security on the browser IE6 and using mozilla to test on the testing above, everything works fine...


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply