CodeIgniter Cookies Working In FF, But Not Chrome

2019-04-16 08:31发布

问题:

I've built a site in CI, and have a login system, which works fine in Firefox but not Chrome or IE. In those two if the username and password are correct it just redirects (i think) back to the login page, not to the login error page, or to the site home (as it should). I've noticed that it doesn't seem to be setting a cookie in Chrome, but it does in FF.

Here's the code in my controller which sets the cookie and redirects after authentication:

            $newdata = array(
                   'username'  => $_POST['login_username'],
                   'real_name' => $name,
                   'user_id' => $uid,
                   'logged_in' => TRUE
               );

            $this->session->set_userdata($newdata);
            //echo $newdata;
                redirect('/site/index');    

Any ideas why this might be happening?

Thanks

回答1:

Check that $_SESSION really is set. On CI you may need to exit gracefully to flush out whats in $this-sessionto the 'real' session variable.

add a echo serialize($_SESSION) so you know whats going on (before the redir).

and check how to end a Ci-request gracefully.



回答2:

I experienced this problem too ... session userdata lost for Chrome and IE, but ok on Firefox. It was due to an incorrect setting in config/config.php

I had to explicitly set cookie_domain To make it automagic in the future I used this command ... ripped off the CI forums.

$config['cookie_domain'] = str_replace("http:/","",str_replace("https://","", $config['base_url']));

I added this line and all was ok.



回答3:

For me it solved just adding more time to the cookie, I had 3600 so I changed to 7200. Seems to be a problem with time on my production site. May help someone.



回答4:

Adjusting the cookie name can fix the issue in the CI configs to remove underscores works wonders. The article CodeIgniter Session Problem in IE explains the details quite well.



回答5:

In my case, the problem was that $config['base_url'] was empty. So i set it to my domain

$config['base_url'] = 'http://yourdomain.com/';


回答6:

CodeIgniter has some problem if the specified domain for cookies is localhost, I've set a fake domain with a real domain's name structure into the hosts file and it works.