0

I'm working on session in codeigniter now and I'm not sure where it goes wrong. I only manage to read session for the 1st page. When I browse to other page, the session gone. The following are the var_dump for dashboard.

array(2) {
["__ci_last_regenerate"]=>
int(1487063353)
["logged_in"]=>
array(2) {
["userName"]=>
string(5) "julie"
["userRole"]=>
string(1) "1"
}}

and the following are var_dump for the second page and when i go back to dashboard

array(1) {
["__ci_last_regenerate"]=>
int(1487063602)
}

The following are my codes. Appreciate if you can help me point out the problem and solutions. Thanks in advance.

config/config.php

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 1800; // 30 minutes
$config['sess_save_path'] = BASEPATH .'/sessions/';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

libraries/SimpleLoginSecure.php

function login($user_name,$user_pass)
{
   $this->CI =& get_instance();

   // verification code goes here

   //set session data
   $user_data = $query->row_array();
   $dataSession['userName'] = $user_data['userName'];
   $dataSession['userRole'] = $user_data['userRole'];
   $this->CI->session->set_userdata('logged_in',$dataSession);
   return TRUE;
}

controller/Login.php

function __construct() {
    parent::__construct();
}

public function index() {
    $this->load->helper('form');
    $this->load->library('form_validation');

    $this->form_validation->set_error_delimiters('<p class="text-red">', '</p>');       
    $this->form_validation->set_rules('userName', 'Username', 'trim|required|xss_clean');
    $this->form_validation->set_rules('userPass', 'Password', 'trim|required|xss_clean|callback_check_database');

    if ($this->form_validation->run() == false) {
        $this->load->view('index');
    }       
}

function verify_user()
{
    $this->load->library('form_validation');
    $this->load->library('SimpleLoginSecure');

    $hasher = new PasswordHash(8, false);
    $userpass = $this->input->post('inputPassword');
    $username = $this->input->post('inputName');

    $results = $this->simpleloginsecure->login($username,$userpass);
    if($results != FALSE) {
      $SESS_data = $this->session->userdata('logged_in');
      $data['SESS_username'] = $SESS_data['userName'];
      $data['SESS_userrole'] = $SESS_data['userRole'];

      var_dump($this->session->all_userdata());
      $this->load->view('dashboard', $data);
  }
  else {
     $attempt = $this->simpleloginsecure->updatePassAttempt($userName);         
     if ($attempt != FALSE) {
        echo '<script>alert("Invalid Username or password"); window.history.back();</script>';
     }      
   }      
}

