CodeIgniter Forums
Cookie help - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Cookie help (/showthread.php?tid=49957)

Pages: 1 2


Cookie help - El Forum - 03-08-2012

[eluser]Kraig[/eluser]
Well I got my sessions to work, and now I'm trying to add a remember me feature. Right now I have it so that if the user checks the box a cookie will be made.
Code:
setcookie('username',$this->input->post('username'),time()+80);
but for some reason I cannot access the cookie to determine if it's there. I know the setcookie is working, because I checked my browsers cookies.
Code:
$cookie = $this->input->cookie('username');
// I've also tried
$_COOKIE['username']; //this gives me an index error



Cookie help - El Forum - 03-08-2012

[eluser]Kristories[/eluser]
You need create a folder to manage cookie and edit your config
Code:
$config['cookie_path']   = "/cookiefolder";

Now create a folder and rename "cookiefolder"


Cookie help - El Forum - 03-08-2012

[eluser]Kraig[/eluser]
Ok...tried that and still the same outcome...??


Cookie help - El Forum - 03-08-2012

[eluser]InsiteFX[/eluser]
You only set the cookie path if you are running on a live server!
On a local server they always go into the tmp directory!

It also depends on the browser you are running...
IE you have to tell it to use session cookies in internet options advanced tab.

Make sure you load the cookie_helper
Code:
$cookie = get_cookie('username');

Also note that unless you do a page refresh the cookie will not show up!

In otherwords you cannot set a cookie and then get and echo it!



Cookie help - El Forum - 03-08-2012

[eluser]Kraig[/eluser]
Ok. So if the user logins with remember me checked, should I run
Code:
if($q) // if the user's credentials validated...
   {
    if($this->input->post('rememberMe'))
    {
     setcookie('username',$this->input->post('username'),time()+80);
    }
    else
    {
     $data = array(
      'username' => $this->input->post('username'),
      'password' => $this->input->post('password'),
      'is_logged_in' => true
     );
    
     $this->session->set_userdata($data);
    }

    redirect($this->input->post('returnurl'));
    exit();
   }
   else
   {
    $this->index();
    $err[] = 'Username and or password are incorrect.';
   }

or should I also run the session data in the rememberMe part?


Cookie help - El Forum - 03-09-2012

[eluser]InsiteFX[/eluser]
1) Check to see if user has remember me
2) If they do then you need to get their database record and log them in
3) If not then you need to redirect them to your login view



Cookie help - El Forum - 03-09-2012

[eluser]Kraig[/eluser]
Ah...so I need to save their cookie info in the db?? What's the point of even using the browser cookie then, because I won't be using it...or do I?


Cookie help - El Forum - 03-09-2012

[eluser]InsiteFX[/eluser]
You still need a cookie for the remember me



Cookie help - El Forum - 03-09-2012

[eluser]CroNiX[/eluser]
The basic steps for each page request would be:
1) Is the user already logged in (check session)?
Yes? update info (if needed) and done.
No? Continue.
2) Does the user have a valid remember me cookie?
Yes? retrieve username from cookie and log them in. Update their info (if needed).
No? Continue.
3) Present Login form.
-Validation
-If passes, log them in, set remember me cookie (if checked), update info, and redirect.
-If fails, repeat Step 3 showing form errors. A single message like "Your username or password did not match." Don't tell them their username was wrong or their password was wrong. That's too helpful to hackers.


Cookie help - El Forum - 03-10-2012

[eluser]Kraig[/eluser]
This is what I have so far...do I need to add session data to the first part of the if statement, or do I just leave it for the cookie setup?

Code:
if($q) // if the user's credentials validated...
   {
    if($this->input->post('rememberMe'))
    {
     setcookie('username',$this->input->post('username'),time()+80);
    }
    else
    {
     $data = array(
      'username' => $this->input->post('username'),
      'password' => $this->input->post('password'),
      'is_logged_in' => true
     );
    
     $this->session->set_userdata($data);
    }

    redirect($this->input->post('returnurl'));
    exit();
   }
   else
   {
    $this->index();
    $err[] = 'Username and or password are incorrect.';
   }