Wednesday 26 June 2013

save data from a form app to a cookie and retrieve using php

create cookie.html file and write the following code.
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>data storing in cookie</title>
</head>
<body>
<form action="cookieset.php" method="post">
    <p> Name <input type="text" name="astate" size="10px" /></p>  
    <input type="submit" name="submit" value="Submit" />
</form>
<form action="retreive.php" method="get">
    <input type="submit" name="retreive" value="Retreive" />
</form>
</body>
</html>

create cookieset.php file and write the following code for store the data.
<?php
$astate = $_POST['astate'];
$expire=time()+60*60*24*30;
setcookie("astate", $astate, $expire);

?>
write the code in retrieve.php file to retrieve cookie data 
<html>
<body>
<?php
if (isset($_COOKIE["astate"]))
  echo "Welcome " . $_COOKIE["astate"] . "!<br>";
else
  echo "Welcome guest!<br>";
?>
</body>

</html>

No comments: