You are on page 1of 2

COOKIES IN PHP

Index.php
<?php
$action=0;
extract($_REQUEST);
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<fieldset>

<table align="center" width="400" bordercolor="#330000">


<form name="f1" method="post" action="index_ctl.php" enctype="multipart/form-data">
<legend>Login here!!</legend>
<?php
if($action==1){
?>
<tr>
<td colspan="2" align="center"><font color="#FF0000">Invalid
Username/Password</font></td>
</tr>
<?php
}
?>
<td>User Name</td>
<td><input type="text" name="uname" style="border-bottom:3px solid #999999;
-moz-border-radius: 9px;
-webkit-border-radius: 9px;"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass" style="border-bottom:3px solid
#999999;
-moz-border-radius: 9px;
-webkit-border-radius: 9px;"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="Login"
style="border-bottom:3px solid #999999;
-moz-border-radius: 9px;
-webkit-border-radius: 9px;">
</tr>

</form>
</table>
</fieldset>

RESOURCE PERSON: MUHAMMAD ADEEL ABIDPage 1


COOKIES IN PHP

</body>
</html>

Index_ctl.php
<?php
extract($_REQUEST);
if($uname=="admin" && $pass=="123"){
setcookie("mycookie", $pass, time() + 300,"/"); // 86400 = 1 day
header("location:home.php");
exit;
}else{
header("location:index.php?action=1");
exit;
}

?>

home.php
<?php
if(!($_COOKIE['mycookie']>0)) {

header("location:index.php");
exit;
}
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
Welcome <?php print($_COOKIE['mycookie']);?><br>
<a href="logout.php">logout</a>
</body>
</html>

logout.php
<?php
unset($_COOKIE['mycookie']);
setcookie('mycookie', null, -1,"/"); // 86400 = 1 day
header("location:index.php");
exit;
?>

RESOURCE PERSON: MUHAMMAD ADEEL ABIDPage 2

You might also like