tags:

views:

107

answers:

4

I have this weird problem with setting up cookies with PHP. Everything worked fine until this morning when i uploaded my script to the server.

Here is some of my code

if (!isset($_COOKIE["loggedin"])){
show login form } else { show content }

This is in my login page

setcookie("loggedin", "true", time()+3600,"/","mydomain.com");

I know using cookies for logins isn't the best practice but it's a dummy site. Any who... 'till this morning, the cookies worked properly, but now they won't. I had a similar issue before, but i can't remember how i solved it :(

Any help is very appreciated!

+1  A: 

Is your oven hot enough?

... ahem

Check for warnings. Are you emitting content before the headers? If so, you'll either want to move your setcookie() higher or use output buffering.

Try a

var_export($_COOKIE);

to see if anything's actually there.

Check to see if the cookie is being set to begin with. There are many ways to watch the HTTP headers as they come down. If you use Firefox, try the Live HTTP Headers extension.

Charles
i get this "array()" i added the export just right after the setcookie function ..
Aviatrix
You'll absolutely want to check for warnings and check the incoming headers then.
Charles
A: 

Check if your browser's blocking the cookies. If it worked before, and the problem started when you put it on a different server, that's probably the most likely cause.

jimyi
no , my browser isn't blocking cookies ..and i dont have a whole bunch of servers to test :/
Aviatrix
A: 

Check your browser's cookies on your domain. If you are using Firefox, get a web developer extension like Firebug with FireCookie. Or if you're using IE, get a IE Cookie Viewer

try dropping the path and domain parameter:

setcookie("loggedin", "true", time()+3600);

it might just help. I had similar problems before, dropping them allows my app to work - weirdly.

thephpdeveloper
i tried droping the domain and path parameter .. still nothing .. i got pisd off and uses sessions instead ..
Aviatrix
+1  A: 

I've had this problem too. I've found that the cookie kept in the browser depends on BOTH the browser AND the server. Check the cookie being set from the browser:


Internet Explorer 7 and 8

Tools > Internet Options > (General Tab) > Browsing History > Settings > View Files

Firefox

Tools > Options > Privacy > Show Cookies


The array() response suggests that more than one cookie is being set.

Michelle
i triple checked my code but there is only 1 place where the cookie is set ..
Aviatrix