Welcome Guest, Not a member yet? Register   Sign In
Issue with Sessions! How to keep the user login forever
#1

Hi;

I use CI database for sessions (and user's auth).

1 - I was wondering how I can get the users to be logged in forever.
2 - Let's say user logs in and stays idle for 15 min. If I reload a page (with Ajax or Jquery) that requires the user to be logged in, it logs the user out and says the user is not logged in. How can I do it so once the user logs in, I'd be able to make ajax calls at any time without having to re login.


This is my config:

PHP Code:
$config['sess_cookie_name'] = 'ci_session_web';
$config['sess_expiration'] = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300

This is how I log them in:

PHP Code:
$newdata = array('username'  => $data['member_details'][0]->first_name,'email'  => $data['member_details'][0]->email'member_id'  => $data['member_details'][0]->id'logged_in' => TRUE);
$this->session->set_userdata('logged_data'$newdata); 

This is how I check if the user is logged in or not.
PHP Code:
if(isset($this->session->userdata['logged_data']['logged_in'])) 

Thanks
Reply
#2

Try this code...

Initializing data to session

PHP Code:
$data = array('username' => $value['username'], 'isLoggedIn' => true);
$this->session->set_userdata($data); 

checking data from session

PHP Code:
if($this->session->userdata('isLoggedIn')) {
   echo 
'You are logged in';
}else {
   echo 
'You are not logged in';


If you can't follow visit the documentation https://www.codeigniter.com/user_guide/l...sions.html
Reply
#3

(This post was last modified: 12-15-2015, 10:53 PM by behnampmdg3.)

Hi lazyfox

Thank you for the code but how could that make a difference? it's just another way of it.

I want the user to be logged in forever.

I added this library, hopefully this will help:

PHP Code:
class MY_Session extends CI_Session {

public function 
sess_update()
{
 
   $CI =& get_instance();

 
   if ( ! $CI->input->is_ajax_request())
 
   {
 
       parent::sess_update();
 
   }
 
 }


xo
Reply
#4

Didn't work!
Reply
#5

(This post was last modified: 12-16-2015, 12:03 PM by cartalot.)

so look in application/config.php and there is session expiration

the session expiration is in seconds -- so lets say for one month
60 seconds x 60 minutes x 24 hours x 31 days = 2678400

so change this
PHP Code:
$config['sess_expiration'] = 7200

to this for one month
PHP Code:
$config['sess_expiration'] = 2678400
Reply
#6

(12-16-2015, 12:01 PM)cartalot Wrote: so look in application/config.php and there is session expiration

the session expiration is in seconds -- so lets say for one month
60 seconds x 60 minutes x 24 hours x 31 days = 2678400

so change this
PHP Code:
$config['sess_expiration'] = 7200

to this for one month
PHP Code:
$config['sess_expiration'] = 2678400

Still logs out!
Reply
#7

Well, the session is only active as long as the browser window is open. Once the user closes the browser window, you have to find another way to do that. This is typically done with a cookie, and is the typical "Remember Me" functionality from logins. However, this method is full of security issues so you have to be very careful when you implement it. This discussion on Stack Overflow has good information about implementing a secure remember me.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB