tecmeister Posted February 17, 2008 Share Posted February 17, 2008 On the quiz that im creating. I want to know how to make the users only able to answer a once. Meaning that they are not able to change their answer once they have gone to there next question. Thanks for your help. tecmeister Link to comment https://forums.phpfreaks.com/topic/91514-fixed-score/ Share on other sites More sharing options...
uniflare Posted February 17, 2008 Share Posted February 17, 2008 you could try using cookies...mmmmm....cookies.....(easier), or session variables(more secure). For Cookie: -- give each question page an id on each question page add this to the VERY start of your page (before anything is sent to the browser) <?php $cStr = @$_COOKIE['Cookiename']; if(preg_match("/^PageID/",$cStr) == 0){ setcookie("Cookiename",$cStr.";PageID",time()+86400); }else{ // Could use Header("Location: nextpage.php");exit(); to redirect if the user has already submitted this question. } ?> replace "Cookiename" with a name of your choice (MUST be same on ALL pages) also replace "PageID" with a Unique String of Characters for each question page _ Next Add a Check where you add the points to the score, something like: <?php if(preg_match("/^PageID/",$_COOKIE['Cookiename']) != 0){ // Add or Subtract Player Score } ?> This way they can still view the page and continue if they accidentally hit the back button. (Assuming you did not use my suggestion for header() redirect) ===== NOTE: I have not used preg_match and setcookie for a long time i hope this example works __________________________ OR with Session Variables: you are familiar with sessions. Same Theory, Add a unique ID to each page <?php $sStr = @$_SESSION['PID_String']; if(preg_match("/^PageID/",$sStr) == 0){ $_SESSION['PID_String'] .= ";PageID"; }else{ // Could use Header("Location: nextpage.php");exit(); to redirect if the user has already submitted this question. } ?> replace "PageID" with a Unique String of Characters for each question page _ Next Add a Check where you add the points to the score, something like: <?php if(preg_match("/^PageID/",$_SESSION['PID_String']) != 0){ // Add or Subtract Player Score } ?> Unfortunately with this alone, all the user has to do is close the browser, of course the user would have to login again - then start again, so if your saving results to a username this should not be a problem. You MAY want to check if $_SESSION['username'] exists (isset() function is handy for this), if it doesn't then redirect to the login/start page. _______________________________ Good luck hope it helps Link to comment https://forums.phpfreaks.com/topic/91514-fixed-score/#findComment-468801 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.