Page 1 of 1

Reading cookie values?

Posted: Sun Dec 12, 2004 5:19 pm
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.

Posted: Sun Dec 12, 2004 5:31 pm
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.

Posted: Sun Dec 12, 2004 6:26 pm
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.

Posted: Mon Dec 13, 2004 7:22 pm
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.

Posted: Mon Dec 13, 2004 7:40 pm
by rehfeld
line 4, you forgot ;