#native_company# #native_desc#
#native_cta#

Setting and Deleting Cookies on a Fly

By James Smith
on July 2, 2002

Version: 2

Type: Full Script

Category: Other

License: GNU General Public License

Description: As a programmer you may have faced this problem of setting a cookie and the next page
still reading the older value of cookie , which is a never a desired case , this problem
becomes typical when a new user has logged in and then logs out and then relogs in with
out closing the browser , so the code below helps to solve the previous values of cookies
and setting new values for a given cookie name

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
  <title>Login</title>
</head>
<body>
&lt;!------ Start log.php -------------------------------&gt;<br>
&lt;html&gt;<br>
&lt;head&gt;<br>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;title&gt;Untitled&lt;/title&gt;<br>
&lt;/head&gt;<br>
&lt;body onload='document.frm4.userid.focus();'&gt;<br>
&lt;form name='frm4' method='get' action='logresp.php'&gt;<br>
&lt;table border=1&gt;<br>
&lt;tr&gt;<br>
&nbsp;&nbsp; &nbsp;&lt;td&gt;Enter User Name :&lt;/td&gt;&lt;td&gt;&lt;input
type='text' name='userid' autocomplete='off'&gt;&lt;/td&gt;<br>
&lt;/tr&gt;<br>
&lt;tr&gt;<br>
&nbsp;&nbsp; &nbsp;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;input type='submit'
VALUE='SETCOOKIE'&gt;&lt;/td&gt;<br>
&lt;/tr&gt;<br>
&lt;/table&gt;<br>
&lt;/form&gt;<br>
&lt;?<br>
if($cookuserid)<br>
{<br>
&nbsp;&nbsp; &nbsp; //Program No 1<br>
&nbsp;&nbsp; &nbsp; print ("&lt;br&gt;&lt;B&gt;userid&lt;/B&gt; cookie value
:: $cookuserid");&nbsp;&nbsp; &nbsp;<br>
&nbsp;&nbsp; &nbsp; $username = "James Smith"; <br>
&nbsp;&nbsp; &nbsp; $email = "[email protected]"; <br>
&nbsp;&nbsp; &nbsp; $Fromname = "Programmer"; <br>
&nbsp;&nbsp; &nbsp; $Fromaddress = "info"; <br>
&nbsp;&nbsp; &nbsp; $mailsubject = " $cookuserid Logged In "; <br>
&nbsp;&nbsp; &nbsp; <br>
&nbsp;&nbsp; &nbsp; $msg = "&lt;font face='OCR A Extended' size='2'&gt;Hi
$username n&lt;br&gt;"; <br>
&nbsp;&nbsp; &nbsp; $msg = $msg.''."t This is to inform you that&nbsp; $cookuserid
just Logged In&nbsp; n&lt;br&gt; "; <br>
&nbsp;&nbsp; &nbsp; print ("&lt;PRE&gt;"); <br>
&nbsp;&nbsp; &nbsp; if (mail($username." &lt;".$email."&gt;", $mailsubject,
$msg, "From: ".$Fromname." &lt;".$Fromaddress."&gt;nContent-Type: text/html;
charset=iso-8859-1")) <br>
&nbsp;&nbsp; &nbsp; { <br>
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print ("Mail Sent To $username
,Informing&nbsp; $cookuserid Logged In &lt;br&gt;&lt;br&gt;"); <br>
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print ($msg); <br>
&nbsp;&nbsp; &nbsp; } <br>
&nbsp;&nbsp; &nbsp; else <br>
&nbsp;&nbsp; &nbsp; { <br>
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print ("Mail Dead"); <br>
&nbsp;&nbsp; &nbsp; } <br>
&nbsp;&nbsp; &nbsp; print ("&lt;/PRE&gt;"); <br>
&nbsp;&nbsp; &nbsp;}<br>
?&gt;<br>
&lt;/body&gt;<br>
&lt;/html&gt;<br>
&lt;!------- End log.php ----------------------------------&gt;<br>
<br>
&lt;!------&nbsp; Start logresp.php -------------------------------&gt;<br>
&lt;? <br>
&nbsp;&nbsp; &nbsp;//Program No 2<br>
&nbsp;&nbsp; &nbsp;setcookie("cookuserid", $userid, 0, "/"); <br>
&nbsp;&nbsp; &nbsp;setcookie("cookuserid", 0);<br>
&nbsp;&nbsp; &nbsp;header ("Location: log.php?userid=$userid&amp;cookuserid=$cookuserid");<br>
?&gt;<br>
&lt;!------- End logresp.php ----------------------------------&gt;
</body>
</html>