Welcome Guest, Not a member yet? Register   Sign In
[solved]cant set cookies
#1

[eluser]Bianca Migueis[/eluser]
trying to update an existing website to codeigniter but got stuck at the login function... the if statement works since im redirected to the hoome page with the right password and username but for some reason it wont create a cookie... any thoughts... please?

Code:
public function authorize()
    {
        $this->db->where('username', $_POST['username']);
        $this->db->where('password', $_POST['password']);
        $data['query'] = $this->db->get('user');
        
        if ($data['query']->num_rows == 1)
        {
            $username = array(
                  'name'    => 'username',
                  'value'   => $_POST['username'],
                  'expire'  => '',
                );
            $password = array(
                  'name'        => 'password',
                  'value'       => $_POST['password'],
                  'expire'      => '',
                );
            $this->input->set_cookie($username);
            $this->input->set_cookie($password);
            
            redirect('home');
        }
        else
        {
            $this->session->set_flashdata('msg', 'Invalid Username or password.');
            
            redirect('signin');
        }
    }
#2

[eluser]InsiteFX[/eluser]
Thats because you are not setting them right!

CodeIgniter User Guide - Set Cookie

You need to set an expiration time!

InsiteFX
#3

[eluser]Bianca Migueis[/eluser]
Thank you for the link of the page I have printed right in front of me. I have read that page and many others I found on this forum and online but still can't figure out what I'm doing wrong so if you'd be so kind as to tell me why do you say i'm not setting them right I would be forever grateful Smile

EDIT: "Only the name and value are required. To delete a cookie set it with the expiration blank." that's from the user guide. Therefore I left it blank (''), should I use NULL or something like that? I've tried everything I can think of... Sad
#4

[eluser]InsiteFX[/eluser]
I did you need to set and expiration time, also if your using IE
you need to change the cookie name to cisession not ci_session
Code:
// application/config/config.php
$config['sess_cookie_name'] = 'cisession';

$data = array(
    'name'    => 'username',
    'value'   => $_POST['username'],
    'expire'  => time()+900
);

set_cookie($data);

You should not store passwords in a cookie!

InsiteFX
#5

[eluser]Bianca Migueis[/eluser]
Thank you, I misread the part where it says to leave it blank to delete. I thought it meant it wouldn't have an expiration date if it was set to blank. Do you know how to do that without an expiration? I want to leave it there until I decide to delete it.

I usually use encryption and store passwords in cookies. What do you usually do to keep users signed in even after a couple of days?

Thank you for the IE tip Smile
#6

[eluser]InsiteFX[/eluser]
I create a remember me cookie that hods a timeout and token
in the database users table, I then regenerate this cookie
on the fly to protect it.

user comes to your site
check to see if they have a remember me cookie, if they do you auto log them in.
if they do not then they are forced to login.

They will stay loggin until they click logout.

On logout you delete the remember ne cookie.

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB