setting & 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
nightman
Forum Newbie
Posts: 6
Joined: Wed Jul 04, 2007 10:29 am

setting & reading cookies

Post by nightman »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Trouble setting a cookie

Dear all

I am trying to set cookies using different arguments,  at the moment one works and the other doesn't.  I have 'cookie 1' sitting at the document root 'htdocs' of apache 1.3. and 'cookie 2' in a file named 'yourdomain.com' in htdocs.  I cannot figure why cookie 1
works and cookie 2 doesn't:


Cookie 1 part 1:

Code: Select all

<?php
$Month = 2592000 + time();
//this adds 30 days to the current time
setcookie(AboutVisit, date("F jS - g:i a"), $Month);
?>

Cookie 1 part 2:

Code: Select all

<?php
if(isset($_COOKIE['AboutVisit']))
{
$last = $_COOKIE['AboutVisit'];
echo "Welcome back! <br> You last visited on ". $last;
}
else
{
echo "Welcome to our site!";
}
?>

----------------------


Cookie 2:

Code: Select all

<?php
setcookie("color", "black", time()+3600, "/", "yourdomain.com", 0);
?>
<html>
<head>
<title>cookie with if statement</title>
</head>
<body>
<?php
if (isset($_COOKIE[vegetable])) {
print "<p>Hello again, your chosen vegetable is $_COOKIE[vegetable]</p>";
} else {
print "<p>Hello you. This may be your first visit</p>";
}
?>
</body>
</html>

From the code can anyone see why cookie 2 does not work or does anyone know of any tutorials that list multiple ways for setting, reading and manipulating with cookies?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

if (isset($_COOKIE[vegetable])) { is supposed to be if (isset($_COOKIE[color])) {

http://php.net is a good tutorial.
nightman
Forum Newbie
Posts: 6
Joined: Wed Jul 04, 2007 10:29 am

PHP Tutorials

Post by nightman »

Hello thanks for pointing that out, I changed the values back but the same problem persisted. I have looked over at php.net for a listing on cookies it helped to outline the parameters that work with setcookie(), http://uk3.php.net/manual/en/function.setcookie.php

Regarding cookie 2 it works with the parameters: 'name' 'value' 'expire' and 'path'

but not with: 'domain', 'secure' and 'httponly'.

When I specify a domain and set the bools for 'secure' and 'httponly' (true and false) no cookie gets set.

When specifying a domain does it have to be a fully qualified one, or can it be a folder named, 'example.com'? or '.example.com' under document root?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

How does the domain value compare to what the browser sees as the domain? Secure will only work if you are running over SSL.
nightman
Forum Newbie
Posts: 6
Joined: Wed Jul 04, 2007 10:29 am

Reply

Post by nightman »

Thanks for that I am not running over SSL. When I am able to set cookies in the browser they belong to 'localhost'. Though when I specify localhost as the "domain" the same problem still persists. For example if I have a file named 'ck_2.php' and I typed in the browser url:

http://localhost/ck_2.php /*the url to the left is not a valid one*/

if ck_2.php has the values below:

setcookie("car", "ford", date(), "/", "localhost", 0) // ck_2.php will not work

setcookie("car", "ford", date(), "/", "/localhost/", 0) // ck_2.php will not work

setcookie("car", "ford", date(), "/", "/localhost.com/", 0) // ck_2.php will not work

setcookie("car", "ford", date(), "/", "/.localhost.com/", 0) // ck_2.php will not work

setcookie("car", "ford", date(), "/", "/mydomain.com/", 0) // ck_2.php will not work, mydomain.com is a folder named mydomain.com under htdocs

but if it had these values:

setcookie("car", "ford", date(), "/") // ck_2.php will work

Whenever I include a domain argument the script balks. Is the domain parameter exclusive to SSL in a situation where you can only have a domain parameter if SSL is being used?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You don't need to specify a domain when working with localhost. The only time you need to specify a domain is if you want the cookie to have a different visibility in-the-wild. For example "www.your.site" could have other subdomains like "forums.your.site" and "news.your.site." If you wanted to have access to the cookie in all three, the domain would need to be set to ".your.site" note the leading dot.
nightman
Forum Newbie
Posts: 6
Joined: Wed Jul 04, 2007 10:29 am

nice one

Post by nightman »

got it in one - cheers!
Post Reply