Doctor_Strop Posted July 6, 2007 Share Posted July 6, 2007 Hi all, I'm a noob with PHP, but I've done a fair amount of reading. I am trying to set and get cookies on my site so I can alter the language, and the user will still have the language set when they surf back in. I can manage to set the cookie in a file as follows... <?php $language = $_GET['language']; $refPage = "http://bfhq.co.uk/"; $name ="BFHQlanguageselect"; $expiry = 60 * 60 * 24 * 180 + time(); $path = "../languages/".$language; $domain = "bfhq.co.uk"; if (is_dir($path)) { if (opendir($path)) { setcookie($name,$language,$expiry); if (isset($_COOKIE[$name])) { header('Location: '. $refPage); } } } ?> but when I try and call it again nothing happens and no cookies are found. See my code below for calling the cookie... <?php $name ="BFHQlanguageselect"; //check cookie even exists print_r($_COOKIE); if (isset($_COOKIE[$name])) { $language = $_COOKIE[$name]; } else { $language = "english"; } ?> This is doing my head in as from what I have read this should work. My server is set up to read PHP 4 and 5. Regards Andy > Quote Link to comment https://forums.phpfreaks.com/topic/58787-solved-cookies/ Share on other sites More sharing options...
pocobueno1388 Posted July 7, 2007 Share Posted July 7, 2007 Try this: <?php if (isset($_COOKIE['BFHQlanguageselect'])) { $language = $_COOKIE['BFHQlanguageselect']; } else { $language = "english"; } I did that just in case your $name variable was out of scope. Quote Link to comment https://forums.phpfreaks.com/topic/58787-solved-cookies/#findComment-291676 Share on other sites More sharing options...
phpknight Posted July 7, 2007 Share Posted July 7, 2007 If this is in the same script, you won't be able to read the cookie until the next time the page is reloaded. So if you try to set a cookie and then read it again, you won't get a result. If that is indeed your problem, let me know. I have a workaround for it. Quote Link to comment https://forums.phpfreaks.com/topic/58787-solved-cookies/#findComment-291713 Share on other sites More sharing options...
Doctor_Strop Posted July 7, 2007 Author Share Posted July 7, 2007 Try this: <?php if (isset($_COOKIE['BFHQlanguageselect'])) { $language = $_COOKIE['BFHQlanguageselect']; } else { $language = "english"; } I did that just in case your $name variable was out of scope. I tried that and unfortunately it still does not show any cookie. It's almost as if the cookie is getting deleted as soon as the script opens the home page. Quote Link to comment https://forums.phpfreaks.com/topic/58787-solved-cookies/#findComment-292085 Share on other sites More sharing options...
Doctor_Strop Posted July 7, 2007 Author Share Posted July 7, 2007 If this is in the same script, you won't be able to read the cookie until the next time the page is reloaded. So if you try to set a cookie and then read it again, you won't get a result. If that is indeed your problem, let me know. I have a workaround for it. Hi mate, both are in different scripts. The first script is linked to rather than included. The second script that finds the cookie (supposedly) is included. Interestingly enough, if any other cookies are present it finds them with the following command... print_r($_COOKIE); Quote Link to comment https://forums.phpfreaks.com/topic/58787-solved-cookies/#findComment-292088 Share on other sites More sharing options...
pocobueno1388 Posted July 7, 2007 Share Posted July 7, 2007 Well, if the scripts are included within eachother, then it still counts as being in the same script. So the cookie will not show up until the page is refreshed. See what happens when you execute that code, then go to a different page/refresh. Quote Link to comment https://forums.phpfreaks.com/topic/58787-solved-cookies/#findComment-292089 Share on other sites More sharing options...
Doctor_Strop Posted July 7, 2007 Author Share Posted July 7, 2007 It still doesn't work. I tried refreshing. I think the cookies are being created. My computer is accepting cookies because I have other cookies for other sites. Could it be something to do with the expiry, as I reckon either the cookie is being deleted as soon as I leave the page via... if (isset($_COOKIE[$name])) { header('Location: '. $refPage); } which proves the cookie existed. Quote Link to comment https://forums.phpfreaks.com/topic/58787-solved-cookies/#findComment-292099 Share on other sites More sharing options...
phpknight Posted July 8, 2007 Share Posted July 8, 2007 Try setting an explicit expiration time in seconds for the cookie. Also, instead of doing the header thing, try just making a simple page first that just says print "I found the cookie."; or something like that. You could narrow down the problem. Quote Link to comment https://forums.phpfreaks.com/topic/58787-solved-cookies/#findComment-292206 Share on other sites More sharing options...
Doctor_Strop Posted July 8, 2007 Author Share Posted July 8, 2007 :D ABSOLUTELY massive thanks. Did what you said and took all the header bits out. In the end it was because I had not set the "path" as "/", and the "domain" as bfhq.co.uk, so the call to access the cookie was over looking it. It now works perfectly. setcookie(name, value, expire, path, domain); regards Quote Link to comment https://forums.phpfreaks.com/topic/58787-solved-cookies/#findComment-292587 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.