/    Sign up×
Community /Pin to ProfileBookmark

PHP, open new window, to prevent BACK or REFRESH

I’ve been battling with this for a while now.

Could really use some help here.

I’m designing a quiz program, which is set on a timer.

Once the user clicks “Start Quiz”, he is re-directed to a new page, where the quiz begins.

He is presented with one question at a time. (10 seconds allocated to each question)

There are sooooooo many ways one could cheat, which is why I decided to implement the following “barriers” :

(a) a Javascript function which disables the “[B]Back[/B]” feature. If the user presses the “[B]Back[/B]“-button on their browser, he gets a blank page, with an error message

(b) a Javascript function which disables “[B]Copy/Paste”[/B]

(c) a Javascript function which disables [B]“Right-Click[/B]” of the mouse

All these 3 functions seem to work. But, there are two issues which I am still unable to resolve

(a) trying to open the quiz-page in a NEW window, where “[B]toolbar[/B]” has been removed. Thus, preventing the user from using the toolbar to go back;

Here is the script I used, but it works only ONCE. Meaning, it disables the toolbar for only the next page (onClick). How can I do this for ALL subsequent pages, after the first one ??

[CODE]<script type=”text/javascript”>
function myPopup() {
window.open(“/Quiz/question_1.php”, “status = 1, height = 900, width = 900, resizable =
0, toolbar=no”)
}
</script>[/CODE]

(b) i know it is impossible to disable the [B]“Refresh”-button (F5)[/B] in PHP. But, I need to somehow prevent the user from refreshing the page. Because, as I said earlier, my quiz is on a timer. Once the quiz begins, the timer begins to count-down (10 seconds……..9……….8…………7……….6). But, if the user presses “F5″………….well, he refreshes the page, and the timer returns to “10” again.

From what I was able to get from Google, there is a Javascript function to disable the “F5” button, but it only works with Internet Explorer. It does not work with other browsers.

