Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/72231-solved-cookies/
Share on other sites

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

}
?>

Link to comment
https://forums.phpfreaks.com/topic/72231-solved-cookies/#findComment-364329
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/72231-solved-cookies/#findComment-364392
Share on other sites

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');

Link to comment
https://forums.phpfreaks.com/topic/72231-solved-cookies/#findComment-364403
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/72231-solved-cookies/#findComment-364411
Share on other sites

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/72231-solved-cookies/#findComment-364413
Share on other sites

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!

Link to comment
https://forums.phpfreaks.com/topic/72231-solved-cookies/#findComment-364418
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.