not so sweet Cookies!!!
Posted: Thu Dec 24, 2009 6:17 am
is cookies stored based on ip address or some other value?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
<?php
if($_POST['sub']){
$name=$_REQUEST['name'];//take users name in a name variable
if (!isset($_COOKIE["user"]))//check if cookie is set or not
{
setcookie("user",$name, time() + 60*60*24*7);//set the cookie if not set, important to not here is that cookie is set based on name
echo "Welcome to our site";
}
else//if cookie has been set already
{
echo "Welcome Back ".$_REQUEST['name'].".";
setcookie("user",$name, time() - 60*60*24*7);//unset the cookie so new users dont get a welcome back message
}
//echo $cookie["user"];
}
else{
echo "<form method='post' action='login.php'>
Name: <input name='name' type='text' /><br />
Username: <input name='uname' type='text' /><br />
Password: <input name='pword' type='password' /><br />
<input type='submit' name='sub' value='login' />
</form>";
echo "<br />";
}
?>Code: Select all
<?php
if($_POST['sub']){
$name=$_REQUEST['name'];//take users name in a name variable
if (!isset($_COOKIE["user"]))//check if cookie is set or not
{
setcookie("user",$name, time() + 60*60*24*7);//set the cookie if not set, important to not here is that cookie is set based on name
echo "Welcome to our site";
}
else//if cookie has been set already
{
$name1=$_COOKIE("user");// get the value of user and store in a variable $name1
if($name==$name1)//compare the values for a match
{
//setcookie("user",$name, time() - 60*60*24*7);//unset the cookie so new users dont get a welcome back message
echo "Welcome Back ".$_REQUEST['name'].".";
}
else
{
echo "Welcome to our site";
}
}
echo $_COOKIE["user"];
}
else{
echo "<form method='post' action='login.php'>
Name: <input name='name' type='text' /><br />
Username: <input name='uname' type='text' /><br />
Password: <input name='pword' type='password' /><br />
<input type='submit' name='sub' value='login' />
</form>";
echo "<br />";
}
?>Code: Select all
<?php
if($_POST['sub']){
if (!isset($_COOKIE["user"]))//check if cookie is set or not
{
$name=$_REQUEST["name"];//take users name in a name variable
setcookie("user",$name, time() + 60*60*24*7);//set the cookie if not set, important to note here is that cookie is set based on name
echo "Welcome to our site";
}
else//if cookie has been set already
{
$name2=$_REQUEST["name"];//take users name in a name variable
$name1=$_COOKIE["user"];//take cookies value in another name variable
if($name1==$name2){//check for a match of users variable and cookie variable
echo "Welcome Back ".$name2.". <br />";
}
else
{
$name3=$_REQUEST["name"];//take users name in a name variable
setcookie("user",$name3, time() + 60*60*24*7);
echo "Welcome to our site. <br />";
setcookie("user",$name3, time() + 60*60*24*7);//unset the created cookie
}
}
//echo $_COOKIE["user"];
}
else{
echo "<form method='post' action='login.php'>
Name: <input name='name' type='text' /><br />
Username: <input name='uname' type='text' /><br />
Password: <input name='pword' type='password' /><br />
<input type='submit' name='sub' value='login' />
</form>";
echo "<br />";
}
?>