Reading cookie values?

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
Antizero
Forum Newbie
Posts: 3
Joined: Sun Dec 12, 2004 4:45 pm

Reading cookie values?

Post by Antizero »

I'm writing a MySQL user system as part of administration of a larger script. My problem here is setting a cookie or using sessions to make sure that the user can only view the 'protected' pages once they have logged in.
I know how to set cookies, but I do not know how to read cookies and can't seem to find any instructions on how to.

I'm guessing its somewhere along the lines of

Code: Select all

<?php
$page = include("path/to/the/content.php");
$token = $_COOKIE["signedin"];
$cookie_on = "yes"

if ($token==$cookie_on)
{
echo $page;
} else {
header("Location:http://nobodycares.info/xmasfaces/admin/index.php");
}
?>
Would using sessions be a better bet? I'd preferably use sessions but I'm a little intimidated.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Its simple:

Code: Select all

echo $_COOKIE['signedin'];
Etc.

Note that this isnt a very secure way to check to see if a user is valid as its an easy task to forge a cookie to say 'yes'. You need it to store something that cannot be guessed and that cna be checked against something secret that you hold in a database or file.
Antizero
Forum Newbie
Posts: 3
Joined: Sun Dec 12, 2004 4:45 pm

Post by Antizero »

Yeah, I didn't intend for the value of the cookie to be merely "yes" but probably an encrypted form of the user's password. Thanks for your help.
Antizero
Forum Newbie
Posts: 3
Joined: Sun Dec 12, 2004 4:45 pm

Post by Antizero »

I'm having another problem with the script I'm trying to write...

Code: Select all

<?php
#$page = include("path/to/the/content.php");
$token = $_COOKIE["signedin"];
$cookie_on = "647888788545421211111164"

if ($signedin == $cookie_on) {
echo "Working!"; #$page;
} else {
header("Location: http://nobodycares.info/xmasfaces/admin/index.html");
}
?>
The cookie is already set with the correct name and value... but this script spits out the following error:
Parse error: parse error, unexpected T_IF in /home/nobodyca/public_html/xmasfaces/admin/cpanel.php on line 6
I'm really confused because I dont see anything wrong with the syntax of the if statement.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

line 4, you forgot ;
Post Reply