Reputation: 43
I am developing a fun script where user will be guessing images by putting points for each image which the game owner will be displaying. If he guesses right, his points will be doubled. If he fails to guess, the points will be added to game owner.
In this, After submitting the form, the images which user has guessed and the images game owner displays, will be shown in the page. After that the calculations will happen.
I have properly done all the steps, but if the user reloads the page from browser refresh button, the calculations happens again and again . Not getting how to avoid this.
I can not use
header("location: reditect_page.php");
exit();
As the user should see the data.
Can somebody suggest me to resolve this?
Here is the my submission page
if(isset($_POST['guess']))
{
//user entered/guessed images
$img1 = $_POST['img1'];
$img2 = $_POST['img2'];
$img3 = $_POST['img3'];
$img1_pnt = $_POST['point1'];
$img2_pnt = $_POST['point2'];
$img3_pnt = $_POST['point3'];
$sn1 = mysqli_query($con, "SELECT * from images where id=".$img1."");
$sn2 = mysqli_fetch_array($sn1);
$mn1 = mysqli_query($con, "SELECT * from images where id=".$img2."");
$mn2 = mysqli_fetch_array($mn1);
$is1 = mysqli_query($con, "SELECT * from images where id=".$img2."");
$is2 = mysqli_fetch_array($is1);
//game Owner's image
$k1 = "SELECT * FROM days_data WHERE id=".$gid." AND lead=".$_SESSION['lead']." ORDER BY id DESC LIMIT 1";
$k2 = mysqli_query($con, $k1);
$k3 = mysqli_fetch_array($k2);
$var1 = $k3['img1'];
$var2 = $k3['img2'];
$var3 = $k3['img3'];
$vararray = array($var1, $var2, $var3);
$s1 = mysqli_query($con, "select * from images where id = ".$var1."");
$s2 = mysqli_query($con, "select * from images where id = ".$var2."");
$s3 = mysqli_query($con, "select * from images where id = ".$var3."");
$q1 = mysqli_fetch_array($s1);
$q2 = mysqli_fetch_array($s2);
$q3 = mysqli_fetch_array($s3);
//Display User guessed images
echo '<div class="row gutter30" style="margin-top:20px;">
<h2>Guessed Data</h2>
<div class="col-xs-4">
<img src="img/'.$mn2['image'].'" class="image" />
</div>
<div class="col-xs-4">
<img src="img/'.$sn2['image'].'" class="image" />
</div>
<div class="col-xs-4">
<img src="img/'.$is2['image'].'" class="image" />
</div>
</div>
//Game Owner's Images
<div class="row gutter30" style="margin-top:20px;">
<h2>RESULT</h2>
<div class="col-xs-4">
<img src="img/'.$q1['image'].'" class="image" />
</div>
<div class="col-xs-4">
<img src="img/'.$q2['image'].'" class="image" />
</div>
<div class="col-xs-4">
<img src="img/'.$q3['image'].'" class="image" />
</div>
//Here i will do the calculations of points
if(($_POST['img1_pnt'])!='')
{
if(in_array($_POST['img1'], $vararray))
{
// Calculations part
}
}
}
Upvotes: 1
Views: 664
Reputation: 61
I hope this is what you are looking for:
If I understand you correctly, if the page is refresh, the data is display twice, if it refreshes 3 times, a similar data is display 3 times.
This works for me.
include 'action.php';
> echo '<META HTTP-EQUIV=REFRESH CONTENT="1; > '.$_SERVER["PHP_SELF"].'">';
or any code similar. Since you are currently in your index.php file, the code above ask to refresh your action.php file which does not contain your form. Hope this would work.
Upvotes: 0
Reputation: 2755
You can use session to keep track of the user attempt count. Then use this value to prevent the user from attempting multiple times.
In your code where you start the session, add the following code to start tracking attempts.
session_start();
if (!isset($_SESSION['attempt_count'])) {
$_SESSION['attempt_count'] = 0;
}
Then you need to increment the attempt count for each attempt made by the user and prevent multiple submissions. For this, replace the following code.
if(isset($_POST['guess']))
{
//user entered/guessed images
$img1 = $_POST['img1'];
$img2 = $_POST['img2'];
$img3 = $_POST['img3'];
With the code below.
if(isset($_POST['guess']) && $_SESSION['attempt_count']==0)
{
$_SESSION['attempt_count']++;
//user entered/guessed images
$img1 = $_POST['img1'];
$img2 = $_POST['img2'];
$img3 = $_POST['img3'];
Upvotes: 1
Reputation: 2738
Put this code in starting of your php code outside isset submit button
if(empty($_SESSION['key']) && !isset($_SESSION['key'])){
$randomkey = rand(0,99999);
$mykey = $_SESSION['key'] = $randomkey
}
It set Session a unique random key. now in form field take one hidden field
<input type="hidden" name="key" value="<?=$mykey?>">
Now On Submit
$mykey
Matches With Hidden Variable $mykey
So it prevent user adding data multiple times by Refreshing the page.
I hope it Helps... :-)
Upvotes: 1