Page 1 of 1
Using Cookies
Posted: Sat Jun 03, 2006 9:04 am
by a94060
Hi all,
I am not really sure how to use cookies still. I read up on the setcookie and $_COOKIES array. This is what i have:
admin page
Code: Select all
<?PHP
$you = $_POST['you'];
$me = $_POST['me'];
if(($you == '***NO NEED***') && ($me == '***no NEED***')) {
setcookie('in','TRUE');
echo 'You are in,please go <a href="***no need***">Here</a>';
}
else{
header("Location: ***NONE***");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Logging In</title>
</head>
<body>
</body>
</html>
I try to read the cookie like this : i also printed out what is going in to the array with print_r($_COOKIES) which tells me :
"Array ( [PHPSESSID] => 9876bfe982aec20028ca3f6ed61daceb )" But only after i refresh that page(does it come ebcause of session start)
Code: Select all
<?PHP
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Main Admin Page</title>
</head>
<body>
<?PHP
print_r($_COOKIE);
if($_COOKIE['in'] != 'TRUE') {
echo 'You are not logged in,please go to <a href="***lalal***">Here</a> to login';
}
Any help?
Posted: Sat Jun 03, 2006 10:12 am
by ambivalent
You're missing some arguments for setcookie, try it like this:
Code: Select all
setcookie('in', 'TRUE', time()+3600, "/");
That should set a cookie with the name "in" and a value of "TRUE", and an expiry time 1 hour in the future. Note that 'TRUE' is just a word and not a boolean value. You could also do the following:
Code: Select all
setcookie('in', TRUE, time()+3600, "/");
In this case, your cookie name 'in' would have a value of 1.
If you're using Firefox, get the
Web Developer extension. Among other things, you can view cookies and their values immediately as they are set by a given page.
Posted: Sat Jun 03, 2006 11:26 am
by a94060
ok,thanks man. so to acces it on another page,i would just use the $_COOKIES['in'] in order to check it? and if i still use the value true, i would do osmething like this:
Code: Select all
if($_COOKIE['in'] == '1') {
/*Do what i want here*/
}
else {
echo 'You are not logged in';
}
like that?
Posted: Sat Jun 03, 2006 12:56 pm
by ambivalent
That should work, give it a try. I think I'd go about it like this:
Code: Select all
if(isset($_COOKIE['in']) && ($_COOKIE['in'] == true))
{
echo "You're in";
}
else
{
echo "Nope";
}
Posted: Sat Jun 03, 2006 1:24 pm
by a94060
you had told me that TRUE=1. so i tried it like this in my scripts :
Code: Select all
if($_COOKIE['in'] == 1 ) {
echo '<font color=green>You are logged in,please go <a href="http://avi.aerohostale.com/admin/main.php">Here</a> to see the main page.</font>';
}
else {
this is the set part im using
Code: Select all
if(($you == '***') && ($me == '***')) {
setcookie('in',TRUE,time()+3600,"/");
echo 'You are in,please go <a href="http://avi.aerohostale.com/admin/main.php">Here</a>';
}
this is how im tryin and the print_r($_COOKIE) is still only givin nothing now because i removed using sessions. I login,my script says im in,then i clik the main link and it says im not in.The webdeveloper util tells me this :
Name in
Value 1
Host xxx.yyy.zzzzzz.com
Path /admin
Secure No
Expires Saturday, June 03, 2006 3:24:30 PM
Posted: Sat Jun 03, 2006 3:13 pm
by ambivalent
a94060 wrote:you had told me that TRUE=1. so i tried it like this in my scripts :
Code: Select all
if($_COOKIE['in'] == 1 ) {
echo '<font color=green>You are logged in,please go <a href="http://avi.aerohostale.com/admin/main.php">Here</a> to see the main page.</font>';
}
That's fine, checking for TRUE or 1 makes no real difference since they are both "true" in this case.
a94060 wrote:
this is the set part im using
Code: Select all
if(($you == '***') && ($me == '***')) {
setcookie('in',TRUE,time()+3600,"/");
echo 'You are in,please go <a href="http://avi.aerohostale.com/admin/main.php">Here</a>';
}
this is how im tryin and the print_r($_COOKIE) is still only givin nothing now because i removed using sessions. I login,my script says im in,then i clik the main link and it says im not in.The webdeveloper util tells me this :
Name in
Value 1
Host xxx.yyy.zzzzzz.com
Path /admin
Secure No
Expires Saturday, June 03, 2006 3:24:30 PM
All of the details provided by the utility appear to be correct and the cookie is properly set. Where is print_r($_COOKIE) being called? If it's on the page which initially set the cookie, it will be empty until a new page request is made (or forced with header()). What's the code at /admin/main.php?
This is what it seems to me you are attempting:
Code: Select all
// if(($you == '***') && ($me == '***')) blah blah
//assume password has been validated, set the cookie and redirect.
setcookie('in', TRUE, time()+3600, "/");
header("Location: http://domain/admin.php");
Code: Select all
admin.php:
if(isset($_COOKIE['in']) && ($_COOKIE['in'] == true))
{
echo "Secret content";
}
else
{
echo "Go away";
}
echo "<pre>";
print_r($_COOKIE);
echo "</pre>";
Posted: Sat Jun 03, 2006 4:03 pm
by a94060
here is the main.php file
Code: Select all
<?PHP
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Main Admin Page</title>
</head>
<body>
<?PHP
print_r($_COOKIE);
if($_COOKIE['in'] != 'TRUE') {
echo 'You are not logged in,please go to <a href="***">Here</a> to login';
}
else {
echo 'Welcome to the admin page. This is the basic layout so far.<br>Please use one of the links below to do what you would like to.';
echo '<br>';
echo '<br>';
echo '<a href="***">Add An Offer</a>';
echo '<br>';
echo '<a href="***">View Pending and Completed Offers</a>';
echo '<br>';
echo '<a href="***">Make a Payment to Someone</a>';
//}
?>
</body>
</html>
here is the index.php file
Code: Select all
<?PHP
$you = $_POST['you'];
$me = $_POST['me'];
if(($you == 'kevin') && ($me == 'hockey')) {
setcookie('in','TRUE');
echo 'You are in,please go <a href="***">Here</a>';
}
else{
header("Location: ***");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Logging In</title>
</head>
<body>
</body>
</html>
Posted: Sun Jun 04, 2006 12:35 pm
by ambivalent
So is there still a problem? BTW, your setcookie() is still missing arguments.
Read more on cookies
here and
here.
Posted: Sun Jun 04, 2006 12:54 pm
by a94060
i have read about the set cookie command ill read that other link at hudzilla
Posted: Sun Jun 04, 2006 2:45 pm
by serban
i don't really understand why you used that "session_start();" at the beginneing... but since you used it, you might as well use sessions for your auth: #_SESSION['in']=true;
seems more efficient to me