Example of uses for cookies
In this example we will learn how to set a cookie and how to retrieve the established value. In order to do this, we will ask users to insert their names that will be embedded in a cookie.
First we will ask the users to insert the value of their name, we use a form that will process the page process_cookie.phtml.
<!-- PHP Tutorial WebEstilo.com -->
<html>
<head>
<title>PHP Example</title>
</head>
<body>
<H1>Ejemplo de uso de cookie</H1>
Introduzca su nombre:
<FORM ACTION="procesar_cookie.phtml" METHOD="GET">
<INPUT TYPE="text" NAME="nombre"><BR>
<INPUT TYPE="submit" VALUE="Enviar">
</FORM>
</body>
</html>
We set the cookie ejemuser with the value previously inserted, and which duration is one hour.
<?php // Manual de PHP de WebEstilo.com
setcookie("ejemusuario", $_GET['nombre'], time()+3600,"/","");
?>
<html>
<head>
<title>PHP Example</title>
</head>
<body>
<H1>Ejemplo de uso de cookie</H1>
Se ha establecido una cookie de nombre <b>ejemusuario</b> con el valor: <b><? print $_GET['nombre']; ?></b> que será válida durante 1 hora.
</body>
</html>
In this example we can see how easy it is to retrieve the value of the previously set cookie.
<!-- PHP Tutorial WebEstilo.com -->
<html>
<head>
<title>PHP Example</title>
</head>
<body>
<H1>Ejemplo de uso de cookie</H1>
Se ha establecido la cookie de nombre <b>ejemusuario</b> vale: <b><? print $_COOKIE['ejemusuario']; ?></b>
</body>
</html>