note 59726 added to ref.session

From: Date: Wed, 14 Dec 2005 12:52:17 +0000
Subject: note 59726 added to ref.session
Groups: php.notes 
Request: Send a blank email to php-notes+get-100124@lists.php.net to get a copy of this message
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(); } ?> ---- Manual Page -- http://www.php.net/manual/en/ref.session.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+59726 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59726&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59726&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59726&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59726&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59726&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59726&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59726&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+59726&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#100124) next »