Jump to content

is this impossible?


floatermule

Recommended Posts

ok this is a little problem i've been having with a trivia game script i've been working on...

 

here's the scenario..

 

the user clicks "start a new game", which loads a page that creates a row in a MySQL database with a Unique ID for the user as well as sets the cookie for the UID, that page instantly redirects to a 3rd page where you select the category for the trivia question as well as select your wager for the upcoming question.

 

after the user had made their selections they are sent to a page that selects a question from the database, while another query updates the users history to make sure the question selected is not asked again, this page auto redirects you to the question page... here's where i'm having my problem...

 

i have a counter script (javascript) on the question page that counts down 30 seconds and then auto submits the form... the counter is displayed in a text field.

 

here's the code for the counter:

 

<form name="counter">
	<input type="text" size="8" readonly name="d2" class=count></form> 
	<script> 
	<!-- 
	// 
	 var milisec=0 
	 var seconds=30
	 document.counter.d2.value='30' 


	function display(){ 

	if(seconds == 0 && milisec == 0){
	document.forms.form1.submit()
	}


	 if (milisec<=0){ 
		milisec=9 
		seconds-=1 
	 } 
	 if (seconds<=-1){ 
		milisec=0 
		seconds+=1 
	 } 
	 else 
		milisec-=1 
		document.counter.d2.value=seconds+"."+milisec 
		setTimeout("display()",100) 
	   
	} 

	display() 
	--> 
	</script> 

 

 

Now my problem is that the user can simply refresh the page to reset the counter and give them more time to answer... i would like to figure out a way to stop people from being able to reset that counter by refreshing or using the back button...

 

any idea??

 

p.s. i've thought about going all Flash so that a refresh would totally start the game over... but flash frames stop when you right-click or click and hold anywhere on the page... so that wouldn't work either...

Link to comment
https://forums.phpfreaks.com/topic/102756-is-this-impossible/
Share on other sites

what if you set the time they originally started as a session variable and then base the number of seconds they get in the javascript code off of the time the page first loaded.

 

<?php 
session_start();

if(!isset($_SESSION['loadtime']){
  $_SESSION['loadtime'] = time();
}
$now = time();

$elapsedTime = $_SESSION['loadtime'] - $now;  //first time should be Zero; on reload will equal number of seconds elapsed

$timeRemaining = 30 - $elapsedTime;


?>

... then, in your javascript, instead of using a fix var of "30" use the dynamic variabl set in php ...

var seconds= <?php echo $timeRemaining ?>

Link to comment
https://forums.phpfreaks.com/topic/102756-is-this-impossible/#findComment-526280
Share on other sites

it's a good idea.. but i cannot get your code to work...

 

Cannot send session cookie - headers already sent by (output started at blah blah blah.....

 

 

i guess i'm not to familiar with $_SESSION variables... i did stick the PHP code in the first line of the document...

Link to comment
https://forums.phpfreaks.com/topic/102756-is-this-impossible/#findComment-526302
Share on other sites

hmm, the error means that something was output to the browser before you initiated the session_start() call.  make sure there is no code before it, not even white space.  If that doesn't help, checkout the sticked thread on this site about output buffering and header errors.

Link to comment
https://forums.phpfreaks.com/topic/102756-is-this-impossible/#findComment-526315
Share on other sites

i got it to work... kind of...

 

i don't know if your math is right tho... it's adding the elapsed seconds to a new 30 seconds on reload...

 

also, does this reset after each question.. so when the user comes back to the page after answering a question will the sesson variable reset?

Link to comment
https://forums.phpfreaks.com/topic/102756-is-this-impossible/#findComment-526362
Share on other sites

ok.. i think i have it working... but how do you un set the Session variable ... i've tried using these methods:

 

if(isset($_SESSION[loadtime])){
$_SESSION[loadtime] = time();
}

 

as well as

session_unset();

 

would it be easier to user $_COOKIE rather than $_SESSION? and set an the cookie to expire after 40 secs or something?

 

 

Link to comment
https://forums.phpfreaks.com/topic/102756-is-this-impossible/#findComment-526381
Share on other sites

ok i've got it... this seems to work fine..

 

startcount.php

 

<a href="count.php">Start Counting</a>

 

 

count.php

<?php
session_start();
if(!isset($_COOKIE[loadtime])){
setcookie("loadtime", time());
$_COOKIE['loadtime'] = time();
}

$now = time();
echo $_COOKIE['loadtime'];

echo '<br><br>';
echo $now;
echo '<br><br>';
echo 'Time Since Set: ';
$eTime = $now - $_COOKIE['loadtime'];

echo $eTime;

$rTime = 30 - $eTime;

?>


<br><br>
	<form name="counter">
	<input type="text" size="8" readonly name="d2" class=count></form> 

	<script> 
	<!-- 
	// 
	 var milisec=0 
	var seconds=<?php echo $rTime; ?>

	document.counter.d2.value='30' 


	function display(){ 

	if(seconds == 0 && milisec == 0){
	document.forms.form1.submit()
	}


	 if (milisec<=0){ 
		milisec=9 
		seconds-=1 
	 } 
	 if (seconds<=-1){ 
		milisec=0 
		seconds+=1 
	 } 
	 else 
		milisec-=1 
		document.counter.d2.value=seconds+"."+milisec 
		setTimeout("display()",100) 
	   
	} 

	display() 
	--> 
	</script>  

	<form action="resetsess.php" method=GET name=form1>

	<input type="submit" value="Answer">
	</form>

 

resetsess.php

 

<?php
setcookie("loadtime", "", -3600);

header("Location: startcount.php");
?>

 

Link to comment
https://forums.phpfreaks.com/topic/102756-is-this-impossible/#findComment-526608
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.