CodeIgniter Forums

Full Version: problem with session in template
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I would have the need to use the if statement within the view to display different data depending if the user is logged in or not.

In this moment the controller is:
PHP Code:
public function index() {
 if(
$this->session->userdata('logged_in')){
 
$session_data $this->session->userdata('logged_in');
 
$data['username'] = $session_data['login'];
 
$this->smarty->view('home.tpl'$data);
 } else {
 
redirect('user/login''refresh');
 }
 } 

I use the smarty template.

How can I make sure to spend the session management across smarty template? So the management and data?

Thanks for the reply.
Assign the $session_data to the Smarty template like so:

PHP Code:
$smarty->assign('user'$session_data); 

See the Smarty assign() documentation for more information.
thanks for the reply Smile but if I wanted to show something different? For example:

Code:
{if($this->session->userdata('logged_in'))}
Hello, {$user}
{elseif}
Hello guest
{/if}


Thanks for the future reply Smile
Ok, I so stupid Big Grin ahah that was the solution Big Grin
How can I do to enforce these guidelines on all pages of the site?

Code:
$session_data = $this->session->userdata('logged_in');
        $data['username'] = $session_data['login'];
        $this->smarty->assign('user_log', $session_data);

and do not have to declare for each controller?

Thanks for the reply Smile
With a MY_Controller, then all of your other controllers would extend MY_Controller instead of CI_Controller.

http://www.codeigniter.com/user_guide/ge...core-class
I created MY_Controller in application/core/ and this is the code:

PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
MY_Controller extends CI_Controller {

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

 
       public function index() {
 
               $session_data $this->session->userdata('logged_in');
 
               $data['username'] = $session_data['login'];
 
               $this->smarty->assign('user_log'$session_data);
 
       }



In the all other controller I edit in this way:
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
NameClass extends MY_Controller 

But when I try to access in the page I found this error:

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined index: user_log

Filename: compiled/c3a9b50f1375d1dde5af34b18cf8ecc0df629901.file.rightSide.tpl.php

Line Number: 33

Backtrace:
Code:
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: compiled/c3a9b50f1375d1dde5af34b18cf8ecc0df629901.file.rightSide.tpl.php

Line Number: 33

Huh How should I do?

Sorry but it is the first time that I attempt to well with CI


EDIT: If I move 
Code:
$session_data = $this->session->userdata('logged_in');
               $data['username'] = $session_data['login'];
               $this->smarty->assign('user_log', $session_data);

In the __construct() session data makes them see me but this is the error:
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined index: username

Filename: compiled/c3a9b50f1375d1dde5af34b18cf8ecc0df629901.file.rightSide.tpl.php

Line Number: 35
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;
Thanks for the reply.
Since I will not have to use only the username but also other values, which method do you recommend to use?


EDIT: When I use $data['username'] = $session_data['login']; after I pass the $data variable in view.
But it does not work anyway.


I am very stupid, i found this discussion http://stackoverflow.com/questions/17446...d-retrieve that I did find the solution.

If I already have the tag assigned user_log and when used in template works perfectly, I just use that and add the field that I want to use.

For example: I need to use the username field

I'm going to write in the template {$user_log.username} Smile

thanks for the reply and support!!