Check if user hits back button in browser - php

I'm trying to use the javascript onbeforeunload event to ask the user if they want to exit the page, but I don't want the event to fire (EDIT: "the event" being the dialog box that pops up asking the user to click ok to leave the site or click cancel to stay on the current page) if the user hits the back button since they will be most likely be staying on my site.
So is there a way to tell if a user has hit the back button using javascript or PHP?
I've gotten a solution using a hidden iframe that only works in IE, but I need something that can work for Firefox, Chrome, and Safari if possible.
EDIT: My IE solution works because when the user hits the back button the iframe is sent back but the parent page remains at the same spot. From this I can tell that the user has indeed hit the back button, so I then use history.back(). This little hack doesn't work in any other browser (to my knowledge), so I'm looking for a cross-browser solution.
tl;dr I'm using window.onbeforeunload to pop up a dialog asking users if they want to leave my site or not. I don't want this to pop up when the user hits the back button. How can I tell that the user has hit the back button in their browser?
Thanks,
Rick

Short answer:
No.
Long answer:
Noooooooooooooooooooooooooooo.
please don't try to keep users on your website unless you have a very good reason to. Saving form fields would be an example of a good use. Checking if they're moving on to another website would be a bad use.
People don't travel from page-to-page as much as they did in the early days of the web. Instead they use google and social networks to find interesting pages, and consume separate distinct pieces of information.

You can't know in advance on which page your user will go when he leaves your page. You can't even get the URLs in its current history.
I see no solution to your problem and I doubt there's one, sorry.

If you don't want anything to happen when the user clicks the back button, then you don't necessarily need to determine if the back button has been hit.
Your goal is to determine who will "most likely be staying on [your] site," and create an extra step for everyone who wants to leave. You're trying to interrupt and override the user's expectations of how his browser will behave.
If you really want to do this, have event listeners for all unload events that aren't triggered by the back button: every link on your page, closing the window, etc. It won't be easy, and you won't be able to catch all events. But you're going to be pissing people off unless you have a good reason for doing this, so if it's really important then put the extra effort in.
tl;dr: Add event listeners to everything that isn't the back button and bring up the dialog in the callback function. It will piss people off, though.

Related

How can I prevent the user to go back to my shop checkout with the "back" button of the browser? [duplicate]

I am working on a my site to allow users to take test so they can see how much they know of a particular subject. I am running into a little problem though. Once a user submits the test for grading, how do I prevent them from going back to the test page? I am on a Mac with Safari running and when I click the back button in my web browser after I submit the test it leaves all of the answers I answered filled out. I want it do this: When a user submits a test and they click the back button in their web browser it redirects them to the main test page.
I am using PHP and MYSQL. I even have the test pages setup so that the user must come from a certain url (I am using HTTP_REFERER) and I have tried other stuff such as sessions but I cannot seem to figure this out. Any help is greatly appreciated.
You don't stop them.
Instead change your application so that it still works even if they go back. You can embed a unique number in a hidden field on the page and if they resubmit the same test twice you can detect it and display an appropriate error message. You should also think about what should happen if they modify the unique number.
If you don't want people to post different answers once they have already answered, all you have to do is check, in the script that accepts the test for grading, that the user has never submitted the test before. If you don't, a clever student will always be able to to circumvent your protection by sending an appropriate request directly to that script.
If you don't want people to see previous answers (for instance, if you have two people grade their tests on the same computer), consider using AJAX on the test page to submit the answers and then erase them from the fields. This way, most browsers will not remember the answers and the back button will not un-erase data that was erased by JavaScript.
At the top of the grade page, put the following:
session_start();
$_SESSION['testcomplete'] = 'yes';
Then at the top of each page of the test, put this:
session_start()
if ($_SESSION['testcomplete'] == 'yes') {
header("Location:cheater.php");
}
You could simulate there being no page to go back to. From one page, generate each test page using jQuery, and provide no way to go back, only forward. The back button would take them to the page before they ever launched the test, and you could allow them to launch the test again and generate the right part where they should be. This would be pretty easy, if you haven't gone too far in development the current way.
You could run javascript that clears out all the answers. You might also just allow one submission so that subsequent submissions don't get processed. HTTP_REFERER is usually sent, but can be spoofed and forged by an altered browser.
On the top of the script POST-ing the answers, do a check whether you have the test results in the database for the current user for this test. If you do, redirect to results.
if(get_test_results($user)){
$test_url = get_test_url($user);
header( "Location: $test_url" ) ;
}
Disabling the back button is not a good idea.
I was facing a similar problem making an online examination myself
what I did is
I provided a session variable such that if the user pastes the previous page's URL in the address bar then on loading the page the page is automatically forwards to the next desired page. Whether the page whose URL was mentioned is the being visited the first time or being revisited is determined by the value of the session variable
If the user instead of loading the page does a go back via the browser button the it automatically redirects to the next page in history as :
javascript:window.history.forward(1);
Hope this helps :)
http://www.htmlgoodies.com/tutorials/buttons/article.php/3478911/Disabling-the-Back-Button.htm you should be able to do it in javascript.

