c_pattle Posted February 14, 2011 Share Posted February 14, 2011 I'm having trouble setting a cookie in php. When a user logs in I send an ajax request to an file which contains this code to set the cookie setcookie("userCookie", $_SESSION['username'], time()+60*60*24*30); However when I include the following code to check to see if the cookie has been set is says is hasn't if(isset($_COOKIE['userCookie'])) { echo("cookie set"); } else { echo("cookie not set"); } I check to see that my browser is accepting cookies and it definitely is so I'm not sure what causing the problem. Thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/227661-php-cookie-problem/ Share on other sites More sharing options...
ManiacDan Posted February 14, 2011 Share Posted February 14, 2011 1) Are you sure that variable you're setting exists? If it's FALSE or 0, it won't go through. 2) $_COOKIE is only populated on subsequent loads of the page. Set the cookie, visit a new page, cookies are there. 3) Are you seeing the cookies in your browser? Install the "view cookies" plugin for firefox, then view the cookies in the page information dialog (CTRL+I [i for Information]) -Dan Quote Link to comment https://forums.phpfreaks.com/topic/227661-php-cookie-problem/#findComment-1174231 Share on other sites More sharing options...
c_pattle Posted February 14, 2011 Author Share Posted February 14, 2011 Thanks, yeah I am sure the variable exists. I think the problem is the fact that it's located in an external file. When I copied the same code into my main index page it works fine but not when it's in another file. Quote Link to comment https://forums.phpfreaks.com/topic/227661-php-cookie-problem/#findComment-1174237 Share on other sites More sharing options...
johnny86 Posted February 14, 2011 Share Posted February 14, 2011 If you are trying to access the cookie from a different folder than where it's set, it might not be available. You should specify your cookie options in the setcookie statement. setcookie ("myCookie", "myValue", time()+60*60*24*30, '/', '.domain.com'); '/' makes it available on all directories under your domain.com .domain.com makes the cookie available to all subdomains.. if you specify www.domain.com then no subdomains are included, only the default subdomain which is www. Try those and if you can access it after that.. Be more specific of where you need the cookie. Those are instructions for the clients browser on when to send the cookie to you with their http request. Quote Link to comment https://forums.phpfreaks.com/topic/227661-php-cookie-problem/#findComment-1174245 Share on other sites More sharing options...
ManiacDan Posted February 14, 2011 Share Posted February 14, 2011 When I copied the same code into my main index page it works fine but not when it's in another file. PHP code can't simply be copied and pasted from one document to another and retain its values. I bet it's not working when you think it is. echo "COOKIE SET TO: {$_SESSION['username']}"; I bet it's not there. Quote Link to comment https://forums.phpfreaks.com/topic/227661-php-cookie-problem/#findComment-1174251 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.