Farming - Northern Rock - Gourmet Chocolate Assortments - Mobile Phones - Loans

PDA

View Full Version : How do I find the URL of the previous page?


Weirfire
Oct 19th 2004, 2:32 am
I was wondering if there was a way of extracting the page that was last viewed with PHP? It's only for pages where someone has clicked through to the page.

Weirfire
Oct 19th 2004, 3:19 am
I found it.

It's $_SERVER['HTTP_REFERRER'] just in case anyone was curious. :)

xml
Oct 19th 2004, 3:20 am
<?php
$ref = getenv("HTTP_REFERER") ;
?>

Weirfire
Oct 19th 2004, 3:28 am
Is there any advantage of using getenv than $_SERVER. They seem to do the same thing.

xml
Oct 19th 2004, 4:20 am
Hmm not that i know of :).

Weirfire
Oct 20th 2004, 2:30 am
lol

Cheers :)

T0PS3O
Oct 20th 2004, 2:56 am
I believe $_SERVER is how they want you to do it. getenv won't be supported for much longer. It has to do with global settings etc. Used to be a security risk but with $_SERVER $_GET etc. is fixed apparently. (Disclaimer: all off the top of my head from research some months back. Do not take this for fact and rely on it.)

Weirfire
Oct 20th 2004, 8:41 am
I see. I'll use $_SERVER then :)

Lever
Feb 5th 2005, 8:08 am
So how can this be executed from within the code of the page? I've got a page that processes a form and I want to enable it for the user to be able to click "go back" when a form field is missing. This is in my phpif(empty($email)) {
echo "<p>You didn't enter an email address, please go back and try again</p>";
} I'd like the go back text to be the link to the referring page (the form)

Any help very much appreciated :)

sadcox66
Mar 2nd 2005, 7:13 pm
if(empty($email)) {
echo "<p>You didn't enter an email address, "."<a href=".$_SERVER['HTTP_REFERRER'].">please go back</a> and try again</p>";
}

Lever
Mar 3rd 2005, 3:01 am
Cheers dude, I dropped an R from REFERRER and it worked fine :) Now I just gotta figure out how to keep the variables intact on the previous form page so I've used <a href='javascript:history.go(-1)'>please go back</a> to keep it working for now ;)

walliioo
Jun 7th 2005, 8:01 am
There is a big problem using HTTP referer with IE ... this function simply do not send the URL ... it works with Mozilla but not with IE ...

So if someone have an other idea ... exept using a form .... it will be fun !!

Thanks :D

Weirfire
Jun 7th 2005, 9:47 am
Welcome to the forums walliioooioiooooo :)

The HTTP Referrer works for me with IE. I don't really know how it couldn't work with 1 browser and not another due to the fact it is a PHP function which is executed on the server.

My feelings are, it's your server that is at fault if you have your code laid out nicely.

walliioo
Jun 8th 2005, 1:49 am
Yep i did not think about this... may be a security on the server or on the IE ???

Please help :confused: :confused:
lol

exam
Jun 12th 2005, 6:11 pm
I found it.
It's $_SERVER['HTTP_REFERRER'] just in case anyone was curious. :)Two comments:
- As already mentioned, it's $_SERVER['HTTP_REFERER'] (It is spelled wrong)
- This variable is set by the client software and can be manipulated, don't rely on it for anything important.Now I just gotta figure out how to keep the variables intact on the previous form pageYou can put them in a $_SESSION variable.

Weirfire
Jun 12th 2005, 6:19 pm
Two comments:
- As already mentioned, it's $_SERVER['HTTP_REFERER'] (It is spelled wrong)


Sorry exam :o

exam
Jun 12th 2005, 7:25 pm
No apologies necessary Weirfire, I was only clarifying for archival purposes as I remember all to well when I was starting out and couldn't get it to work until I misspelled it. No worries man :)

Edit: Oh and I didn't mean you misspelled it, referrer is misspelled as referer in php and in the HTTP protocol.

Weirfire
Jun 13th 2005, 2:03 am
No apologies necessary Weirfire, I was only clarifying for archival purposes as I remember all to well when I was starting out and couldn't get it to work until I misspelled it. No worries man :)

Edit: Oh and I didn't mean you misspelled it, referrer is misspelled as referer in php and in the HTTP protocol.

I see :)

If you use phpinfo(); you get to see all the global variables. :)

nevetS
Jun 13th 2005, 3:52 am
There are sometimes browser objects, toolbars, etc. that block the referrer. It's sent as part of the http request from the client.

Another way to do things is to set a cookie with every page visit to the location of the page, but that's blockable too.

cornelius
Jun 20th 2005, 4:33 pm
isnt refferer unreliable? and cookies be blocked??

use sessions its easy

i would do something like

--------------------------------------------------------------------
page_1.php
--------------------------------------------------------------------
<?php
session_start();

//some code

$page_name = $_SERVER[PHP_SELF];

$_SESSION['page_name'] = $page_name;

//some code
?>
--------------------------------------------------------------------


--------------------------------------------------------------------
page_2.php
--------------------------------------------------------------------
<?php
session_start();

//some code

$previous_page = $_SESSION['page_name'];

//some code
?>
--------------------------------------------------------------------

Weirfire
Jun 20th 2005, 4:38 pm
isnt refferer unreliable? and cookies be blocked??

use sessions its easy

i would do something like

--------------------------------------------------------------------
page_1.php
--------------------------------------------------------------------
<?php
session_start();

//some code

$page_name = $_SERVER[PHP_SELF];

$_SESSION['page_name'] = $page_name;

//some code
?>
--------------------------------------------------------------------


--------------------------------------------------------------------
page_2.php
--------------------------------------------------------------------
<?php
session_start();

//some code

$previous_page = $_SESSION['page_name'];

//some code
?>
--------------------------------------------------------------------

I've never actually used sessions before but does it not do the same thing as a cookie?

exam
Jun 20th 2005, 7:37 pm
I've never actually used sessions before but does it not do the same thing as a cookie?Yes. Php session handling can save the info on the server, but it still uses regular cookies to identify the user. :)

cornelius
Jun 21st 2005, 1:26 pm
it is but if the user has cookies disabled, sessions write the cookies into the url

exam
Jun 22nd 2005, 1:44 pm
it is but if the user has cookies disabled, sessions write the cookies into the urlYou are absolutely right, but anyone in their right mind wouldn't use sessions in the URL (think SEO) unless it was in a password-protected part of the site or you got rid of them for the bots.