How to disable back and refresh button in browser using php

Want to disable back and refresh button for a website in php using codeigniter.
whenever the person click on browser back button it should not be able to go on the page he visited before.
and does not reload that page once loaded.
hey its not for static page its for dynamic page . attending question on online test. please understand that
There is no way to do that. Except you may show an alert to user, when he tries to close window.
About the alert:
Warn user before leaving web page with unsaved changes
That's imho not possible, and it's good so :) If you want, that your users don't use the back key, write this down as a warning message or something else :)
Check this out Iam not sure this is your actual issue but maybe it will help you out.
Preventing Double submit on refresh/back

How to display message if user clicks on browser button?

My question is it possible to display a message if browser buttons have been pressed in order to be able to display a message stating if user should leave the page or stay on a page? Also can a message be displayed if user tries to change a url if they are on a certain page?
Thanks
Server side is needed for foolproofing this. On the server, when assessment is started, you should first have start screen URL. You must program it so that once assessment is started (after clicking start), it goes to the exam URL, and your assessment is always saved - when user goes back, using server side tricks have it so that instead of showing the start screen, it redirects back to the correct page (in a way that still maintains history), shows the exam with the data still in it, and pops up the warning not to click back button. Then, if user does it again, it does the same thing, again.
You have to have it so the exam is saved all the time. unload javascript event can be used to notify via alert() that they shouldn't be leaving the exam, and thats about all. onbeforeunload event can be used to try to give them the choice to stay, but shouldn't be depended on as it doesn't work on every browser.
Use onbeforeunload as suggested in a comment. There are various example, like this one: https://web.archive.org/web/20211028110528/http://www.4guysfromrolla.com/demos/OnBeforeUnloadDemo1.htm
If you want to call out the buttons that the user must click, you will need to do some browser detection, as most browsers have OK/Cancel buttons on the window that they display, but Chrome has "Leave this page" and "Stay on this page".

Which solution is best when the browser back button is hit

I have an application and the problem I have is with the back brower buttons, if the user cliks on a back browser, then it would mess things up in the application.
So I want to do either 2 things:
Solution 1: I have found a site where it shows the javascript code for disabling back buttons on all browsers. Obviously you need javascript enabled for this to work but my application won't work very well without javascript anyway so this won't be a problem (I include a warning in each page in my application stating javascript must be enabled in order to use the app)
Solution 2: IF the user does click on the back button at any time, then it will navigate the user to the safety.php page where on this page it will inform the user that they can't use the browser back button, then it will destroy all of the sessions so that the user is logged out. If they want to use the app again then they will have to login again and use the app from the beginning.
My question is that out of both solutions, which one would be better to use? My application is where a teacher creates an online assessment, creating the assessments detials (start time/ duration date etc) and creating the questions and answers for each assesment (could be single or multiple assessments)
Disabling the back button is generally poor usability practice. Your best bet is to manipulate the history object (search pushState if you're not familiar with these methods) so that hitting the back button would return them to a page outside your experience, or however you're accomplishing "Solution 2", but warn them that they are leaving the "logged in" section before they actually leave the page, so that they can choose to stay, using confirm() or a custom pop-up.
use javascript in the first place to prevent adding the location to the history list.
catch the onclick event of the proper link, and use document.location.replace("next_page.html");
so this way, when a user clicks the back button he would leave your site, rather then going back and messing up your app.
also: you can catch the unload event of the page where the user presses back button, so that you can confirm the user for exiting.

How to test facebook "like" button?

Working on integrating the "like" button into a site.
The site is currently running on XAMPP in localhost mode. Not visible anywhere outside this computer.
Upon the site user clicking the button I need to log the event into the database. I have their information from a form they filled-out prior to even seeing the button.
Here's my problem. I am trying to debug the code that does a lot of this. However, if I click on the "like" button just one time it is forever grayed-out after that.
I can clear cookies on Firefox and get the button back. This also means that I have to login to FB again. This makes for a very difficult debugging session. Surely there's a better way?
Here's my problem. I am trying to debug the code that does a lot of this. However, if I click on the "like" button just one time it is forever grayed-out after that.
If you hover over the greyed-out version, you should see an X to remove your like.

Categories