Welcome Guest, Not a member yet? Register   Sign In
Trying to Get Session Information to the View
#11

[eluser]pickupman[/eluser]
Another quick tip you can use in MY_Controller for using a variable/property in every controller that extends MY_Controller is using [url="http://codeigniter.com/nightly_user_guide/libraries/loader.html#this-load-vars-array"]$this->load->vars()[/url]:
Code:
class MY_Controller extends CI_controller {
    
  public function __construct()
  {
     parent::__construct();
      $data['user_info']['display_name'] = $this->session->userdata('display_name');
      $data['user_info']['user_id'] = $this->session->userdata('user_id');
      
      //Pass $data array in construct to your views
      $this->load->vars($data);
    }
}

Your code above for MY_Controller would only work if you were passing $this->data to your view rather than $data. If you used $this->data inside your construct and the other methods, just make sure you are passing $this->data to your view.
Code:
$this->load->view('templates/full_layout', $this->data);

I prefer the more OOP style, as you can access it from with in the methods. As a suggestion I would have auth_model->login_check() return the user object or FALSE when not found. Then you don't have to make another call for the user information.
#12

[eluser]php_princess[/eluser]
Ok, thank you for the tip, but do you know how to fix my session problem?
#13

[eluser]pickupman[/eluser]
There are a number of issues with the session class but it will work when setup correctly. In your configuration.php file try changing your cookie name to cisession (remove underscore). Then set the expiration time out further. Internet Explorer will check the time between the server and your computer local time. The default expiration maybe less than that time offset.
#14

[eluser]php_princess[/eluser]
I have my cookie name set to "session". Here's all my session variables:

Code:
$config['sess_cookie_name']  = 'session';
$config['sess_expiration']  = 0;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie'] = TRUE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name']  = 'ci_sessions';
$config['sess_match_ip']  = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;
#15

[eluser]php_princess[/eluser]
Sooooooooo anything in my last post jump out at you guys as what could be causing the userdata user_id and display_name to be deleted when I go to the next page? If not, what part of my code should I show you?
#16

[eluser]pickupman[/eluser]
Try setting the match_user_agent to FALSE and see if that helps. I assume you have tried echoing the session data out to see if it is disappearing somewhere. Also watch your session id. Is it changing each page request or is it staying the same.
#17

[eluser]php_princess[/eluser]
I noticed something. When I do $this->session->add_userdata() it adds the information to the session that is already running, eventhough this code comes after $this->session->sess_destroy();

How can I make it so CodeIgniter will add the user data to the new session, or is there just no way to do this and I'll have to just make sure the user has already logged out before my script makes a new session.

My code:
Code:
$this->session->sess_destroy();
    $this->load->library('session');
    $user_id = $this->auth_model->get_user_id($this->input->post('login_name'));
    $this->session->set_userdata('user_id', $user_id);
    $this->session->set_userdata('display_name', $this->auth_model->get_display_name($user_id));
    
    $data['alert_type'] = 'success';
    $data['main_content'] = 'auth/login_success';
    $data['alert_message'] = 'Hello '.$this->session->userdata('display_name').'! You have been logged in.';
#18

[eluser]pickupman[/eluser]
Usually destroying a session should be done on redirect like when logging out. Remove the line destroying the session, and see if that works. If it does, just unset the userdata key for the data points you are wanting to recreate for security purposes.

Code:
//Remove existing userdata
$this->session->unset_userdata('user_id');
$this->session->unset_userdata('display_name');

    $user_id = $this->auth_model->get_user_id($this->input->post('login_name'));
    $this->session->set_userdata('user_id', $user_id);
    $this->session->set_userdata('display_name', $this->auth_model->get_display_name($user_id));
    
    $data['alert_type'] = 'success';
    $data['main_content'] = 'auth/login_success';
    $data['alert_message'] = 'Hello '.$this->session->userdata('display_name').'! You have been logged in.';

Usually a session has to be started before any output has been sent to the browser, and that's why it is usually done on a redirect like a logout link. By unsetting the keys, you are achieving the same result with have to try and start a new session.
#19

[eluser]TWP Marketing[/eluser]
[quote author="php_princess" date="1347507813"]It just occurred to me that I can't use $this->session->userdata('username') in the view. If I need to print a username I can still just do $data['username'] = $this->session->userdata('username') and then the view can get the username from $username, but I would have to do this in every controller because the username is displayed on every page.

How can I have CodeIgniter automatically put this in $data['username']?

...or am I approaching this entirely the wrong way, and you guys have a better idea?

Thanks in advance.[/quote]

If you are using a base controller
( http://philsturgeon.co.uk/blog/2010/02/C...ing-it-DRY ),
you can set your user data once, from the session, and have it available to all controllers which extend the base controller.
#20

[eluser]php_princess[/eluser]
So what's the best way to check if there's an active session, before going through with the login?

Something like this?

Code:
if (!isset($this->session->userdata('session_id'))
{
   $this->load->library('session'); //***Would I use this eventhough my autoloader autoloads the session class?***
   $this->session->set_userdata('user_id', $user_id);
   $this->session->set_userdata('display_name', $display name);
}
else
{
   //Display error message about how you need to log out before logging in.
}

CodeIgniter doesn't seem to have any clear way to start a new session? Like, there is no $this->session->sess_create(); That's why I thought maybe the act of loading the class would make a session.




Theme © iAndrew 2016 - Forum software by © MyBB