Welcome Guest, Not a member yet? Register   Sign In
problem with session in template
#8

Don't put it in the index() method of MY_Controller. Put it in the __construct(), or define a different function in MY_Controller (other than index()) and have __construct() run it.

Code:
class MY_Controller extends CI_Controller {
  public function __construct()
  {
    parent::__construct();
    $this->init_session();
  }

  public function init_session()
  {
    $session_data = $this->session->userdata('logged_in');
    $data['username'] = $session_data['login'];  //this isn't going to do anything because $data isn't passed anywhere.
    $this->smarty->assign('user_log', $session_data);
  }
}

See the comment about your error, hope it makes sense.

You can do several things to overcome that.
1) You already assign session data to the smarty template (which contains username)...use that instead of $data['username']
2) Assign username to a GLOBAL variable, which can be read everywhere ($this->data['username'] = $session_data['login'], then access $this->data['username'] in the template
3) Use CI's load::vars(). (see userguide for "loader" class)
$this->load->vars($session_data);
Then all of the array vars in $session_data will be available everywhere in your app just like passing $data to a view. echo $username;
Reply


Messages In This Thread
problem with session in template - by StratoKyke - 05-14-2015, 02:35 AM
RE: problem with session in template - by CroNiX - 05-16-2015, 03:56 PM
RE: problem with session in template - by CroNiX - 05-17-2015, 09:52 AM



Theme © iAndrew 2016 - Forum software by © MyBB