1

For my website user tracking is implemented in which login duration of user is stored in database. I am using time() to get current time. So,

$login_time = time ();   // when user logs in  

$logout_time = time ();   // when user logs out 

$duration = $logout_time - $login_time;          // duration of login

But , what to do if your does not to logout ? What will be the right value to store in database? I am using PHP, Codeigniter and MySql.

WeAreRight
  • 885
  • 9
  • 24

2 Answers2

0

You can use sess_expiration In you config file you can add below line and define the timeout for non logged out user.

  $config['sess_expiration'] = 7200; // 2 hours

This will let you the default time of logout if user doesn't log out. Now using this value you can get the duration. This value is stored in seconds convert it into minutes and subtract from your logged in time.

Also if is dosn't help you can use ci_sessions table.

Nishant Nair
  • 1,999
  • 1
  • 13
  • 18
-1

Please consider two fields, like login_time and logout_time

$login_time = time ();   // when user logs in  
$logout_time = time ();   // when user logs out 

When user will login then store time() on login_time field and set logout_time field is null.

Set below line on config file

$config['sess_expiration'] = 0; // never expired

Then you will need to update logout time field if user logout other wise you will get duration based on current time and login time which is you have stored in database.

Kamlesh
  • 654
  • 8
  • 21