note 59728 added to ref.session
| From: | php-general at lists dot php dot net | Date: | Wed, 14 Dec 2005 13:07:39 +0000 |
| Subject: | note 59728 added to ref.session | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-100126@lists.php.net to get a copy of this message | ||
ok...i'll want to post a simply game that uses sessions...so you'll understand how
important sessions are for php and html! Please name it "ahorcado.php".
<?php
// Start or revover session
session_start();
include("funciones.php");
// There is a game in play
if(isset($_POST["palabra"]))
{//New game!
$_SESSION["palabra"]=$_POST["palabra"];
$_SESSION["max_intentos"]=$_POST["max_intentos"];
$_SESSION["partida_en_curso"]=true;
$_SESSION["intentos"]=0;
$_SESSION["letras"]="";
juega();
}
else
{
if($_SESSION["partida_en_curso"]==true)
{
$duplicado=false;
for($i=0;$i<(strlen($_SESSION["letras"])) ;$i++)
{
if(( $_SESSION["letras"]{$i} )==($_POST["letra"]) ){ $duplicado=true;}
}
if ($duplicado==false) {
$_SESSION["letras"]= $_SESSION["letras"] . $_POST["letra"];
}
$_SESSION["intentos"]= $_SESSION["intentos"] + 1;
$restantes= ($_SESSION["max_intentos"]) - ($_SESSION["intentos"]);
if($_SESSION["intentos"] < ($_SESSION["max_intentos"] + 1 )){
juega();
}
else {
$_SESSION["partida_en_curso"]=false;
echo abre_web("AHORCADO");
echo "<p>Lo sentimos, pero se te han acabado los intentos.</p>";
echo cierra_web();
}
}
else
{
echo abre_web("Bienvenidos al juego del ahorcado");
echo formulario_principal();
echo cierra_web();
}
}
function juega()
{
echo abre_web("AHORCADO");
echo "<p>Llevas ". $_SESSION["intentos"] ."
intentos.</p>";
echo "<p>Has visto estas letras: ". $_SESSION["letras"] .
"</p>";
$palabra=$_SESSION["palabra"];
$ganar="cierto";
for($i=0;$i<(strlen($palabra));$i++)
{ $esta="falso";
for($j=0;$j<(strlen($_SESSION["letras"]));$j++)
{
if($_SESSION["letras"]{$j}==$palabra{$i})
{
echo $palabra{$i};
$esta="cierto";
}
}
if($esta=="falso")
{
$ganar="falso";
echo "-";
}
}
if($ganar=="cierto")
{
echo "<h1>HAS GANADO!!!!!</h1>";
$_SESSION["partida_en_curso"]=false;
}
else {
$salida=<<<pre
<form action ="ahorcado" method = "post">
<p>Introduce una letra.</p>
<br>
<input type ="text" name ="letra"/>
<br>
<input type = "submit" value= "JUGAR"/>
pre;
}
echo $salida;
echo cierra_web();
}
?>
----
Manual Page -- http://www.php.net/manual/en/ref.session.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+59728
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59728&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59728&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59728&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59728&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59728&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59728&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59728&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+59728&report=yes
Search -- http://master.php.net/manage/user-notes.php