PDA

View Full Version : cookie not setting correctly


skinner927
10-11-2006, 04:20 AM
Ok, i'm trying to creat a cookie with these paramaters

setcookie("cookieid", "$random", time()+86400, '/', 'localhost');

the thing is, it doesn't work but if i put it in like this:

setcookie("cookieid", "$random", time()+86400, '/');

it works fine and sets the host as localhost EXACTLY as I've spelt it, whats going on here? :confused:

mlseim
10-11-2006, 01:26 PM
setcookie("cookieid", "$random", time()+86400, '/');

You're saving "cookieid" as "$random" (with the dollar sign), not the variable.
... by putting the variable in quotes.

Try this:

setcookie("cookieid", $random, time()+86400, '/');


.

CFMaBiSmAd
10-11-2006, 02:15 PM
Because the variable $random is enclosed in double-quotes, it gets parsed and replaced with its value. If they were single-quotes, it probably would not work.

The reason that the cookie does not work when the domain parameter is set to 'localhost' is that localhost is not a domain. It is a WINS name that resolves to the 127.0.0.1 loop back IP address, referring to the local computer.

skinner927
10-11-2006, 10:34 PM
first, I dont belive this works:
setcookie("cookieid", $random, time()+86400, '/');

as value has to be a string and sending in a number will produce an error, second:

If i don't set the cookies domain, when I check it, it responds with a domain of localhost

Any ideas?

GJay
10-11-2006, 10:55 PM
Only hosts within the specified domain can set a cookie for a domain and domains must have at least two (2) or three (3) periods in them to prevent domains of the form: ".com", ".edu", and "va.us"


why do you need to specify the domain anyway? if it defaults to the correct thing, then there's no problem, surely?

CFMaBiSmAd
10-11-2006, 11:42 PM
The version without any quotes surrounding $random works (just verified) because PHP always does a good job of converting things to strings when a string is needed.

The localhost/ value that you see in the cookie file is (from the PHP manual) "The path on the server in which the cookie will be available on." This does not have anything to do with a domain, which localhost is not.

skinner927
10-12-2006, 04:18 AM
I dont know if you read that wrong but thats not totaly correct. you can setup the domain because if you check the php manual right at the top it says that you should set the domain to .yourdomain.com without the www that way it works on all subdomains (www and others) of your website.

the path is what folders it works in, if you select / for your path, it works everywhere on your website.

and localhost is the domain because if you're running the file locally what else could be the domain, it cant be blank or else it would never be used.

skinner927
10-12-2006, 04:21 AM
why do you need to specify the domain anyway? if it defaults to the correct thing, then there's no problem, surely?

so it can work with sub domains. for example if the cookie is set at www.yourdomain.com it wont work at images.yourdomain.com

GJay
10-12-2006, 08:04 AM
The localhost/ value that you see in the cookie file is (from the PHP manual) "The path on the server in which the cookie will be available on." This does not have anything to do with a domain, which localhost is not.

You're quoting the wrong bit of the manual.
path is the 4th argument, domain is the 5th.

The part of the cookie-spec I posted wasn't for fun, it's telling you how the domain argument works.

The easiest solution would be to either get some web-hosting, and develop there, or learn how to setup vhosts in apache, so that you can point a domain-name of your choosing at localhost.

CFMaBiSmAd
10-12-2006, 08:32 AM
No I quoted the part I intended, because - Skinner927 stated in his 1st post that - "sets the host as localhost EXACTLY" The only place he could be getting this statement from is looking in the actual cookie file, where when he used the second form that worked, with the 4th parameter of '/', that this sets the path to localhost/ and is somehow confusing this to mean that the localhost portion of this is a domain, which he has been told it is not.

I quoted the path parameter definition as a possible reason why someone might attempt to interpret what they saw as something that it is not.

Edit: Let me clarify a little more. Doing the following using http://localhost/somefile.php -
$random = 123;
setcookie("cookieid", "$random", time()+86400, '/');
Causes a cookie file with the following -
cookieid
123
localhost/
1024
3588502400
29814350
2885208896
29814149
*Someone could interpret the path portion of this to reinforce the idea that localhost is a domain.

skinner927
10-12-2006, 09:50 PM
Ok, I think i understand what you're saying now. I just wont deal with it until this goes live. I'm currently developing it on my computer instead of the server because its easier. i'll deal with the cookies at a later time.

thanks for the help.