note 59726 deleted from ref.session by dmytton
| From: | dmytton@php.net | Date: | Wed, 14 Dec 2005 14:53:36 +0000 |
| Subject: | note 59726 deleted from ref.session by dmytton | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-100136@lists.php.net to get a copy of this message | ||
Note Submitter: useradd at dot dot dot com
Reason: useless
----
Hi, I'd want to show you how important sessions are for php...look this simple game in html...
<?php
// starts new session or recover last one...
session_start();
include("funciones.php");
// There is a game in play...
// If I come from initial form, i must start a new game...
// otherwise, I will follow started game...
if(isset($_POST["palabra"]))
{
// I come from initial form
$_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.php" 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();
}
?>