bdichiara Posted October 8, 2007 Share Posted October 8, 2007 I've never really needed to use cookies. I am having trouble figuring out why my cookies aren't working. I'm trying to set a cookie for the language of the site. To test to see if the cookie is actually being set, i tried 2 things: if(setcookie("mysite_language",$lang,time()+60*60*24*365)){ echo 'cookie set successfully'; } if(!(setcookie("mysite_language",$lang,time()+60*60*24*365))){ echo 'cookie not set'; } This seemed to be working. When I went to change the languages i received the successful message. At the same time, i'm using $_SESSION['lang'] = $lang, so while their on the site, it reads off that variable rather than the cookie. This is working just fine. It's only after they leave when problems start. When I close the browser and open it back up, for some reason this code is not working: if(empty($_SESSION['lang'])){ //attempt to read cookie if(isset($_COOKIE['mysite_language']) && !empty($_COOKIE['mysite_language'])){ $_SESSION['lang'] = $_COOKIE['mysite_language']; } else { //else set to English $_SESSION['lang'] = 'en'; } } It never remembers the changed language and always starts back in English. I've tried print_r($_COOKIE); print_r($HTTP_COOKIE_VARS); and the only thing that shows up is PHPSESSID=... for both of them. Is there something set incorrectly in my PHP.INI file? I'm using Firefox to test this. It seems like it's setting the cookie successfully, but not maintaining it. Is my expiration date wrong? Should I be using the directory or domain settings in setcookie()? Please help! Quote Link to comment https://forums.phpfreaks.com/topic/72231-solved-cookies/ Share on other sites More sharing options...
sKunKbad Posted October 8, 2007 Share Posted October 8, 2007 how about $lang = 'english'; if (!isset($_COOKIE)){ $lang = $_REQUEST['lang']; setcookie("TestCookie", $lang, time()+3600, "/", ".example.com", 1); } Quote Link to comment https://forums.phpfreaks.com/topic/72231-solved-cookies/#findComment-364288 Share on other sites More sharing options...
yzerman Posted October 8, 2007 Share Posted October 8, 2007 a good way to test if cookies are being set is to print_r($_COOKIE); Quote Link to comment https://forums.phpfreaks.com/topic/72231-solved-cookies/#findComment-364291 Share on other sites More sharing options...
bdichiara Posted October 8, 2007 Author Share Posted October 8, 2007 ok, this echo's nothing if(!isset($_COOKIE)){ echo 'false'; } which means that cookies are working. and as you can see from my initial post, I already tried print_r($_COOKIE) and it returns only 1: PHPSESSID Any other suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/72231-solved-cookies/#findComment-364317 Share on other sites More sharing options...
bdichiara Posted October 8, 2007 Author Share Posted October 8, 2007 I am using this now, and still no luck: setcookie("mysite_language", $lang, (time()+60*60*24*365), "/", "sub.mysite.com", 1); I'm using a sub-domain if it makes any difference... Quote Link to comment https://forums.phpfreaks.com/topic/72231-solved-cookies/#findComment-364318 Share on other sites More sharing options...
yzerman Posted October 8, 2007 Share Posted October 8, 2007 Try this <?php ini_set("session.use_cookies",1); ini_set("session.use_only_cookies",1); ini_set("session.name","COLMOSH_SESSID"); session_start(); setcookie('mysite_lang',$lang,time()+60*60*24*365,'','sub.mysite.com'); if (!isset($_COOKIE['mysite_lang'])) { //tell the user to select a language } else { //language is set - do something } ?> Quote Link to comment https://forums.phpfreaks.com/topic/72231-solved-cookies/#findComment-364329 Share on other sites More sharing options...
bdichiara Posted October 8, 2007 Author Share Posted October 8, 2007 ini_set("session.use_cookies",1); ini_set("session.use_only_cookies",1); ini_set("session.name","COLMOSH_SESSID"); session_start(); setcookie('mysite_lang',$lang,time()+60*60*24*365,'','sub.mysite.com'); My PHP.INI shows that use_cookies is already On, however use_only_cookies is Off. I don't see how changing the Session ID Name will help, but I did that, as well as everything else in your post, but I still only have 1 cookie displaying on print_r($_COOKIE); and that is the PHPSESSID, which by the way did not get renamed. This is starting to frustrate me. Here's the PHP_INFO page: http://iact.solepixel.com/phpinfo.php Thanks for the help, everyone! A list of cookies appears at the top of the page of http://iact.solepixel.com Also, the only languages that work right now are English and Spanish, and Spanish only works for some of the pages right now. Quote Link to comment https://forums.phpfreaks.com/topic/72231-solved-cookies/#findComment-364392 Share on other sites More sharing options...
yzerman Posted October 8, 2007 Share Posted October 8, 2007 yeah, my fault. I copied that off of some code I created and forgot to modify the session name :/ ini_set("session.use_cookies",1); ini_set("session.use_only_cookies",1); ini_set("session.name","MYSITE_SESSID"); session_start(); setcookie('mysite_lang',$lang,time()+60*60*24*365,'','sub.mysite.com'); Quote Link to comment https://forums.phpfreaks.com/topic/72231-solved-cookies/#findComment-364403 Share on other sites More sharing options...
bdichiara Posted October 8, 2007 Author Share Posted October 8, 2007 Ok, here's something interesting I've figured out. Right after I set the cookie, I'm displaying the contents of print_r($_COOKIE); This is showing the newly created cookie, however immediately after i set the cookie, i'm sending the user back to the referring page. So, for example, if they're on the home page, and they choose a different language, It works like this http://iact.solepixel.com CLICK: Change to Spanish http://iact.solepixel.com/languages/es/ WRITE COOKIE['language'] UPDATE SESSION['lang'] REDIRECT to referrer If i comment out the redirect (header("Location: " . $referrer), it displays the cookie fine, everything is set properly, all is good. So why after the redirect does it lose the value? Quote Link to comment https://forums.phpfreaks.com/topic/72231-solved-cookies/#findComment-364411 Share on other sites More sharing options...
yzerman Posted October 8, 2007 Share Posted October 8, 2007 Because you are not deleting the cookie, you are making 2 seperate cookies - and the site doesnt know which to use. I went to your site, chose language as spanish - then chose english. Went into my cookies - and I have 2 cookies - one for english and one for spanish. When you set your cookie - you need to delete or expire the old cookie first, then try setting the cookie. Quote Link to comment https://forums.phpfreaks.com/topic/72231-solved-cookies/#findComment-364413 Share on other sites More sharing options...
bdichiara Posted October 8, 2007 Author Share Posted October 8, 2007 Ok, I did that, and that may have helped, but I think the solution was that it was setting cookies in the current directory. I used this instead and it seems to be working now setcookie("mysite_language", $lang, (time()+(60*60*24*365)), '/'); The important part being the '/' at the end. Thanks for all the help! Quote Link to comment https://forums.phpfreaks.com/topic/72231-solved-cookies/#findComment-364418 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.