I have a quiz game/guessing game website and I am trying to move all the code for scorekeeping serverside in order to stop people from cheating. The game shows the user 2 pictures and the user has to click on the correct one.
I have 1 PHP with all the pictureIDs. 2 of them are set as session variables and are given to the Javascript with an HTTP request and put on the html.
So now when the users answers a question I have 1 php document for each clickable image which compares that image to the solutions and returns correct or false. it also keeps track of the score. After that the first php is called with 2 new pictureIDS.
For the score tracking i have session variables. "score" variable and a "Lives" variable as a SESSION variable in PHP which are incremented and decremented when the user is right or wrong. (I also have a streak variables which adds a 2x multiplier after three correct in a row but that shouldn't be relevant.)
For some reason, the score incrementing only works once. for the first question the score goes up 1 or the lives go down 1 but after that for every other question the score stays the same.
I have included a lot of my code I hope this isn't too overwhelming. Basically the javascript isn't that relevant. All you need to know is I'm basically running the first php then the second, and then the first again and so on in a loop. And its only changing the session Variables once. What am i doing wrong? There must be something wrong with how im starting the session right? `
PHP document for showing selecting the 2 images:
<?php
header('Content-Type: application/json');
?>
<?php
if (session_status() == PHP_SESSION_NONE) {
session_start();
$_SESSION["score"]=0;
$_SESSION["streak"]=0;
$_SESSION["lives"]=5;
$_SESSION["onfire"]=0;
}
$rightimages = array( "right1","right2","right3");
$wrongimages = array( "wrong1","wrong2","wrong3");
$right= mt_rand(0,count($right)-1);
$wrong= mt_rand(0,count($wrong)-1);
$coin = mt_rand(0,1);
if ($coin == 1){
$img1=$rightimages[$right];
$img2=$wrongimages[$wrong];
}else{
$img1=$wrongimages[$wrong];
$img2=$rightimages[$right];
}
$_SESSION["im1"]=$img1;
$_SESSION["im2"]=$img2;
$arr = array('im1' => $img1, 'im2' => $img2);
$myJSON = json_encode($arr);
echo $myJSON;
?>
PHP document checking solution for when img 1 is clicked.
<?php
header('Content-Type: application/json');
?>
<?php
if (session_status() == PHP_SESSION_NONE) {
session_start();
$_SESSION["score"]=0;
$_SESSION["streak"]=0;
$_SESSION["lives"]=5;
$_SESSION["onfire"]=0;
}
$wrongimages = array( "wrong1","wrong2","wrong3");
$iswrong =in_array($_SESSION["im2"], $wrongimages);
if ($iswrong){
$_SESSION["lives"]--;
$_SESSION["streak"]=0;
}
if(!$iswrong){
$_SESSION["score"]++;
$_SESSION["streak"]++;
if ($_SESSION["streak"]>2){
$_SESSION["onfire"]==0;
$_SESSION["score"]++;
}
if ($_SESSION["score"]==10){
$_SESSION["lives"]++;
}
}
$iswrongnum=(int)$iswrong;
$myarray=array('iswrong' =>$iswrong,'score' => $_SESSION["score"], 'streak' => $_SESSION["streak"], 'lives' =>$_SESSION["lives"]);
$myJSON = json_encode($myarray);
echo $myJSON;
?>
Relevant JS code this function is called repeatedly after every question. (i have reduced it down to only clicking image 1 because clicking image 2 would be essentially the same and it's not relevant to the question.)
function play(){
var xmlhttp1 = new XMLHttpRequest();
xmlhttp1.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var myObj = JSON.parse(this.responseText);
img1 = myObj.im1;
img2 = myObj.im2;
document.getElementById("img1").src = img1;
document.getElementById("img2").src = img2;
}
};
xmlhttp1.open("GET", "arrayquestion1.php", true);
xmlhttp1.send();
document.getElementById("img1").onclick = function() {
this.onclick=null;
document.getElementById("img2").onclick =null;
var xmlhttp2 = new XMLHttpRequest();
xmlhttp2.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var myObj2 = JSON.parse(this.responseText);
isonewrong = myObj2.iswrong;
score = myObj.score;
streak =myObj.streak;
lives =myObj.lives;
}
};
xmlhttp2.open("GET", "arraynorm1.php", true);
xmlhttp2.send();
}
if (session_status() == PHP_SESSION_NONE)in your code snippets will always evaluate totrue. So you reset the values each time. You need to callsession_startat the start of your code