sooraj s pillai
sooraj s pillai

Reputation: 916

store logout time in database using codeigniter

I want to store logout time in the database while i click logout button . please help me how to fetch the date and time and store it in the database. I am not much familiar to codeigniter .

here is my logout controller

public function logout()
{
    $this->session->unset_userdata('SESS_ADMIN');

    redirect('test');
}

Upvotes: 1

Views: 1730

Answers (4)

lwegaba
lwegaba

Reputation: 147

For those using Codeigniter 4 (ci_4), here is my solution after the above of ci3 guided me.

My Controller

public function logout()
{

    $this->mysessionsModel      = new MySessionsModel();

    $id = session()->get('logged_user');
    $time = ['logged_out' =>  date("Y-m-d H:i:s")];

    if(!empty($id)){
    $this->mysessionsModel->logoutTime($id, $time); 
    }
    $session = session();
    session()->remove('logged_user');
    session()->remove('loggedIn', true);    
    
    $session->destroy();
    return redirect()->to('/'); 
}

MODEL

// updating User logout time in system.

public function logoutTime($id, $time)
{
    $builder = $this->db->table('table_name');
    $builder->where('code', $id);
    $builder->update(['logged_out' =>$time]);
    if($this->db->affectedRows()==1){
         return true;
         
     }else{
         return false;
     }
}

NOTE: Ensure your login time and logout time do not have ATTRIBUTES "on update CURRENT_TIMESTAMP".

Because the above gave me run for money as every time i updated logout, login time could also change.

Upvotes: 0

Shoukat Mirza
Shoukat Mirza

Reputation: 828

Create a function in model and call it from controller

Controller

public function logout() {
    $date = array(
        'date'=>date('d-m-y h:i:s')
    );
    $this->model_name->logout($date);
    $this->session->unset_userdata('SESS_ADMIN');
    redirect('test');
}

Model

    public function logout($date)
    {
        $this->db->insert(table_name, $date);
    }

Upvotes: 3

Saheb Mondal
Saheb Mondal

Reputation: 163

First create your table.

create table tbl_user_logout(
 id int not null AUTO_INCREMENT,
 user_id int,
 logout_date varchar(100),
 logout_time varchar (100),
 PRIMARY key(id)
);

then go to application/config/database.php and provide your database details

then in your controller add this code

    public function logout()
    {   
        $this->session->unset_userdata('SESS_ADMIN');

        $today_date=date('Y-m-d');
        $today_time=date('H:i:s');

        $post_data=array(
            "user_id"=>$user_id, // user_id you have to get from session data
            'logout_date'=>$today_date,
            'logout_time'=>$today_time,
        );
        $this->db->insert('tbl_user_logout',$post_data);

        redirect('test');
    }

Upvotes: 0

Naim Malek
Naim Malek

Reputation: 1184

public function logout()
{
    $dateTime = date("Y-m-d H:i:s"); 
    $this->userModel->updateLogoutTime($dateTime);
    $this->session->unset_userdata('SESS_ADMIN');

    redirect('test');
}

UserModel.php

public function updateLogoutTime($dateTime="") {
      $data = array(
             'logoutTime' => $dateTime,
      );
      $userId = $this->session->userdata('YOUR_USER_ID_VARIABLE_NAME');
      $this->db->where('id', $userId);
      $this->db->update('TABLENAME' ,$data);
      return $this->db->affected_rows();
}

Upvotes: 1

Related Questions