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

(This post was last modified: 05-14-2015, 04:53 AM by StratoKyke.)

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.
Reply
#2

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.
Reply
#3

(This post was last modified: 05-14-2015, 09:36 AM by StratoKyke.)

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
Reply
#4

Ok, I so stupid Big Grin ahah that was the solution Big Grin
Reply
#5

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
Reply
#6

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
Reply
#7

(This post was last modified: 05-17-2015, 03:24 AM by StratoKyke.)

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
Reply
#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
#9

(This post was last modified: 05-17-2015, 10:33 AM by StratoKyke.)

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!!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB