• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ok to set userdata session variables in admin controller?

#1
[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']; ?>

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

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

#4
[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 ?>


#5
[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

#6
[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.

#7
[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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.