Seth
Seth

Reputation: 6260

PHP Cookies not being set

I'm using CodeIgniter to build a webapp for my laws. I'm trying to build a remember me function, but I can't set cookies on either my shared host or locally.

I've tried both CI's cookie helper and PHP's setcookie(), neither work.

PHP:

$token = $this->random(32, $this->input->post('userPassword'));
$cookie_value = '{"email":"'. $this->input->post('userEmail') .'","token":"'. $token . '"}';

if ( setcookie('remember_me', urlencode($cookie_value), 1000000, '/') )
{
    echo "Set Val: " . json_encode($cookie_value);
}

CI:

$this->load->helper('cookie');
$token = $this->random(32, $this->input->post('userPassword'));
$cookie_value = '{"email":"'. $this->input->post('userEmail') .'","token":"'. $token . '"}';

if ( set_cookie('remember_me', urlencode($cookie_value), 1000000, '/') )
{
    echo $this->input->cookie('remember_me');
    echo "Set Val: " . json_encode($cookie_value);
}

Both seem to work, as they get inside the if block, but they don't set the cookie in either Chrome or Firefox.

What am I doing wrong?

Upvotes: 1

Views: 1102

Answers (1)

Alasdair
Alasdair

Reputation: 14103

For a start, you are setting the expiration of the cookie back in time, so it will expire immediately.

So this:

setcookie('remember_me', urlencode($cookie_value), 1000000, '/')

Should be:

setcookie('remember_me', urlencode($cookie_value), time()+1000000, '/')

Upvotes: 3

Related Questions