jueves, 21 de noviembre de 2013


Configurar con qué color de fondo de página quiere que aparezca cada vez que ingresa al sitio:

  

Sesion_Cookies.php

 

<html>

<head>

<title>Cookies</title>

<meta http-equiv="Content-Type" Content="text/html; charset=UTF-8">

</head>

<body

<?php if (isset($_COOKIE['color'])) echo " bgcolor=\"$_COOKIE[color]\""

 ?>

 >

 <form action="Establecer_Cookie.php" method="post">

Seleccione de que color desea que sea la página de ahora en más:<br>

<input type="radio" value="rojo" name="radio">Rojo<br>

<input type="radio" value="verde" name="radio">Verde<br>

<input type="radio" value="azul" name="radio">Azul<br>

<input type="submit" value="Crear cookie">

</form>

</body>

</html>

  

Establecer_Cookie.php

 

<?php

 if ($_REQUEST['radio']=="rojo")

setcookie("color","#ff0000",time()+60*60*24*365,"/");

elseif ($_REQUEST['radio']=="verde")

setcookie("color","#00ff00",time()+60*60*24*365,"/");

elseif ($_REQUEST['radio']=="azul")

setcookie("color","#0000ff",time()+60*60*24*365,"/");

 ?>

 <html>

<head>

<title>Cookies</title>

<meta http-equiv="Content-Type" Content="text/html; charset=UTF-8">

</head>

<body>

 Se creó la cookie.

 <br>

<a href=" Sesion_Cookies.php">Ir a la otra página</a>

</body>

</html>

  

Borrar una cookie:

  

Sesion_Cookie2.php

 

<html>

<head>

<title>BorrarCookie</title>

</head>

<body>

<form action="Borrar_Cookie2.php" method="post">

 Ingrese su mail:

 <input type="text" name="mailusuario"

value="<?php if (isset($_COOKIE['mail'])) echo $_COOKIE['mail'];?>">

<br>

<input type="radio" name="opcion" value="recordar">

 Recordar en esta computadora el mail ingresado.

 <br>

<input type="radio" name="opcion" value="norecordar">

 No recordar.

 <br>

<input type="submit" value="confirmar">

</form>

</body>

</html>

 

 Borrar_Cookie2.php

 

<?php

 if ($_REQUEST['opcion']=="recordar")

setcookie("mail",$_REQUEST['mailusuario'],time()+(60*60*24*365),"/");

elseif ($_REQUEST['opcion']=="norecordar")

setcookie("mail","",time()-1000,"/");

 ?>

 <html>

<head>

<title>BorrarCookie</title>

<meta http-equiv="Content-Type" Content="text/html; charset=UTF-8">

</head>

<body>

 <?php

 if ($_REQUEST['opcion']=="recordar")

echo "cookie creada";

elseif ($_REQUEST['opcion']=="norecordar")

echo "cookie eliminada";

 ?>

 <br>

<a href="Sesion_Cookie2.php">Ir a la otra página</a>

</body>

</html>

  

Haremos un problema muy sencillo, cargaremos en un formulario el nombre de usuario y clave de un cliente, en la segunda página crearemos dos variables de sesión y en una tercera página recuperaremos los valores almacenados en las variables de session:

  

Usuario_Sesion.html

 

<html>

<head>

<title>Variables de Sesion</title>

</head>

<body>

<form action="Variables_Sesion.php" method="post">

 Ingrese nombre de usuario:

 <input type="text" name="campousuario"><br>

 Ingrese clave:

 <input type="password" name="campoclave"><br>

<input type="submit" value="confirmar">

</form>

</body>

</html>

  

Variables_Sesion.php

 

<?php

 session_start();

$_SESSION['usuario']=$_REQUEST['campousuario'];

$_SESSION['clave']=$_REQUEST['campoclave'];

 ?>

 <html>

<head>

<title>Variables de Sesion </title>

<meta http-equiv="Content-Type" Content="text/html; charset=UTF-8">

</head>

<body>

Se almacenaron dos variables de sesión.<br><br>

<a href="Recuperar_Variables.php">Ir a la tercer página donde se recuperarán

las variables de sesión</a>

</body>

</html>

  

 Recuperar_Variables.php

 

 <?php

session_start();

?>

<html>

<head>

<title>Variables de sesion</title>

<meta http-equiv="Content-Type" Content="text/html; charset=UTF-8">

</head>

<body>

<?php

echo "Nombre de usuario recuperado de la variable de sesión:".$_SESSION['usuario'];

echo "<br><br>";

echo "La clave recuperada de la variable de sesión:".$_SESSION['clave'];

?>

</body>

</html>

 

 

No hay comentarios:

Publicar un comentario