PHP - How determine when Browser back button is used?

Status
Not open for further replies.

bobsoap

Together we can do anyone
Sep 16, 2008
605
16
0
soapdesigned.com
$_SERVER['HTTP_REFERER'] won't work here, as the page is not being reloaded - how is it possible to see when a user hit the back button, and take conditional action (e.g. redirect to a page earlier)? Maybe with JS?
 


in short, there is no definitive technique for this, you just have to make your application get along with it
 
$_SERVER['HTTP_REFERER'] won't work here, as the page is not being reloaded - how is it possible to see when a user hit the back button, and take conditional action (e.g. redirect to a page earlier)? Maybe with JS?

Javascript is likely going to be your only answer here to detect when a browser window is being closed or being left (it won't know you hit back per se, just that it's leaving the current page).

Remember that PHP is strictly server-sided so it can't monitor the client after processing and delivering the results. Javascript can however if the user allows it to.

I guess it depends on what you are doing. If the user is hitting back to another page on your own site you may be able to utilize sessions to see if a user had already been to that page once. But if the back button takes them off the site, there's nothing you can really do. Also most browsers now days allow users to prevent javascript from 'taking over' in a sense and changing the location back.

One method I can think of is using the prototype javascript framework, and create an event observer to observe the window being closed or location getting ready to be changed. And then injunct a check of the destination, and warn the user of leaving the site. And then have your 'correct' links change page via javascript (but still have a valid anchor underneath so that the page still works in the event javascript doesn't work)
 
  • Like
Reactions: Aequitas
Javascript is likely going to be your only answer here to detect when a browser window is being closed or being left (it won't know you hit back per se, just that it's leaving the current page).

Remember that PHP is strictly server-sided so it can't monitor the client after processing and delivering the results. Javascript can however if the user allows it to.

I guess it depends on what you are doing. If the user is hitting back to another page on your own site you may be able to utilize sessions to see if a user had already been to that page once. But if the back button takes them off the site, there's nothing you can really do. Also most browsers now days allow users to prevent javascript from 'taking over' in a sense and changing the location back.

One method I can think of is using the prototype javascript framework, and create an event observer to observe the window being closed or location getting ready to be changed. And then injunct a check of the destination, and warn the user of leaving the site. And then have your 'correct' links change page via javascript (but still have a valid anchor underneath so that the page still works in the event javascript doesn't work)

The user would remain on the domain, it wouldn't take them off site. It's basically an intermediate "confirmation page" that is forwarding to the next page after a second (and writing an input to the db). I don't want this page to come up again if user clicks back, but I want them to "jump" that page and go back to the first one.

I'll have to see how I manage to do that. I think the check still has to take place on this intermediate page, and because it is not producing a server request when user clicks back (as you point out), I guess you're right and JS is my only choice.

What I thought about was placing a cookie on the last page (the one after the confirmation page) and checking for that on the intermediate page, and when found, do a meta refresh to the first page. I'll have to try that later.

Thanks for your input, much appreciated.
 
I think I see what you're doing here Bob - you trying to make it so that if the user goes from the confirm page and goes back to the previous page it displays a different page (like a, discount page or something to push the conversion)?

In which case, I think you're right - the best way I can see to get that going is to have the confirm page drop a cookie. The cookie can be removed on the page that confirms the sale and the previous page can check to see if the cookie exists, if so you can have header ('discount.php') or whichevers. I've not tried it but I can't see why that wouldn't work.
 
Yes, something like that :) I just don't want user to be sitting on the confirmation page which serves no other purpose than displaying a confirmation.

And yes, it works this way - just that everything has to happen in JavaScript on the confirmation page. I included a simple window.location if the cookie is set, and it works just fine now.

Now I just have to decide if I want to go through the hassle of including page-specific header functions in header.php (so that the cookie is only placed on the 3rd page), or if I place the cookie with JavaScript in the first place.

I usually tend to prefer using PHP, but that will have to happen before headers are sent. It's an age-old issue anyway :)

Thanks Dave!
 
Status
Not open for further replies.