I am getting the guessing of a secret number through $_SESSION, I have removed that part, but now I need it to count the attempts I have made, I have been trying with the structure of $_SESSION['variable'] and adding, but I always get remains at 1, it does not increase for each attempt I try.
I leave you the PHP code:
<?php
session_start();
?>
<html>
<head>
<title>Adivina el valor oculto</title>
</head>
<body>
<h1>Advinando el número..</h1>
<?php
function genValor() {
return $var = rand(0, 10);
}
if (!isset($_SESSION['valor_aleat'])) $_SESSION['valor_aleat'] = genValor();
if (isset($_REQUEST['numero']))
{
$_SESSION['intentos'] = 0;
$Num_Recibido = $_REQUEST['numero'];
$Num_Aleatorio = $_SESSION['valor_aleat'];
echo "Número secreto";
echo "<br>";
echo $Num_Aleatorio;
echo "<br>";
echo "Número introducido por ti";
echo "<br>";
echo $Num_Recibido;
echo "<br>";
if ($Num_Aleatorio == $Num_Recibido) {
echo "<H1>Numero acertado</H1>";
echo '<a href="./Adivino.html"> Iniciar nueva partida </a>';
unset ($_SESSION['valor_aleat']);
++$_SESSION['intentos'];
}
else {
if ($Num_Recibido < $Num_Aleatorio) {
echo "Numero incorrecto, el valor es mayor";
echo "<br>";
echo '<a href="./Adivino.html"> Volver a la 1º Pagina </a>';
++$_SESSION['intentos'];
} else {
echo "Número incorrecto, el valor es menor";
echo "<br>";
echo '<a href="./Adivino.html"> Volver a la 1º Pagina </a>';
++$_SESSION['intentos'];
}
}
}
echo "<br>";
echo "<br>";
echo "Numero de intento:";
echo $_SESSION['intentos'];
?>
</body>
</html>
There you have everything, I ask for a secret number to get it from me but it doesn't let me do the operation of counting all the attempts correctly.
I think the correct way to increment would
intentos
be something like this.Try and tell me. Untested code.