Cookie and Session Bayu Priyambadha, S.Kom
What is Cookie? Data saving mechanism Saved in user/client computer Containt of teks data Transmit as a HTTP Header Has an active time Has a function : Identification client Authentication client
PHP Cookie PHP function : Example : setcookie(name, value, expire); $value = 'something from somewhere'; setcookie("TestCookie", $value, time()+3600); ?>
Read Cookie Using : Example : $_COOKIE[“nama_cookie”] <?php echo $_COOKIE("TestCookie"); ?>
Try This!!! <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php if (!isset($_COOKIE["nama_client"])) { ?> <form action="proses.php" method="post"> Nama : <input type="text" name="nama" /><br /> <input type="submit" value="Go" /> </form> <? } else echo "Ini dia nama client-nya : ".$_COOKIE["nama_client"]; </body> </html>
Try This!!! (2) <?php setcookie("nama_client",$_POST["nama"],time()+120); ?>
Session Session is same as cookie, but this mechanism store data to the server Must be started with : session_start() Example : <?php session_start(); $_SESSION['views'] = 1; echo "Pageviews = ". $_SESSION['views']; ?>