function logout() {
    $this->load->library('SimpleLoginSecure');

    // Logout
    $this->simpleloginsecure->logout();
    echo '<script>alert("Successfully Logout"); </script>';
    redirect(base_url());
}
Julie
  • 313
  • 6
  • 18
  • can you please your all code for controller –  Feb 14 '17 at 09:22
  • You means your session values lost on second page? – Mayank Pandeyz Feb 14 '17 at 09:23
  • @MayankPandeyz yes.. the value gone.. i dont know whats wrong.. and i only put sess_destroy during logout.. even when i go back to dashboard.. no session data captured. – Julie Feb 14 '17 at 09:27
  • @NavjotSingh .. sure.. i'll update my posting.. one moment – Julie Feb 14 '17 at 09:28
  • On successful login you are setting some value in SESSION, it is available on dashboard page, but note on another page. This is th problem you are facing ? – Mayank Pandeyz Feb 14 '17 at 09:38
  • you can set data to session simply like this in Codeigniter In the start of every controller constructor like this. function __construct() { parent::__construct(); $this->load->library('session'); } –  Feb 14 '17 at 09:38
  • @MayankPandeyz correct.. no session value on other page and when i go back to dashboard.. the session gone as well.. the session value only show during the first time display dashboard.. – Julie Feb 14 '17 at 09:40
  • @NavjotSingh I already load session in autoload.php.. should i put again in all page? No need right? – Julie Feb 14 '17 at 09:40
  • In autoload.php is enough – Mayank Pandeyz Feb 14 '17 at 09:46
  • Did you get something? – Mayank Pandeyz Feb 14 '17 at 09:49
  • @MayankPandeyz get what? I don't see any errors tho.. and my var_dump only display **array(1) { ["__ci_last_regenerate"]=> int(1487065198) }** – Julie Feb 14 '17 at 09:51
  • OK, give me 2 minutes. – Mayank Pandeyz Feb 14 '17 at 09:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/135666/discussion-between-mayank-pandeyz-and-julie). – Mayank Pandeyz Feb 14 '17 at 09:53
  • Note: Your name of your library should be **libraries/Simpleloginsecure.php** same with class first letter only upper case. Then `$this->load->library('simpleloginsecure')`; https://www.codeigniter.com/user_guide/general/styleguide.html#file-naming –  Feb 14 '17 at 10:05
  • @wolfgang1983 yes.. it is the same.. typo in my posting.. – Julie Feb 14 '17 at 10:07
  • Hi Julie , plz do not use login function in libraries, you create a same function in helper , and second point your logout function wrong – Manish sharma Feb 14 '17 at 10:40
  • @Manishsharma .. I'm using the SimpleLoginSecure library to do password hashing as it is much more easier than developing myself.. and I believe.. for CI.. most developer are using library to do password hashing.. **correct me if im wrong and do let me know which way is better to do password hashing in CI**.. and as for logout function.. I'll look into it.. thank you.. – Julie Feb 14 '17 at 11:18
  • @Julie : your login function not working about password hashing,,,password hashing different concept.so my recommendation please make it simple demo without your libaray . – Manish sharma Feb 14 '17 at 11:49
  • What version of CI are you using? [CI 2 sessions lib was not reliable.](http://stackoverflow.com/a/34338027/3585500) – ourmandave Feb 14 '17 at 14:17
  • @ourmandave im using latest version 3.1.3 – Julie Feb 15 '17 at 02:03
  • @Manishsharma my password hashing with SimpleLoginSecure works correctly.. or at least works as expected.. I dont think it was the cause because I got the session value when open the dashboard.. its just that the value seems destroyed @ reset at the following page.. – Julie Feb 15 '17 at 02:06
  • @Julie Could it be the obscure [.htaccess modifying headers problem?](http://stackoverflow.com/a/34662283/3585500) – ourmandave Feb 15 '17 at 03:01
  • Also see this answer for common problems persisting a session. http://stackoverflow.com/questions/10655355/getting-a-checkbox-array-value-from-post – ourmandave Feb 15 '17 at 03:36
  • @ourmandave .. thanks for the article.. i'll go thru it..and i'll check the .htaccess as well.. I've been stuck for days on this issue.. doing other modules to clear my mind.. maybe there's something i missed out.. I just didnt find it yet.. _sigh_ – Julie Feb 15 '17 at 03:40
  • @Julie : As you wish go ahead – Manish sharma Feb 15 '17 at 04:21
  • @Manishsharma I'm not sure whats wrong.. I didnt change my code.. so what I did is.. at dashboard.. when i still have the session value.. i refresh my page manually (press ctrl+R).. after that i navigate to other page and to my surprise.. the session value are there..until I logout... why is that happen?.. why I need to refresh?.. and how to automatically refresh my page after assign the session?.. I'm a bit confuse.. – Julie Feb 15 '17 at 11:08

1 Answers1

0

Make sure you are actually loading the session library, the safest bet if its something that's going to be used 95-100% of the time stick it in autoload.

application/config/autoload.php in the libraries array add 'session', this will ensure on initailisation it will always be loaded. Its around line 60:

$autoload['libraries'] = array('session');

The most common issue for this kind of thing occurring is not having the library loaded when you need it.

I would also suggest using a full controller and model for account management instead of libraries or helpers, makes life much easier for you as its within the standard CI scope.

Brian Ramsey
  • 850
  • 7
  • 21
  • I did put session in autoload.. and as for account management .. to verify user.. i use SimpleLoginSecure as I'm using the library to do password hashing.. – Julie Feb 14 '17 at 11:14