CodeIgniter Forums
Ok to set userdata session variables in admin controller? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Ok to set userdata session variables in admin controller? (/showthread.php?tid=52671)



Ok to set userdata session variables in admin controller? - El Forum - 06-20-2012

[eluser]gwerner[/eluser]
I'm setting a couple of session variables to the data array in my Admin_Controller to access in all admin view files. I was going to put in my controllers, but seems redundant to place in each. Another thought was to place them in my header template within my views. Ultimately I think this is the better way to access throughout all my admin pages. Is this a secure/ok way to implement?

Code:
class Admin_Controller extends MY_Controller {

function __construct()
    {
        parent::__construct();
        
  if($this->session->userdata('logged_in'))
  {
   $session_data = $this->session->userdata('logged_in');
   $this->data['user'] = $session_data['user'];
   $this->data['login'] = $session_data['login'];
   $this->data['username'] = $session_data['first'] . " " . $session_data['last'];
  }
  else
  {
   redirect('access/login', 'refresh');
  }
    }
}

I then have access to the variable in my views via

Code:
<?php echo $this->data['username']; ?>



Ok to set userdata session variables in admin controller? - El Forum - 06-20-2012

[eluser]Karman de Lange[/eluser]
deleted, no sleep answer


Ok to set userdata session variables in admin controller? - El Forum - 06-20-2012

[eluser]CodeIgniteMe[/eluser]
The best way if to pass data as variables like instructed on the UserGuide Views
because having access to the $this object from the view can create accidental overwriting of the properties of $this or $this itself. therefor violating the MVC model.

I am only pertaining to the way you pass the data though. But as long as you know what you are doing, you can bravely experiment.


Ok to set userdata session variables in admin controller? - El Forum - 06-20-2012

[eluser]Karman de Lange[/eluser]
Have look at:
Code:
$this->load->vars($array)
Passes array of variables to access in your views:

Not tried but this might work;

Code:
$session_data = $this->session->userdata('logged_in');

$this->load->vars($session_data)
then in the view you will have
Code:
Username: <?php echo $usernane ?>




Ok to set userdata session variables in admin controller? - El Forum - 06-20-2012

[eluser]CodeIgniteMe[/eluser]
[quote author="Karman de Lange" date="1340259175"]Have look at:
Code:
$this->load->vars($array)
Passes array of variables to access in your views:

Not tried but this might work;

Code:
$session_data = $this->session->userdata('logged_in');

$this->load->vars($session_data)
then in the view you will have
Code:
Username: <?php echo $usernane ?>

[/quote]

correct, this is a much acceptable solution


Ok to set userdata session variables in admin controller? - El Forum - 06-21-2012

[eluser]gwerner[/eluser]
[quote author="Karman de Lange" date="1340259175"]Have look at:
Code:
$this->load->vars($array)
Passes array of variables to access in your views:

Not tried but this might work;

Code:
$session_data = $this->session->userdata('logged_in');

$this->load->vars($session_data)
then in the view you will have
Code:
Username: <?php echo $usernane ?>

[/quote]

This works like a charm.

[quote author="CodeIgniteMe" date="1340258493"]The best way if to pass data as variables like instructed on the UserGuide Views
because having access to the $this object from the view can create accidental overwriting of the properties of $this or $this itself. therefor violating the MVC model.

I am only pertaining to the way you pass the data though. But as long as you know what you are doing, you can bravely experiment.[/quote]

Thanks for pointing this out. Makes perfect sense. I didn't have a good feeling about doing it that way, but was the only way I was getting it to work without the above solution. Thanks again.


Ok to set userdata session variables in admin controller? - El Forum - 06-21-2012

[eluser]CodeIgniteMe[/eluser]
Good to know Smile
Again, you can experiment on things as long as you know and understand what you are doing. Experimenting on things is a sign of good development health.