Reading cookies

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Altec88
Forum Newbie
Posts: 7
Joined: Mon Feb 20, 2006 8:53 pm

Reading cookies

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

have you set cookies?
Altec88
Forum Newbie
Posts: 7
Joined: Mon Feb 20, 2006 8:53 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you can't enumerate the cookies unless you have some to enumerate ;)
Altec88
Forum Newbie
Posts: 7
Joined: Mon Feb 20, 2006 8:53 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Altec88
Forum Newbie
Posts: 7
Joined: Mon Feb 20, 2006 8:53 pm

Post 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 :(
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What for? Post what you've got so far that sets and reads the cookies. We can nudge you from there.
Altec88
Forum Newbie
Posts: 7
Joined: Mon Feb 20, 2006 8:53 pm

Post 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 :)
Altec88
Forum Newbie
Posts: 7
Joined: Mon Feb 20, 2006 8:53 pm

Post 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.
Post Reply