Page 1 of 1
Reading cookies
Posted: Mon Mar 06, 2006 3:44 pm
by Altec88
Hello,
I'm trying to set up a user name/password setup to my site. The username/password system works now.
BUT now, I need cookies.
I'm having problems trying to make it so when you go to the homepage, it tells you if your logged in or not in the upper right corner. The cookies are called register and login.
I found this code for reading cookies, is it right? How do I integrate it with my cookies/info?
Code: Select all
<?php
foreach ( $_COOKIE as $key => $value )
print ( "<tr>
<td> $key </td>
<td> $value </td>
</tr>" );
?>
Where do I go from here? I'm lost.......
Thanks!
Posted: Mon Mar 06, 2006 4:50 pm
by feyd
have you set cookies?
Posted: Mon Mar 06, 2006 4:55 pm
by Altec88
feyd wrote:have you set cookies?
No, that's the 2nd and 3rd setup (for my class)
Why, do I have to do that first?
Posted: Mon Mar 06, 2006 5:11 pm
by feyd
you can't enumerate the cookies unless you have some to enumerate

Posted: Mon Mar 06, 2006 5:18 pm
by Altec88
feyd wrote:you can't enumerate the cookies unless you have some to enumerate

Well i'm not putting any files on the web until they're all done. Doesn't that make it ok? Sorry, i'm new.
Posted: Mon Mar 06, 2006 5:37 pm
by feyd
it doesn't have to be "on the web" to be able to set cookies. So long as you, yourself, can access it via a browser, you can transmit a cookie.
Posted: Mon Mar 06, 2006 5:47 pm
by Altec88
feyd wrote:it doesn't have to be "on the web" to be able to set cookies. So long as you, yourself, can access it via a browser, you can transmit a cookie.
I give up, theres no hope

Posted: Mon Mar 06, 2006 5:52 pm
by feyd
What for? Post what you've got so far that sets and reads the cookies. We can nudge you from there.
Posted: Mon Mar 06, 2006 6:25 pm
by Altec88
feyd wrote:What for? Post what you've got so far that sets and reads the cookies. We can nudge you from there.
Ok give me a minute

Posted: Mon Mar 06, 2006 6:34 pm
by Altec88
My login page: (login.php)
Code: Select all
<"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmls = "http://www.w3.org/1999/xhtml">
<head>
<title>Login Page</Title>
<link rel=stylesheet href="default.css" type="text/css">
<?php
$log = $_cookie['login'];
if (($log != "") && ($survey == "yes"))
{
header('location: surveyform.php?survey=yes&loggedin=yes');
}
?>
</head>
<body>
<p>
<h2>Login:</h2>
</p>
<form action = "surveyform.php" method = "post">
<p>
Username:
<input type="text" name="username">
</p>
<p>
Password:
<input type="password" name="password">
</p>
<p>
<input type="submit" value="Submit">
</p>
<?php
if ($survey=="yes")
print "<input type='hidden' name='survey' value='yes'>";
?>
<p><a href="register.html"><h2>Register Today!</h2></a></p>
Now, this script is the one I need to set the cookies: (surveyform.php)
Code: Select all
<?php
$loggedIn = false;
$file = fopen("members.dat", "r");
if(!feof($file))
{
$fileContents = fread($file, filesize("members.dat"));
$loginPairs = explode("\n", $fileContents);
foreach($loginPairs as $loginPair)
{
if(strstr($loginPair, $_POST["username"]))
{
$loginInfo = explode("|", $loginPair);
if($loginInfo[1] == $_POST["password"])
{
$loggedIn = true;
break;
}
}
}
if(!$loggedIn)
{
echo "<h2>The credentials you supplied could not be
found.</h2>";
die();
}
}
else
{
echo "Could not open <b>members.dat</b> for reading!";
}
?>
I have to check the value of $loggedin. If it's not equal to "yes", you need to set two cookies:
1.) Name register, value of firstname, and expiration of 1 week
2.) Name login, value of firstname, and expiration of 1 hour.
I then have to check the value of $survey. If its not equal to "yes", you should redirect the user to the welcome page.