problem with session in template |
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() { 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 but if I wanted to show something different? For example:
Code: {if($this->session->userdata('logged_in'))} Thanks for the future reply
How can I do to enforce these guidelines on all pages of the site?
Code: $session_data = $this->session->userdata('logged_in'); and do not have to declare for each controller? Thanks for the reply
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'); In the all other controller I edit in this way: PHP Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); But when I try to access in the page I found this error: Code: A PHP Error was encountered Code: A PHP Error was encountered 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'); In the __construct() session data makes them see me but this is the error: Code: A PHP Error was encountered
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 { 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} thanks for the reply and support!! |
Welcome Guest, Not a member yet? Register Sign In |