Welcome Guest, Not a member yet? Register   Sign In
Little help with sessions
#1

[eluser]knucklehead[/eluser]
Hello!
I have in plan to make some kind of CMS for my site which is developed in CI. I am little confused about handling sessions so please tell me if I am doing right thing here;

I am using MY_Controller, and inside of that file I added another class:
Code:
// above is MY_Controller... Admin_Controller, is used to be a 'template' for entire CMS...
class Admin_Controller extends MY_Controller {
    
    function Admin_Controller () {
        parent::MY_Controller();
        
        $this->load->library('session');
        
        $this->data['sessionID'] = "";
        $this->data['menu'] = "";
        $this->data['footer'] = "footer text"; // some custom text
        $this->load->view('admin/admin_home', $this->data, TRUE);
    }
}

My admin controller looks like this:
Code:
<?php
class Admin extends Admin_Controller {
    
    function __construct() {
        parent::Admin_Controller();
    }
    
    function index() {
        $welcome_message = "welcome message"; // just some custom message
        
        $this->load->helper('form', 'url');        
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('username', 'Username', 'callback_login_check|trim|required|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|md5');
        
        if ($this->form_validation->run() == FALSE) {
            
            $newdata = array('user_logged' => FALSE);
            $this->session->set_userdata($newdata);
            $this->data['user_logged'] = $this->session->userdata('user_logged');
            
            $this->data['admin_main_view'] = $this->load->view('admin/admin_login', NULL, TRUE);
            $this->load->view('admin/admin_home', $this->data);
            
        } else {
            $this->data['menu'] = $this->load->view('admin/admin_menu', NULL, TRUE);
            $this->data['admin_main_view'] = $welcome_message;
            $this->load->view('admin/admin_home', $this->data);
        }
    }
    
    function login_check($str) {
        $user = $this->input->post('username');
        $pass = $this->input->post('password');
        $query = $this->db->query("SELECT username,password FROM users WHERE username='$user' AND password=md5('$pass')");
        if($query->num_rows() == 1) {
            $newdata = array('user_logged' => TRUE);
            $this->session->set_userdata($newdata);
            $this->data['user_logged'] = $this->session->userdata('user_logged');
            return TRUE;
        } else {
            return FALSE;
        }
    }
}

/* EOF */

My admin_home.php file is really simple, basically it looks like this:
Code:
<?php
if ($user_logged == TRUE) {
    echo $menu;
}
echo $admin_main_view;
echo $footer;
?>

And if I want to load some other page from my menu (let's call it 'news'), I do it like this:
Code:
<?php
class News extends Admin_Controller {
    
    function __construct() {
        parent::Admin_Controller();
    }
    
    function index() {
        if ($this->session->userdata('user_logged') == TRUE){
            $this->data['user_logged'] = $this->session->userdata('user_logged');
            $this->data['menu'] = $this->load->view('admin/admin_menu', NULL, TRUE);
            $this->data['admin_main_view'] = $this->load->view('admin/admin_news', NULL, TRUE);
        } else {
            $this->data['user_logged'] = $this->session->userdata('user_logged');
            $this->data['menu'] = "";
            $this->data['admin_main_view'] = $this->load->view('admin/admin_home', NULL, TRUE);
        }
        $this->load->view('admin/admin_home', $this->data);
    }
    
}

/* EOF */

Since I had lots of localized names in my variables there could be a small typo in my examples, but I need to know if I understand the logic correctly here...
I am not sure if I am handling session data correctly??

Thank you very much for any help/tips/advices....!!
#2

[eluser]flaky[/eluser]
If the sessions are working (and doing their job as you intended), then you are managing them well.
As I can see and understand from the code samples, you are handling the session well.
#3

[eluser]knucklehead[/eluser]
ok... thanks for help flaky Smile i consider this resolved now, i guess... Smile
#4

[eluser]Dyllon[/eluser]
[quote author="EllisLab" date=""]The Model represents your data structures. Typically your model classes will contain functions that help you retrieve, insert, and update information in your database.[/quote]

Models are great, use them to help keep your controllers clean and free of maintenance nightmares like queries.




Theme © iAndrew 2016 - Forum software by © MyBB