Welcome Guest, Not a member yet? Register   Sign In
set_cookie not working
#11

[eluser]CroNiX[/eluser]
you can't set a cookie and then immediately read it. The cookie gets written during the final output of the request.
#12

[eluser]ibnclaudius[/eluser]
I created another method, and put this:

Code:
print_r($this->input->cookie());

After loading the index method, I accessed it but got a blank page.
#13

[eluser]ibnclaudius[/eluser]
I set the cookies like this:

Code:
$cookie = array(
    'name'   => 'remember',
    'email'  => $email,
    'value'  => $value,
    'expire' => '86500',
    'domain' => '',
    'path'   => '/',
    'secure' => TRUE);
set_cookie($cookie);

And check like thiss:
Code:
if(get_cookie('remember')) { }

But nothing happens..
#14

[eluser]Aken[/eluser]
Change 'secure' to FALSE.
#15

[eluser]Bhashkar Yadav[/eluser]
i don't think, array data type can be stored into cookie. can you try like
Code:
$cookie = array(
    'name'   => 'remember',
    'email'  => $email,
    'value'  => $value,
    'expire' => '86500',
    'domain' => '',
    'path'   => '/',
    'secure' => TRUE);
$serialize_cookie = serialize($cookie);
$this->set_cookie('MyCookieData', $serialize_cookie);

while accessing
Code:
$d = $this->get_cookie('MyCookieData');
$d1 = unserialize($d);


now you can access $d1['name'];
#16

[eluser]Aken[/eluser]
The array is for the cookie settings, not the data being stored.
#17

[eluser]Bhashkar Yadav[/eluser]
yes Eric, i didn't notice the array $cookie.
#18

[eluser]InsiteFX[/eluser]
You can pass an array to the cookies value!
Code:
$data = array('value1' => '1', 'value2' => '2');

$data = serialize($data);

$cookie = array(
    'name'   => 'remember',
    'email'  => $email,
    'value'  => $data,
    'expire' => '86500',
    'domain' => '',
    'path'   => '/',
    'secure' => FALSE
);

// then when you get the cookie, you will need to unserialize the value back to an array.
#19

[eluser]ibnclaudius[/eluser]
Now I can set cookie and check if there's cookie set.

Bu how can I retrieve for example the email and token data from inside a serialized value?

Code:
public function create_cookie($user_id)
{
  $token = sha1($user_id . uniqid(rand(), true) . microtime());
  
  $data = array('user_id' => $user_id, 'value' => $token);
  
  $data = serialize($data);

  $cookie = array(
   'name'   => 'remember',
   'value'  => $data,
   'expire' => '86500',
   'domain' => '',
   'path'   => '/',
   'secure' => FALSE);

  $create_cookie = $this->CI->mb_database->create_cookie($user_id, $token);

  if($create_cookie)
  {
   set_cookie($cookie);
  }
}

This is not working..
Code:
if(get_cookie('remember'))
  {
   $session = unserialize(get_cookie('value'));
   $this->mb_actions->auto_login_user($session['email'], $session['token']);
  }
#20

[eluser]Aken[/eluser]
The NAME of the cookie goes into get_cookie(), and the VALUE is what is returned from the get_cookie() function.




Theme © iAndrew 2016 - Forum software by © MyBB