Problems with cookies / MAMP / CodeIgniter

2019-03-01 17:51发布

I'm having a problem with reading cookies on localhost, using MAMP and Codeigniter.

I'm trying to ude a cookie to authenticate acess to an admin area. I can set the cookie (I see it on my browser - Chrome) but i'm not able to read it after doing that to grant the acess. I've already tried lots of configurations, but none works. I really need some help on this.

Those are the essencial parts of my code:

Here I set the cookie

$cookie = array(
    'name'   => 'login',
    'value'  => 'true',
    'expire' => '0',
    'secure' => TRUE
);
set_cookie($cookie);

Here I redirect the user to login page if there is no cookie and to control panel if the cookie is set

function login_redirect() {
    $this->load->helper('cookie');
    if (uri_string() == 'admin/controlpanel') {
        if ($this->input->cookie('login')) {
        } else {
            redirect('admin/');
        }
    }
    if (uri_string() == 'admin') {
        if ($this->input->cookie('login')) {
            redirect('admin/controlpanel');
        }
    }
}

OBS: all this code is in the admin_model

Any tips?

Thanks and sorry about my english. I hope I've made myself clear.

1条回答
Anthone
2楼-- · 2019-03-01 18:49

Codeigniter has some problems with the Cookie and Session libraries when run on some localhost configurations. You'll spend hours trying to find out the particular problem with your setup. The best bet is to use generic PHP cookie/session when on localhost and use another library when in testing.

I appreciate that this is by no means the best solution but it's the best advice I can offer.

查看更多
登录 后发表回答