javascript - How to set cookies for remember me checkbox after login in codeigniter?

891

view:

Controller:

public function login_redirect()
{
    $email = $this->input->post('elogin');
    $password = $this->input->post('plogin');
    $remember = $this->input->post('remember_me');

    $this->db->select('*');
    $this->db->from('user');
    $where = "email='".$email."' and password='".$password."' and status='1'";
    $this->db->where($where);
    $query = $this->db->get();
    if($query->num_rows() > 0) 
    {
        $result = $query->result_array();
        $this->session->set_userdata('user_id',$result);
        if (!isset($_POST)) 
        {
            header ("Location:".base_url()."thankyou");
        } 
        else 
        {
            echo json_encode(array('redirect' => base_url().'thankyou'));
        }
    }
    else 
    {
        echo json_encode(array('error' => 'Wrong email or password or may be your account not activated.'));
    }
}

In this code, I simply create a login form and do login via jquery ajax which is successfully working. Now, I want to set cookie after successful login via jquery ajax and cookie available only for one day after the day cookie expires. So, How can I do this? Please help me.

Thank You

43

Answer

Solution:

View Page - Login.php :

 <input type="email" class="form-control" name="email" placeholder="Enter Email"  value="<?php if(isset($_COOKIE["email"])) { echo $_COOKIE["email"]; } ?>" required>
<input type="checkbox" name="remember" <?php if(isset($_COOKIE["email"])) { ?> checked <?php } ?> > Remember Me

Controller - LoginController.php

$email = $this->input->post('email');
$password = md5($this->input->post('password'));
$remember = $this->input->post('remember');

if(!empty($remember)) {  
  setcookie ("email",$email,time()+ 3600);  
  setcookie ("password",$password,time()+ 3600);
}else{  
    if(isset($_COOKIE["email"] && isset($_COOKIE["password"])) ){  
        setcookie ("email",""); setcookie ("password","");   
    }  
}  

People are also looking for solutions to the problem: javascript - How to access PHP variables after posting and redirecting with AJAX?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.