简体   繁体   中英

My cookies won't bake properly

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!

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM