Help - Search - Members - Calendar
Full Version: Return previous page with PHP
HTMLHelp Forums > Programming > Server-side Scripting
dougmanic
Is there a possible way to store the user's previous page history in a variable using PHP?

I tried $_SERVER['HTTP_REFERER'], but nothing shows up.
Brian Chandler
QUOTE(dougmanic @ Nov 14 2006, 09:16 AM) *

Is there a possible way to store the user's previous page history in a variable using PHP?

I tried $_SERVER['HTTP_REFERER'], but nothing shows up.


Simply, and strictly, no there isn't. The php program is running on a server; the "history" is a property of a browser that has issued an HTTP request to the server - there is no way for the php program to "know" anything about the browser except what was contained in the HTTP request.

So yes, there may be a 'refer®er' header, but you can't rely on that.

But what are you _actually_ trying to do?
Brian Chandler
QUOTE(Brian Chandler @ Nov 14 2006, 01:15 PM) *


....

So yes, there may be a 'referŽer' header, but you can't rely on that.

....



Admins: Pleeeease switch this idiotic nonsense off! Please give us a board like the old htmlhelp board where the software was written by intelligent people with the intent of being useful, instead of by total morons.


Peter1968
QUOTE(dougmanic @ Nov 14 2006, 11:16 AM) *

Is there a possible way to store the user's previous page history in a variable using PHP?

I tried $_SERVER['HTTP_REFERER'], but nothing shows up.


In short, PHP is server-side, the browser's history is client-side, and never the twain shall meet. PHP, as a server function, doesn't have a clue what the user's browser is about. Makes sense when you give it some thought, doesn't it?

Peter1968
QUOTE(Brian Chandler @ Nov 14 2006, 03:21 PM) *


Admins: Pleeeease switch this idiotic nonsense off! Please give us a board like the old htmlhelp board where the software was written by intelligent people with the intent of being useful, instead of by total morons.


Referer
Referrer

*shrug* What are exactly you typing in? What idiotic nonsense?
Brian Chandler
QUOTE(Peter1968 @ Nov 14 2006, 04:25 PM) *

QUOTE(Brian Chandler @ Nov 14 2006, 03:21 PM) *


Admins: Pleeeease switch this idiotic nonsense off! Please give us a board like the old htmlhelp board where the software was written by intelligent people with the intent of being useful, instead of by total morons.


Referer
Referrer

*shrug* What are exactly you typing in? What idiotic nonsense?


The sort of moronic nonsense you get with "Office" software, until you find out how to switch it all off, that interferes with what you type. You type 'fish' and the software thinks you must have meant to type 'bacon', that sort of thing. In this case I wrote arr-ee-eff-ee-leftparenthesis-r-rightparenthesis-r-e-r, which is what I meant to write, because I am more intelligent than this moronic software, but the software changed it as above. (The point is that "referrer" is misspelled in the HTTP specification.)

I suppose the heroic thing to do would be to write an intermediating page that allows you to type, and works out what needs to be submitted to the moron software to get it displayed without being messed up.
Haven't time just at the moment.
Peter1968
referŽer

Edit: I see what you're saying. To be honest, I don't recall a setting inside of IPB that'll disable such "smart tagging". Mind you, I used an earlier version, when it was still free.
Curtis
Lol, that seems like a waste of programming, imo. It should just let people type the hex entities for stuff like that.

Also, as for the server side never getting a hold of the history, that will always be so because of security/privacy issues. However, you can get pretty close when asynchronous scripting (ajax).
El-Aurian
Sorry, for resurrecting an old thread.

But i was hoping to use the variable to detect whether someone has accessed a php page, using a page on my website. to try and prevent hackers from accessing the scripts.

Is $_SERVER['HTTP_REFERER'], the best option for this?

Thanks,

Matthew Millar
Curtis
That's the wrong way to go about security. What are you really trying to accomplish?

In any case, if you read this thread, you should see that such information from the client is unreliable, at best.
El-Aurian
I'm hoping to detect the previous page, so i can tailor what is displayed by the php, dependent on which page the user came from.

