Jump to content


Photo

Php Creates Cookie But Doesn't Store Them?


  • Please log in to reply
1 reply to this topic

#1 BrettHartel

BrettHartel

    Advanced Member

  • Members
  • PipPipPip
  • 55 posts

Posted 06 December 2012 - 08:12 PM

My php script creates an array of cookies. I know this because I print them on the screen. Unfortunately, when I click on a different page the cookies are not registering. I try to print the array and nothings shows. I do not get any errors. What should I do?

Index
<!DOCTYPE html>
<html>
<head>
<title>Diamond Book Club</title>
</head>
<body>
<!--#include virtual="menu.html" -->
<p style="text-align:center;color:red;"><b>Do not attempt to utilize this site. Your browser may crash and you may experience unplanned credit card charges! This site is still being tested for usability.</b></p>
<form action="signup.php" method="post">
eMail: <input type="text" name="eMail">
First Name: <input type="text" name="FirstName">
Last Name: <input type="text" name="LastName">
Password: <input type="password" name="Password">
<input type="radio" name="Gender" value="male">Male
<input type="radio" name="Gender" value="female">Female
Birth Month: <input type="text" name="Month">
Birth Day: <input type="text" name="Day">
Birth Year: <input type="text" name="Year">
<input type="submit" value="Sign Up">
</form>
<p>
<form action="deleteaccount.php" method="post">
User ID: <input type="text" name="User_ID">
<input type="submit" value="Delete User ID">
</form>
<p>
<?php
if (isset($_COOKIE["User_ID"]))
  echo "Welcome " . $_COOKIE["User_ID"] . "!<br>";
else
  echo "Welcome guest!<br>";
?> </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
<p> Text from Index </p>
</div>
</div>
</body>
</html>

