I'm using PHP's Rand() function to generate two random numbers that I can compare against the user's, and echo out a success message if the user's answer equals (1st random number + 2nd random number.) However, I'm running into problems.
I suspected that the form was re-generating the numbers every time the form POSTED and the input was collected, so I tried using sessions instead to keep those numbers persistent. It's a mess to say the least.
I found this existing post: Problems with guess a number game , but the solution didn't remedy my problem.
<?php
if(empty($_POST))
{
$_SESSION['$number1'] = Rand(0, 100);
$_SESSION['$number2'] = Rand(0, 100);
}
if($_POST["submit"])
{
$input = $_POST['input'];
if($input == ($_SESSION['$number1'] + $_SESSION['$number2']))
{
echo "Correct! ";
}
else
{
echo "Incorrect! ";
}
}
echo "<hr><br> What is... <b>" . $_SESSION['$number1'] . " + " . $_SESSION['$number2'] . "</b>";
?>
<form action="" method="post">
<input type="text" name="input">
<input type="submit" name="submit">
</form>
<?php
echo "<b>DEBUG ANSWER: </b> " . ($_SESSION['$number1'] + $_SESSION['$number2']);
?>
Any help would be appreciated!
I changed a few things, personally, I wouldn't use the session, rather user hidden inputs. (if you're worried about security.. you shouldn't be.. numbers game, not banking site)
<?php
//Create a function to generate the random numbers (So we can re-use it)
function generateNumbers()
{
$one = Rand(0, 100);
$two = Rand(0, 100);
//Now return the random numbers
return array('number1' => $one, 'number2' => $two);
}
//Check if the submit button has been clicked
if($_POST["submit"])
{
$input = $_POST['input'];
if($input == $_POST['number1'] + $_POST['number2'])
{
echo "Correct! ";
}
else
{
echo "Incorrect! ";
}
}
//Now we create the numbers
$numbers = generateNumbers();
echo "<hr><br> What is... <b>" . $numbers['number1'] . " + " . $numbers['number2'] . "</b>";
?>
<form action="" method="post">
<input type="text" name="input">
<input type="submit" name="submit">
<!-- Personally I would rather use hidden inputs, than use the session -->
<input type="hidden" name="number1" value="<?php echo $numbers['number1'] ?>" />
<input type="hidden" name="number2" value="<?php echo $numbers['number2'] ?>" />
</form>
<?php
echo "<b>DEBUG ANSWER: </b> " . ($numbers['number1'] + $numbers['number2']);
?>
Related
I am trying to create an adding game PHP and HTML, where the user guesses the sum of two random integers. However, when the user submits the answer, the integers randomize again, changing the sum and making the user's guess incorrect.
<?php
$x = (rand(1,10));
$y = (rand(1,10));
$sum = $x + $y;
if (isset($_POST['submit']))
{
$guess = $_POST['guess'];
if ($sum == $guess)
{
echo "nice, you got it right.";
}
else {
echo "aw, too bad. <br>";
}
}
echo "<br>What is " . $x . " + " . $y . "? <br>";?>
<form action="" method="post">
<input name="guess" type="text" />
<input name="submit" type="submit" />
</form>
I am expecting the output to be "nice, you got it right" when the $guess==$sum, but $sum changes when the form is submitted, making the 2 variables unequal.
Try to use session and store the two random number. PHP Session
just like this.
<?php
session_start();
if (isset($_POST['submit']))
{
$sum = $_SESSION['y'] + $_SESSION['x'];
$guess = $_POST['guess'];
if ($sum == $guess)
{
echo "nice, you got it right.";
}
else {
echo "aw, too bad. <br>";
}
}
$_SESSION['x']= (rand(1,10));
$_SESSION['y']= (rand(1,10));
echo "<br>What is " . $_SESSION['x'] . " + " . $_SESSION['y'] . "? <br>";
?>
<form action="" method="post">
<input name="guess" type="text" />
<input name="submit" type="submit" />
</form>
I hope I answer your problem. just read the PHP session it helps you a lot.
You can create a hidden input field to post the sum value. Then compare that value with the guessed value.
<?php
$x = (rand(1,10));
$y = (rand(1,10));
$sum = $x + $y;
if (isset($_POST['submit'])) {
$guess = $_POST['guess'];
$value = $_POST['sum'];
if ($value == $guess) {
echo "nice, you got it right.";
} else {
echo "aw, too bad. <br>";
}
}
echo "<br>What is " . $x . " + " . $y . "? <br>";
?>
<form action="" method="post">
<input name="guess" type="text" />
<input type="hidden" name="sum" value="<?=$sum?>" />
<input name="submit" type="submit" />
</form>
You should store your $x and $y in a hidden input.
Calculate the sum by using the post value instead of generating new value.
Hello i made this code below but i want it to be able to check if i guessed the answer good. If i insert 3 and 4 the answer is 5 but how do i let the code check if the answer "5" is right with the 2("3 and 4") input fields. So if all three input fields are correct. echo good and if not echo false. I am bad at explaining but i hope it's clear.
<form method="POST">
<label>Rechthoek verticaal</label>
<input name="vert"></input>
<label>Rechthoek horizontaal</label>
<input name="hor"></input>
<label>Schijne zijde</label>
<input name="schuin"></input>
<input type="submit" name="submit"></input>
</form>
<?php
if(isset($_POST['submit'])) {
$vert = $_POST['vert'];
$hor = $_POST['hor'];
$schuin = $_POST['schuin'];
$vert = pow($vert,2);
$hor = pow($hor,2);
$schuin = sqrt($vert + $hor);
echo $schuin;
}
?>
You need to check wheter sqrt($vert + $hor) is same as $_POST['schuin']
<form method="POST">
<label>Rechthoek verticaal</label>
<input name="vert"></input>
<label>Rechthoek horizontaal</label>
<input name="hor"></input>
<label>Schijne zijde</label>
<input name="schuin"></input>
<input type="submit" name="submit"></input>
</form>
<?php
if(isset($_POST['submit'])) {
$vert = $_POST['vert'];
$hor = $_POST['hor'];
$schuin = $_POST['schuin'];
$vert = pow($vert,2);
$hor = pow($hor,2);
if ($schuin == sqrt($vert + $hor)) {
echo "Good";
}
else {
echo "False";
echo "<br> Answer : ".sqrt($vert + $hor);
}
I'm not pretty sure to be understanding your problem, but if i'm, just compare the POST var with the calculated one:
echo ($schuin == $_POST['schuin'])?'good':'false';
PD: you are usingthe same var "$schuin" twice.
I writing simple game. I randomizes two numbers and then user put your sum value number to input form whose I sent on the same site. Now if user guess sum numbers it he get 10 points, but if he will miss, lost one chance.
I have problem, because if i Refresh site i overiate he points and chance. Any solution?
$_SESSION['points'];
//$_SESSION['chance'];
$X = rand(1, 10);
$Y = rand(1, 10);
echo $X . " " . $Y;
?>
<form name="MyForm" action="" method="POST">
<input type="text" name="value">
<input type="hidden" value="<?= $X ?>" name="number">
<input type="hidden" value="<?= $Y ?>" name="number2">
<input type="submit" value="Generuj" name="Wynik?"/>
</form>
<?php
if (!empty($_POST) && isset($_POST)) {
$user = $_POST['value'];
if ($_POST['number'] * $_POST['number2'] == $user) {
echo ' ok !';
echo $_SESSION['points'] += 10;
} else {
echo 'error';
// echo 'You lost one chance' . $_SESSION['chance']--;
}
}
You can simply use a PHP session OR a PHP Cookie.
Using cookies (if information is not sensible)
<?php
$value = 'something from somewhere';
setcookie("TestCookie", $value);
setcookie("TestCookie", $value, time()+3600); /* expire in 1 hour */
?>
Then you can echo your cookie like this
<?php
echo $_COOKIE['TestCookie'];
?>
If you're using Sessions
<?php
session_start();
$_SESSION['myVar'] = $myValue;
?>
Then you can echo it that way
<?php
echo $_SESSION['myVar'];
?>
NOTE that with the session, you always have to start your session at the beginning of your PHP file.
I am trying to write a simple math captcha function to validate a form which will alow users to post a specific article on a blog.
The function:
function create_captcha($num1, $num2) {
$num1 = (int)$num1;
$num2 = (int)$num2;
$rand_num_1 = mt_rand($num1, $num2);
$rand_num_2 = mt_rand($num1, $num2);
$result = $rand_num_1 + $rand_num_2;
return $result;
}
Form:
<h2 class="email_users_headers">Post Article</h1>
<form action="" method="post" id="post_blog_entry">
<input type="text" name="title" id="title" placeholder="title... *"><br>
<textarea name="content" id="content" cols="30" rows="5" placeholder="content... *"></textarea><br>
<?php echo create_captcha(1, 20) . ' + ' . create_captcha(1, 20) . ' = '; ?>
<input type="text" name="captcha_results" size="2">
<input type="hidden" name='num1' value='<?php echo create_captcha(1, 20); ?>'>
<input type="hidden" name='num2' value='<?php echo create_captcha(1, 20); ?>'>
<input type="submit" name="publish" value="post entry" id="publish">
</form>
Captcha validation:
if (empty($_POST['captcha_results']) === true) {
$errors[] = 'Please enter captcha';
} else {
if ($_POST['captcha_results'] != $_POST['num1'] + $_POST['num2']) {
$errors[] = 'Incorrect captcha';
}
}
I know this is totally wrong because no matter the input the result is always incorrect. Could someone point me in the right direction as I don't have any experience with PHP.
Every time you run create_captcha(1, 20) it generates different values.
create_captcha(1, 20) - 3 + 7 = 10
create_captcha(1, 20) - 6 + 11 = 17
...
And in your form you call it 4 times.
I think you should do this:
$num1 = create_captcha(1, 20);
$num2 = create_captcha(1, 20);
And use this values in your form:
<textarea name="content" id="content" cols="30" rows="5" placeholder="content... *"></textarea><br>
<?php echo $num1 . ' + ' . $num2 . ' = '; ?>
<input type="text" name="captcha_results" size="2">
<input type="hidden" name='num1' value='<?php echo $num1; ?>'>
<input type="hidden" name='num2' value='<?php echo $num2; ?>'>
Having the two numbers in a hidden input allows bots to solve it through javascript (see Pseudocode)
code = hidden[0].value + hidden[1].value
To mitigate this, consider multiplying the value, and using base64. This will slow down the bots.
You can mitigate it further by providing a decoy hidden input
$num3 = mt_rand(0,20);
echo '<input type="hidden" name="num3" value="'.$num3.'">';
I like the idea a lot though.
I need to make a button with PHP. The fifth time when pressed should change my background color with a random one and keep it of every five presses. after another 5 press my background color should change again and keep it until next 5 press
Here is my code
<?php
session_start();
$_SESSION['counter'] = isset($_SESSION['counter']) ? $_SESSION['counter'] : 0;
if($_POST['sub']) {
$_SESSION['counter']++;
echo "<br/>";
echo $culoare;
echo "<body bgcolor='<?phpecho $culoare;?>'></body>";
}
?>
</head>
<body >
<form action='' method="post">
<input type="submit" name="sub" value="click" />
<input type="hidden" name="counter" value="<?php print $_POST['counter']; ?>" />
</form>
</body>
What is the problem? You have the code.
When the form is submitted, get the value of the counter in the hidden field. If the value is not equal to 5, increment it and pass it again to your view to store it the hidden field. When the value is equal to 5, change your background color and set the counter to 0.
You need to keep your color in an hidden field to if you want to have the same color five times.
<?php
// First call.
if(!isset($_POST)) {
$color = "rgb(" . rand(0, 255) . "," . rand(0,255) . "," . rand(0,255) . ")";
$counter = 0;
}
else {
$color = $_POST['color'];
$counter = $_POST['counter'];
}
if($counter == 5) {
$color = "rgb(" . rand(0, 255) . "," . rand(0,255) . "," . rand(0,255) . ")";
$counter = 1; // Set to 1 if you want to change bg color on fifth click.
}
else {
$counter++;
}
?>
<div style="background-color: <?php echo $color; ?>">
<form method="post">
<input type="hidden" name="color" value="<?php echo $color; ?>" />
<input type="hidden" name="counter" value="<?php echo $counter; ?>" />
<input type="submit" value="Submit" />
</form>
</div>
I think that something like this may work.