Security isn't a huge issue, i was just thinking ahead as to how i could secure it.

I'm aware that i can just add an extra hidden input into the form, but i was hoping to avoid dirtying the referrer pages, if there was a way of detecting the previous page using php.

Thanks,

Matthew Millar
Sean - Bedroom Coder
QUOTE(El-Aurian @ Sep 16 2009, 07:36 PM) *

I'm hoping to detect the previous page, so i can tailor what is displayed by the php, dependent on which page the user came from.

Security isn't a huge issue, i was just thinking ahead as to how i could secure it.

I'm aware that i can just add an extra hidden input into the form, but i was hoping to avoid dirtying the referrer pages, if there was a way of detecting the previous page using php.

Thanks,

Matthew Millar


Im trying to find a better way of doing this just to provide a link to the previous page, but currently using this method below

I am using a link

<a href="<?=$_SESSION['previous_page']?>">Previous Page</a>

and then this called in at the bottom of the page after the link
$_SESSION['previous_page']=htmlentities($_SERVER['REQUEST_URI']);

To then decide what was to be displayed on the page you could set up a switch, you could also use $_SERVER['QUERY_STRING'] and pass through a query on the forms action url,

Of course its not fool proof and the very reason I am now trawling the net trying to find a better way, but it sets a session variable of the last page viewed from any page you set the session.
Jim8974
Hi,

I am trying to create a similar process to automatically create the previous page, you can take this method as far as you want really and the limit of the number of back pages you can go to is only set by how much work you want to add to it.

for this example I will only use the previous page.

okay firstly you need to create two session variables.

Current page

Previous page.

give current page the value of the current page, and test to see if previous page has been set, if it hasnt then it means that its the first visit.

if it is the first visit give previous page a default value.

now when the user changes page the order of this is important as it defines the page links.

take the Current Page Session value and give this to 'Previous Page' and change Current Page Session Value to the ... welll current page name.

You now have two values held in session.

the value of the current page, and the value of the previous page. you can use these in links to provide returns to any drill downs that you have done, (that is of course if you extend it to 2nd previous page 3rd previous page etc.

However there are a LOT of limits to this.

Firstly it will only work on your own site/code as you will only be able to the code withint your own site.


Secondly, its limited past a two page history as once you have lost the 3rd lastr page you can end up going round in circles

(e.g open page 1 go to page 2, go back using script to page one, the previous page is now page 2, if you use the link again, the previous page is now page 1 etc etc etc.)



hope this helps you or can give you a rough idea of a better script.
geilt
Thanks very much for this helpful description. It helped me create the following. I am making an admin nav in Wordpress and the following solution worked for me.

In order to overcome this problem I did the following:

CODE

    <?php
    session_start();
    if($_REQUEST['page'] != $_SESSION['oldPage'])
        $_SESSION['oldURL'] = $_SESSION['newURL'];

    $_SESSION['oldPage']     = $_REQUEST['page'];
    $_SESSION['newURL']     = $_SERVER['REQUEST_URI'];    
    ?>

So you see I am checking to make sure that the current page does not equal the old page (in the case of a page refreshing itself to update an option for example).

If its not equal then we change the oldURL to the newURL so that it stores the proper last page. We do not want o ochange the oldURL if we are on the same page.

Then we set a session variable called oldPage to the query value of "page" to check on the next page. We finalyl set the current full page to newURL so that we can pass it to old URL on the next page (if its a different page).

A valid example of a link going back to previous would be

CODE

    <?php
    $lastLink    = "<a href='" . $_SESSION['oldURL'] . "'>Back to Previous Page</a>";
    ?>


It works, and it works well, though thinking about it still confuses the hell out of me!
Ephraim F. Moya
It seems to me that you guys are on a fools errand.

None of the 'solutions' you have described include the current user. I assume that your aim is to keep records about each individual user. None of the posts even mentions that.

So I think what you'll end up with is some kind of random path for a random number of visitors.

The big boys keep a record of each individual page an individual visitor sees in a whole other database table by guest id number.

Since most modern browsers throw away cookies whenever the browser is closed the methods described fail overnight.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2024 Invision Power Services, Inc.