trecool999 Posted October 6, 2008 Share Posted October 6, 2008 Hi guys. I have a few years of experience behind me in PHP, MySQL and other languages but there is this one cookie problem I can't get my head around. If you need it, my website is here: http://web-share.org.uk/ Every time I set a cookie, I know it is there because I can check what cookies are set in Firefox. The problem I've been having is that I can't get PHP to recognise it whenever I come back! Every time I visit my site it tells me that no cookie existed, even though I know damn well it has been set because Firefox tells me it has! Please help! If you need the code, I probably won't be able to give it to you as it is set in many different files (It's a template, source, index and functions based website because I'm making a CMS). *UPDATE*: Whenever I click another menu item after closing the browser and opening again, then it tells me there was a cookie... WTF!? Quote Link to comment Share on other sites More sharing options...
revraz Posted October 6, 2008 Share Posted October 6, 2008 Is the cookie set with www.web-share.org.uk or just web-share.org.uk? Quote Link to comment Share on other sites More sharing options...
Orio Posted October 6, 2008 Share Posted October 6, 2008 From php.net: Common Pitfalls: [*] Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. Expire time is set via the expire parameter. A nice way to debug the existence of cookies is by simply calling print_r($_COOKIE);. You're probably checking on the same page you're setting the cookie. Orio. Quote Link to comment Share on other sites More sharing options...
trecool999 Posted October 6, 2008 Author Share Posted October 6, 2008 From php.net: Common Pitfalls: [*] Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. Expire time is set via the expire parameter. A nice way to debug the existence of cookies is by simply calling print_r($_COOKIE);. Nope. I log in, which sets the cookie, then close the browser, load up web-share.org.uk and then navigate to another page, which only then displays the cookie info I ask it to. You're probably checking on the same page you're setting the cookie. Orio. Quote Link to comment Share on other sites More sharing options...
revraz Posted October 6, 2008 Share Posted October 6, 2008 Are you using relative or absolute links in your pages to other pages? Quote Link to comment Share on other sites More sharing options...
trecool999 Posted October 6, 2008 Author Share Posted October 6, 2008 Are you using relative or absolute links in your pages to other pages? Absolute. Quote Link to comment Share on other sites More sharing options...
revraz Posted October 6, 2008 Share Posted October 6, 2008 Use relative instead, you are probably changing domain names. Quote Link to comment Share on other sites More sharing options...
trecool999 Posted October 6, 2008 Author Share Posted October 6, 2008 No domain name changes and it doesn't work with relative anyway. Besides, it stays with the domain defined in it's settings.php file. Quote Link to comment Share on other sites More sharing options...
revraz Posted October 6, 2008 Share Posted October 6, 2008 Why wouldn't it work with relative? Post your Set code and your check code. The only way it would work somewhere and not elsewhere is because you are crossing domains. Quote Link to comment Share on other sites More sharing options...
trecool999 Posted October 6, 2008 Author Share Posted October 6, 2008 I'm not sure why it doesn't. Things like images and links would redirect to non-existant places which was really annoying. Saying that, what would changing the links have anything to do with receiving the cookies anyway? Quote Link to comment Share on other sites More sharing options...
revraz Posted October 6, 2008 Share Posted October 6, 2008 Because to me it sounds like you are setting the cookie on one domain, and by using a absolute path in your links, you are switching domains. Try it with relative path just to see if the cookie works. If not, post the code where you set the cookie along with the page name, and post the code where you check the cookie and that page name. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 6, 2008 Share Posted October 6, 2008 Are you sure you aren't setting a cookie on like, http://test.com and then going to http://www.test.com? That CAN make a difference, but you can fix it by putting a . in front of the domain name when setting the cookie, which makes it include all subdomains. Example: .test.com Quote Link to comment Share on other sites More sharing options...
trecool999 Posted October 6, 2008 Author Share Posted October 6, 2008 I'm not really setting it with the URL included. I'm just doing a straight-forward setcookie(). Either way, it's called the same thing ($_COOKIE['WebShareCookie']) and Firefox sees it as the same cookie. I'm going to make this easier for myself (if this is possible): How can I save the session id to a cookie and then load the same session using the id the cookie has? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 6, 2008 Share Posted October 6, 2008 Try explicitly setting the domain parameter with the . included. Quote Link to comment Share on other sites More sharing options...
trecool999 Posted October 6, 2008 Author Share Posted October 6, 2008 Reply above pls. Quote Link to comment Share on other sites More sharing options...
revraz Posted October 6, 2008 Share Posted October 6, 2008 You are if you don't tell it otherwise. You are not considering any of the advice given, so I'll leave you with someone else to help you. I'm not really setting it with the URL included. I'm just doing a straight-forward setcookie(). Quote Link to comment Share on other sites More sharing options...
trecool999 Posted October 6, 2008 Author Share Posted October 6, 2008 I'll set the domain then. But can somewhere answer my newwest question pls? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 6, 2008 Share Posted October 6, 2008 Uhh, you'd just use the session_start() function...because it sets a cookie and reads the sessid by itself. Quote Link to comment Share on other sites More sharing options...
trecool999 Posted October 6, 2008 Author Share Posted October 6, 2008 Doesn't it get mixed with other page PHPSESSID's? :S Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 6, 2008 Share Posted October 6, 2008 Doesn't it get mixed with other page PHPSESSID's? :S No, each domain has its own cookies, so you can be on PHPFreaks and have a session, and you can be on Facebook and have your own session too. Quote Link to comment Share on other sites More sharing options...
trecool999 Posted October 6, 2008 Author Share Posted October 6, 2008 Still not working... Every time I reopen the browser, it deletes the cookie and starts a new session... How do I save the session cookie? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 6, 2008 Share Posted October 6, 2008 Session cookies don't persist between browser closes/openings unless you explicitly set a cookie expiry time. Quote Link to comment Share on other sites More sharing options...
trecool999 Posted October 6, 2008 Author Share Posted October 6, 2008 I did. 0 . G2g anyway. See you guys tomorrow, hopefully when I will get over this problem. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 6, 2008 Share Posted October 6, 2008 0 means that it gets deleted when the browser closes. Quote Link to comment Share on other sites More sharing options...
trecool999 Posted October 7, 2008 Author Share Posted October 7, 2008 Am I right in thinking setting it to a date in the past would work? Quote Link to comment 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.