3

I want the below question to work for all major browser Opera, Internet Explorer, Chrome, Safari and Firefox

I want to create an application where the user has to follow an order of pages Text1.php, Text2.php, Text3.php in order to fully able to create an exam.

Problem is that at anytime the user can click on the back or forward browser buttons or access another page by entering in the page url.

So my question is this:

  1. If user clicks on back or forward browser buttons, it will display a prompt box or little window stating that:

    You are not suppose to leave this page when creating an assessment, if you want to leave the page then you will sent back to the "Menu" page and lose all you details for this exam. Click on this link below:

    Leave Assessment

    If you want to contine with creating the current assessment then please click on the link below:

    Continue Assessment

  2. If user is on either of Assessment creating pages Text1.php, Text2.php OR Text3.php and the user tries to change web address in url to access another page then the same window or prompt box or above will be displayed.

My question is that is this possible where if the box is shown when url or browser buttons are used and then be able to stay on current page if user clicks on Continue Assessment or navigate to menu.php if user clicks on Leave Assessment link. Can somebody provide a sample on how this can be achieved? I need it working in all the major browsers.

Thanks

Samuel Edwin Ward
  • 6,526
  • 3
  • 34
  • 62
user1941871
  • 139
  • 2
  • 9
  • "In all major" already means you should start using some `JS` library like Jquery – Yang Jan 07 '13 at 02:13
  • 1
    I don't think you can do this server-side only. Changing the address in the URL bar doesn't send anything to the server unless you add JS to do so. – Barmar Jan 08 '13 at 04:40
  • Just see [this](http://stackoverflow.com/questions/1119289/how-to-show-the-are-you-sure-you-want-to-navigate-away-from-this-page-when-ch) post. Minor modifications will lead you to what you want. – Alpay Jan 11 '13 at 07:41

5 Answers5

2

You can setup server-side cookies in a session (PHP can handle it well), e.g, an array with pages still avaiable to use and an array with pages alreadly seen.

Then, every time a page is loaded (you can put this on a include file), your script verifies (server-side) the last page user was in the assessment, if the user tries to get back, then your script inform: 'you cannot go back in assessment'.

With JavaScript you get user the warnings you need, before proceding after onclick events.

The deal is: - store user identification on session - attach an array with pages status or with a 'ponter' that indicates the only possible next page.. so: when page 1 loads, the pointer stores 'page2'string, and the initial verification never let the user load pages 1 again, or page 3 and so on. - thats all.

Sorry about english. Not american. ;)

reimorster
  • 31
  • 1
  • 5
0

Your best bet would be to use javascripts onbeforeunload event.

window.onbeforeunload = function() {

}
Dylan Cross
  • 5,918
  • 22
  • 77
  • 118
  • would that work in all browsers and also what happens if javascript is turned off? Unless it is best if I don't display contents of page if javascript turned off. – user1941871 Jan 07 '13 at 02:23
  • I'm actually not entirely sure about the browser support, I believe it should work in all recent major browsers, but I wouldn't rely on it. As far as I know there isn't really a reliable way to do this. – Dylan Cross Jan 07 '13 at 02:28
  • I know what you mean, it does seem tough, hence why I put a bounty on this question – user1941871 Jan 07 '13 at 02:49
  • @user1941871 If you want to handle the situations, like, when JavaScript isn't enabled, then you should make a redirect via `` section for each HTML document – Yang Jan 07 '13 at 02:56
  • Actually I was wondering if this is possible, can a url address bar be disable at an time, this is so user can only navigate pages using within page features ilke links and buttons for example? – user1941871 Jan 07 '13 at 03:07
  • `return 'You are about to leave the assessment';` <-- That's just meaningless. At least `alert()` should be used instead of `return` – Yang Jan 07 '13 at 03:12
  • @metal_fan A `beforeunload` handler doesn't need to use `alert()`. The browser automatically displays the return value as an alert. – Barmar Jan 08 '13 at 04:37
  • @Barmar my bad. I just checked that and kinda shocked about it. Thank you! – Yang Jan 08 '13 at 06:34
0

You can pop up a dialog with Ok and Cancel button using this-

window.onbeforeunload= 'Your text here';

You can't put the buttons(Leave assignment,etc), as browsers don't allow it for security reasons.

The onbeforeunload event isn't specified in w3 standards, so some browsers which are standard-strict don't support it. The well known example is Opera.

svineet
  • 1,859
  • 1
  • 17
  • 28
0

The most convenient browser-side solution seems to be the window.beforeunload one, and I would recommend using that in addition to what I am about to describe.

As part of the normal logic of your PHP pages, set a flag in the user's session that they have seen the current page. So in Page1.php you might set seen1, in Page2.php you would set seen2 and so forth.

Now for the important part. At the top of each of your scripts, before setting the flag or sending anything back to the user, check if the flags in the session match what they should be. So, for instance, on Page1.php you would expect that none of the flags have been set, on Page2.php you expect that seen1 is set but not seen2 or seen3.

If the flags don't match, you can use the flags to determine which page they should be on and redirect them there instead of displaying the page. If you like you can set a session variable to alert them that this has happened (clear it after displaying the alert).

Note that in order for this to work you will have to send the necessary headers and such to prevent the browser or anything else from caching the page.

Samuel Edwin Ward
  • 6,526
  • 3
  • 34
  • 62
0

Why not set a session variable

$_SESSION['on-page']="page1.php";
if($_SERVER['PHP_SELF']!="page1.php"){ die('Please go back and finish the previous page.'); ?>

Something like that?

Hydra IO
  • 1,537
  • 1
  • 13
  • 28