The Menu:
<style>
a.menu {font-size:42px}
a.menu:link {color:#306754;text-decoration:none;}
a.menu:visited {color:#306754;text-decoration:none;}
a.menu:hover {color:#B5EAAA;text-decoration:none;}
a.menu:active {color:#306754;text-decoration:none;}
div.divMenu { float:left;width:35px;height35px; }
a.menublank0 {
display:inline-block; /* This cover full td */
height:50px;
width:12px;
}
a.menublank1 {
display:inline-block; /* This cover full td */
height:35px;
width:70.9px;
}
a.home {
display:inline-block; /* This cover full td */
background:url('/images/menubuttons.gif') no-repeat 0px 0px ; /*sets the background link image */
height:35px;
width:35px;
}
a.home:hover {
background:url('/images/menubuttons.gif') no-repeat 0px -35px ; /*sets the background link image and pushes the rest state up above view */
}
a.profile {
display:inline-block; /* This cover full td */
background:url('/images/menubuttons.gif') no-repeat -35px 0px ; /*sets the background link image */
height:35px;
width:35px;
}
a.profile:hover {
background:url('/images/menubuttons.gif') no-repeat -35px -35px ; /*sets the background link image and pushes the rest state up above view */
}
a.mail {
display:inline-block; /* This cover full td */
background:url('/images/menubuttons.gif') no-repeat -70px 0px ; /*sets the background link image */
height:35px;
width:35px;
}
a.mail:hover {
background:url('/images/menubuttons.gif') no-repeat -70px -35px ; /*sets the background link image and pushes the rest state up above view */
}
a.shop {
display:inline-block; /* This cover full td */
background:url('/images/menubuttons.gif') no-repeat -105px 0px ; /*sets the background link image */
height:35px;
width:35px;
}
a.shop:hover {
background:url('/images/menubuttons.gif') no-repeat -105px -35px ; /*sets the background link image and pushes the rest state up above view */
}
a.buddy {
display:inline-block; /* This cover full td */
background:url('/images/menubuttons.gif') no-repeat -140px 0px ; /*sets the background link image */
height:35px;
width:35px;
}
a.buddy:hover {
background:url('/images/menubuttons.gif') no-repeat -140px -35px ; /*sets the background link image and pushes the rest state up above view */
}
a.chat {
display:inline-block; /* This cover full td */
background:url('/images/menubuttons.gif') no-repeat -175px 0px ; /*sets the background link image */
height:35px;
width:35px;
}
a.chat:hover {
background:url('/images/menubuttons.gif') no-repeat -175px -35px ; /*sets the background link image and pushes the rest state up above view */
}
a.author {
display:inline-block; /* This cover full td */
background:url('/images/menubuttons.gif') no-repeat -210px 0px ; /*sets the background link image */
height:35px;
width:35px;
}
a.author:hover {
background:url('/images/menubuttons.gif') no-repeat -210px -35px ; /*sets the background link image and pushes the rest state up above view */
}
a.search {
display:inline-block; /* This cover full td */
background:url('/images/menubuttons.gif') no-repeat -245px 0px ; /*sets the background link image */
height:35px;
width:35px;
}
a.search:hover {
background:url('/images/menubuttons.gif') no-repeat -245px -35px ; /*sets the background link image and pushes the rest state up above view */
}
a.help {
display:inline-block; /* This cover full td */
background:url('/images/menubuttons.gif') no-repeat -280px 0px ; /*sets the background link image */
height:35px;
width:35px;
}
a.help:hover {
background:url('/images/menubuttons.gif') no-repeat -280px -35px ; /*sets the background link image and pushes the rest state up above view */
}
</style>
<?php
if (isset($_COOKIE["User_ID"]))
   echo "Welcome " . $_COOKIE["User_ID"] . "!<br>";
else {
   ?>
<div id="newuser" style="position:fixed;top:25%;left:20%;background-color:#FEEC65;">
<table border="4px" bordercolor="#220916"><tr><td width="50%">
<div style="background-color:#220916;height:1px;width:100%;"></div>
<div style="font-size:25PX;text-align:center;background-color:#227d4c;color:#220916">Log-In</div>
<div style="background-color:#220916;height:1px;width:100%;"></div><br />
<form action="login.php" method="post">
<div style="text-align:right;line-height:25px;">e-Mail: <input type="text" name="eMail">&nbsp;&nbsp;&nbsp;<br />
Password: <input type="password" name="Password">&nbsp;&nbsp;&nbsp;<br /><br />
<input type="submit" value="Log In">&nbsp;&nbsp;&nbsp;<br /></div>
</td><td width="50%"><br />
<div style="text-align:center;"><font style="font-size:25px;">Sign Up</font><br />
<font style="font-size:50px;">It&apos;s FREE!</font></div>
<div style="text-align:right;line-height:25px;"><form action="signup.php" method="post"><br />
e-Mail: <input type="text" name="eMail">&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;First Name: <input type="text" name="FirstName">&nbsp;&nbsp;&nbsp;<br />
Last Name: <input type="text" name="LastName">&nbsp;&nbsp;&nbsp;<br />
Password: <input type="password" name="Password">&nbsp;&nbsp;&nbsp;<br />
<input type="radio" name="Gender" value="male">Male&nbsp;&nbsp;&nbsp;
<input type="radio" name="Gender" value="female">Female&nbsp;&nbsp;&nbsp;<br />
Birth Mon: <input type="text" name="Month">&nbsp;&nbsp;&nbsp;<br />
Birth Day: <input type="text" name="Day">&nbsp;&nbsp;&nbsp;<br />
Birth Year: <input type="text" name="Year">&nbsp;&nbsp;&nbsp;<br /><br />
<input type="submit" value="Sign Up">&nbsp;&nbsp;&nbsp;
</form></div><br />
</td></tr></table>
</div>
<?php
;}  
?>
<div id="divPreHeader" style="position:fixed;width:1024px;height:12px;left:0px;top:0px;background-color:#FFFFFF;">
</div>
<div id="divHeader" style="position:fixed;width:1024px;height:50px;left:0px;top:12px;background-color:#FFFFFF;">
<a class="menublank0"></a>
<img src="images/header.gif" border=0 alt="Diamond Book Club" />
</div>
<div id="divPreMenu0" style="position:fixed;left:0px;top:62px;width:1024px;height:12px;background-color:#FFFFFF;">
</div>
<div id="divPreMenu1" style="position:fixed;width:1024px;height:3px;top:74px;left:0;background-color:#092315;">
</div>
<div id="divPreMenu2" style="position:fixed;width:1024px;height:1px;top:77px;left:0;background-color:#220916;">
</div>
<div id="divMenu" style="position:fixed;width:1024px;height:35px;top:78px;left:0;background-color:#227d4c;">
<a class="menublank1"></a>
<a class="home" href="/"></a>
<a class="menublank1"></a>
<a class="profile" href="/database_profile"></a>
<a class="menublank1"></a>
<a class="mail" href="/database_mail"></a>
<a class="menublank1"></a>
<a class="shop" href="/database_shop"></a>
<a class="menublank1"></a>
<a class="buddy" href="/database_buddy"></a>
<a class="menublank1"></a>
<a class="chat" href="/database_chat"></a>
<a class="menublank1"></a>
<a class="author" href="/database_author"></a>
<a class="menublank1"></a>
<a class="search" href="/database_search"></a>
<a class="menublank1"></a>
<a class="help" style="text-align:right;" href="/database_help"></a>
<a class="menublank1"></a>
</div>
<div id="divPostMenu1" style="position:fixed;width:1024px;height:1px;top:113px;left:0;background-color:#220916;">
</div>
<div id="divPreSubMenu0" style="position:fixed;left:12px;top:114px;width:3px;height:100%;background-color:#220916;">
</div>
<div id="divPreSubMenu1" style="position:fixed;left:15px;top:114px;width:1px;height:100%;background-color:#227d4c;">
</div>
<div id="divSubMenu" style="position:fixed;left:16px;top:114px;width:35px;height:100%;background-color:#7D2252;">
</div>
<div id="divPostSubMenu1" style="position:fixed;left:51px;top:114px;width:1px;height:100%;background-color:#092315;">
</div>
<div id="divMain" style="width:1024px;">
<div id="divPreContent" style="width:960px;height:114px;float:right;"">
</div>
<div id="divContent" style="width:960px;float:right;background-color:white;">

Setting Cookies after Sign Up (I removed all the echo with comment marts to prevent html from forming. It works and generates the array of cookies that should be created.)

<?php
$con = mysql_connect("localhost","****","****");
if (!$con){ die('Could not connect:  . mysql_error()'); }
mysql_select_db("****", $con);
/* echo "Connected to: " . $con; */
$eMail="$_POST[eMail]"; /* echo "<br>My eMail: " . $eMail; */
$eMailResult = mysql_query("SELECT * FROM eMail
WHERE eMail='$eMail'");
if (mysql_num_rows($eMailResult) == 0){
/* echo "<br>My eMail is Unique"; */}
else { /* echo "<br><b>eMail is already being used.</b> Request your password or choose a different eMail. "; */ exit(0);}
$User_Movement=rand(0,9999);
do {
$User_ID=rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9);
$User_IDResult = mysql_query("SELECT * FROM eMail
WHERE User_ID='$User_ID'");}
while (mysql_num_rows($User_IDResult) != 0);
/* echo "<br>User ID: " . $User_ID;
echo "<br>User ID is Unique"; */
$sql="INSERT INTO eMail (User_ID, eMail)
VALUES
('$User_ID','$_POST[eMail]')";

if (!mysql_query($sql,$con))
   {
   die('Error: ' . mysql_error());
   }
/* echo "<br>eMail Added"; */

$sql="INSERT INTO FirstName (User_ID, FirstName)
VALUES
('$User_ID','$_POST[FirstName]')";

if (!mysql_query($sql,$con))
   {
   die('Error: ' . mysql_error());
   }
/* echo "<br>First Name added"; */

$sql="INSERT INTO LastName (User_ID, LastName)
VALUES
('$User_ID','$_POST[LastName]')";

if (!mysql_query($sql,$con))
   {
   die('Error: ' . mysql_error());
   }
/* echo "<br>Last Name added"; */
 
$sql="INSERT INTO Gender (User_ID, Gender)
VALUES
('$User_ID','$_POST[Gender]')";

if (!mysql_query($sql,$con))
   {
   die('Error: ' . mysql_error());
   }
/* echo "<br>Gender added"; */

$sql="INSERT INTO Password (User_ID, Password)
VALUES
('$User_ID','$_POST[Password]')";

if (!mysql_query($sql,$con))
   {
   die('Error: ' . mysql_error());
   }
/* echo "<br>Password added"; */

$sql="INSERT INTO Birthday (User_ID, Month, Day, Year)
VALUES
('$User_ID','$_POST[Month]','$_POST[Day]','$_POST[Year]')";

if (!mysql_query($sql,$con))
   {
   die('Error: ' . mysql_error());
   }
/* echo "<br>Birthday added"; */
$sql="INSERT INTO Movement (User_ID, User_Movement)
VALUES
('$User_ID','$User_Movement')";

if (!mysql_query($sql,$con))
   {
   die('Error: ' . mysql_error());
   }
/* echo "<br>Movement added"; */
setcookie( "User_ID", $User_ID, time()+6000, "/", ".diamondbookclub.com" );
setcookie( "FirstName", $_POST['FirstName'], time()+6000, "/", ".diamondbookclub.com" );
setcookie( "LastName", $_POST['LastName'], time()+6000, "/", ".diamondbookclub.com" );
setcookie( "eMail", $_POST['eMail'], time()+6000, "/", ".diamondbookclub.com" );
setcookie( "Day", $_POST['Day'], time()+6000, "/", ".diamondbookclub.com" );
setcookie( "Month", $_POST['Month'], time()+6000, "/", ".diamondbookclub.com" );
setcookie( "Year", $_POST['Year'], time()+6000, "/", ".diamondbookclub.com" );
setcookie( "Gender", $_POST['Gender'], time()+6000, "/", ".diamondbookclub.com" );
print_r($_COOKIE);
mysql_close($con);
?>


#2 requinix

requinix

    Transforming Moderator

  • Moderators
  • 6,151 posts
  • LocationWA

Posted 06 December 2012 - 09:09 PM

Calling setcookie() does not make the values appear in $_COOKIE automatically. They'll take effect on the next page load.

If you need them immediately then you can make your own setcookie()-type function which calls setcookie() and also sets the value in $_COOKIE, like
function savecookie($name, $value, $expire = 0, $path = null, $domain = null, $secure = false, $httponly = false) {
    if (setcookie($name, $value, $expire, $path, $domain, $secure, $httponly)) {
        if ($value === false || $expire < time()) {
            unset($_COOKIE[$name]);
        } else {
            $_COOKIE[$name] = $value;
        }
        return true;
    } else {
        return false;
    }
}

Edited by requinix, 06 December 2012 - 09:10 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com