The best solution would probably be : somehow, assign each question a session “ID”; when the user tries to refresh the same page, he gets an error, because that “ID” has already been “initiated” (not sure if this makes sense”)

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 12.2015 — How about tracking on the server side? Track when the question was output to the user, and if the response comes in more than X seconds later, let them know it was too late and reject it (maybe make it 12 or even 15 seconds to account for network lag?
Copy linkTweet thisAlerts:
@newuserphpauthorMar 12.2015 — How about tracking on the server side? Track when the question was output to the user, and if the response comes in more than X seconds later, let them know it was too late and reject it (maybe make it 12 or even 15 seconds to account for network lag?[/QUOTE]


Please, could you elaborate? I'm rather new to this. How would I do this on the server side?
Copy linkTweet thisAlerts:
@NogDogMar 13.2015 — You could simply store the time in a session variable when you display the question page, then compare that to the current time when you process the submitted answer. You'd probably want to track the question number (or however you identify it) so that you can not allow them to refresh it by making a new request to the question page. For instance, for question #1:
[code=php]
<?php
session_start();
if(!isset($_SESSION['qTime'][1])) {
$_SESSION['qTime'][1] = time();
}
[/code]

On the page that processes question #1:
[code=php]
<?php
session_start();
if(!isset($_SESSION['qTime'][1])) {
// they shouldn't be here, so some sort of error handling here
}
elseif(time() - $_SESSION['qTime'][1] > 15) { // or however many seconds you want
// let them know they took too long
}
else {
// we're good to go to process their answer here
}
[/code]
Copy linkTweet thisAlerts:
@newuserphpauthorMar 13.2015 — You could simply store the time in a session variable when you display the question page, then compare that to the current time when you process the submitted answer. You'd probably want to track the question number (or however you identify it) so that you can not allow them to refresh it by making a new request to the question page. For instance, for question #1:
[code=php]
<?php
session_start();
if(!isset($_SESSION['qTime'][1])) {
$_SESSION['qTime'][1] = time();
}
[/code]

On the page that processes question #1:
[code=php]
<?php
session_start();
if(!isset($_SESSION['qTime'][1])) {
// they shouldn't be here, so some sort of error handling here
}
elseif(time() - $_SESSION['qTime'][1] > 15) { // or however many seconds you want
// let them know they took too long
}
else {
// we're good to go to process their answer here
}
[/code]
[/QUOTE]



Muchas gracias, NogDog ?
Copy linkTweet thisAlerts:
@newuserphpauthorMar 15.2015 — Hmm..............can't seem to get it to work ?

Here is my Timer_countdown function :

<script>

var seconds = 10;

setInterval(

function(){

if (seconds <= 1) {

window.location = 'http://localhost/PHP/outoftime.php';
}
else {
document.getElementById('seconds').innerHTML = --seconds;
}
},
1000
);
</script>

[/QUOTE]


I cant seem to store it properly in a session ?

Each question is on a different PHP file ([B]1.php, 2.php, 3.php[/B])

Upon answering each question, the user clicks [B]NEXT[/B], and moves on to the next question

If the user runs out of time (10 seconds), he is re-directed to the "[B]outoftime.php[/B]" file.
Copy linkTweet thisAlerts:
@rootMar 15.2015 — Use a session to track the page that the person is on and anyone wanting to go back should not be prohibited, just barred from changing the responses.

counting time... You need a time reference, say you will end in 20 seconds, you will need to know the starting time reference, add 20 seconds to that to get the finishing time. Something like this might do what you need.

start = new Date().getTime(); // get time reference in milliseconds
end = start + 10000; // add 10 seconds (10,000 milliseconds) to get end reference
// set an update
update = setInterval("updateDisplay() ",999)

function updateDisplay(){
var t=new Date().getTime();
timeleft = Math.floor( (end - t)/1000 );
if( timeleft&gt;0 ){
document.getElementById('seconds').innerHTML = timeleft;
}else{
clearInterval( update );
location.replace('http://localhost/PHP/outoftime.php');
}
}
Copy linkTweet thisAlerts:
@newuserphpauthorMar 15.2015 — It's not working. Maybe I'm doing something wrong?


<script>

var seconds = 10;

start = new Date().getTime(); // get time reference in milliseconds

end = start + 10000; // add 10 seconds (10,000 milliseconds) to get end reference

// set an update

update = setInterval("updateDisplay() ",999)

function updateDisplay(){
var t=new Date().getTime();
timeleft = Math.floor( (end - t)/1000 );
if( timeleft>0 ){
document.getElementById('seconds').innerHTML = timeleft;
}else{
clearInterval( update );
location.replace('http://localhost/GETAQU/PHP/outoftime.php');
}
}
</script>

[/QUOTE]


The idea is to prevent the user from using the REFRESH (F5) button.

Because, this caused the page to RELOAD.........hence setting the timer back to the initial "10 seconds"

I already took care of the BACK-button issue, by simply opening the quiz in a new window, without a toolbar.

But, I need to erase the "Refresh" function.

Which is why I need a session, which stores the time when the page is opened, and closes it after 10 seconds, regardless of what the user does
Copy linkTweet thisAlerts:
@rootMar 15.2015 — Use localStorage as a means to storing a time reference or use a cookie set on page refresh that is then found and the timer start from that time reference. The localStorage option could be useful for keeping a reference of what questions have been answered.

I didn't say the script would work, I presented it as an idea as your timing needs were up to that point fairly simple, however it is a bit more complex than just a count down routine.
Copy linkTweet thisAlerts:
@jedaisoulMar 15.2015 — @ newuserphp

Rule no. 1 Do NOT disable standard functionality like the back and refresh buttons. It is one sure way to annoy people. Instead, as . has said, [i]prevent them from changing the input, or resetting the time[/i].

Rule no. 2 See rule no. 1. ?
Copy linkTweet thisAlerts:
@newuserphpauthorMar 15.2015 — @ newuserphp

Rule no. 1 Do NOT disable standard functionality like the back and refresh buttons. It is one sure way to annoy people. Instead, as . has said, [i]prevent them from changing the input, or resetting the time[/i].

Rule no. 2 See rule no. 1. ?[/QUOTE]



As I clearly stated : I already took care of the BACK-button issue

My problem now is how to prevent users from resetting the time
Copy linkTweet thisAlerts:
@rootMar 17.2015 — Did you read my comment?

Store in local storage a time reference of the end time, if current time exceeds that, you know time is up. Very simple way of making a variable value persistant, your fall back position will be cookies.
×

Success!

Help @newuserphp spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...