[eluser]tommyadey[/eluser]
MY own way of dealing with cookies.
Check if the remember me check box is checked.
Generate a random string eg
Code:
$random_string = random_string('numeric', whatever length e.g 19);
Get the user_id.
Make a token string
Code:
$cookie_token = $random_string . $user_id;
Log the user_id and the cookie token in the database
the database table should have these fields
Code:
user_id int(11)|cookie_token Varchar(30)|cookie_created|cookie_expire|user_agent|ip_address|is_deleted
If model returns true, or if you log it from the controller, if this db insert etc
Code:
$this->input->set_cookie('cooken', $cookie_token, 86400 * 180//chose how long, '.mywebsite.com');
Then make a standard library, then make a function called cookie_login.
You can get $cookie_token like this in the library;
Code:
$cookie_token = $this->CI->input->cookie('cooken');
Do checks on the $cookie_token e.g not empty, isset and ctype_nu, etc
Make a function in model is_cookie_valid($cookie_token) passing cookie token
Model function should look like this;
Code:
public function is_cookie_valid($cookie_token)
{
$user_id = substr($cookie_token, 19);
$query = $this->db->where('user_id', $user_id)->where('cookie_token', $cookie_token)->where('is_deleted', 0)->get('cookie_control');
return ($query->num_rows() === 1) ? $user_id : false;
}
if cooke token has been found, then you return the user_id.
Now back to the cookie_login function,
You can now use the user id to get your user's data, set session as active and so on.
Remember to put
Code:
$this->standard_library(Your library name)->cookie_login();
in a constructor func at the top of the required controllers e.g. login,signup ,home e.t.c . Depends how large your site is.
When the user log's out, you need to destroy the cookie and also set is_deleted in cookie_control as 1.
Code:
delete_cookie('cooken');
That's basically how i do it for now, pretty basic uh. i've seen people set cookie's using the user's email and password. Mine's not the safest and not the worst. It's preety good considering how basic it is though so i guess it should be ok for you, at least for now.