Welcome Guest, Not a member yet? Register   Sign In
Why Won't My Sessions Work?
#1

[eluser]ShoeLace1291[/eluser]
For some reason, I can't get my sessions to work. I have set the encryption key in my config file and have also enabled database sessions. It seems like my sessions aren't setting at all, because when I submit my form to login, the session database row for my session doesn't contain any userdata. I always get redirected to the login form when I go to the index of the admin controller, and the login form always goes through, but no session data is created. Maybe it's a problem with my coding and logic... but anyway, here is my codes.

Controller: admins.php
Code:
<?php

class Admins extends CI_Controller {
    
    function __construct(){
        parent::__construct();
        $this->load->model('admin');
    }
    
    function index(){
        
        if($this->admin->is_valid($this->session->userdata('admin_id'))){
            
            redirect('admins/cp', 'refresh');
            
        } else {
            
            redirect('admins/login', 'refresh');
            
        }
    }
    
    function login(){
        
        if($this->admin->is_valid($this->session->userdata('admin_id'))){
            
            redirect('admins/cp', 'refresh');
            
        } else {
            
            if($this->form_validation->run() == FALSE){
                
                $this->load->view('admin_login');
                
            }
        }
    }
    
    function process_login(){
        
        $configs = array(
            'email_address' => $this->input->post('email_address'),
            'password' => $this->input->post('password')
        );
        
        if($this->admin->loggedout()){
            
            $this->admin->initialize($configs);
            
            if($this->admin->login() == FALSE){
                
                $data['message_title'] = 'Login Success';
                $data['message_text'] = $this->admin->success;
                $data['message_redirect'] = 'admins/cp';
                $this->load->view('message_body', $data);
                
            } else {
                
                $data['message_title'] = 'Login Failure';
                $data['message_text'] = $this->admin->failure;
                $data['message_redirect'] = redirect('home', 'refresh');
                $this->load->view('message_body', $data);
                
            }
            
        } else {
            
            redirect('admins/cp', 'refresh');
            
        }
    }
}

Model: Admin.php
Code:
<?php

class Admin extends CI_Model {
    
    var $id = '';
    var $email_address = '';
    var $password = '';
    var $username = '';
    var $ip_address = '';
    
    function initialize($configs = array()){
        
        if(count($configs) > 0){
            
            foreach($configs as $key => $val){
                
                $this->$key = $val;
                
            }
        }
    }
    
    function login(){
        
        if($this->email_address = ''){
            
            $this->failure = 'A valid email address was not provided.';
            return FALSE;
        
        } else {
            
            $query = "
                SELECT
                    admin_id, email_address, password
                FROM betaadmins
                WHERE email_address = '".$this->email_address."'
                LIMIT 1";
                
            if($query = $this->db->query($query)){
                
                if($query->num_rows() > 0){
                    
                    $admin = $query->row_array();
                    if($admin['password'] == sha1($this->password)){
                        
                        $this->session->set_userdata('admin_id', $admin['admin_id']);
                        $this->success = 'You are now logged in and are being redirected to the admin control panel.';
                        return TRUE;
                        
                    } else {
                        
                        $this->failure = 'The password you provided is not correct.';
                        return FALSE;
                    
                    
                    }
                    
                } else {
                    
                    $this->failure = 'The email address you entered does not exist in our database.';
                    return FALSE;
                
                }
                
            } else {
                
                $this->failure = 'The system encountered an error while processing your request.  Please try again later.';
                return FALSE;
            
            }
            
        }
        
    }
    
    function is_valid(){
        
        if($this->session->userdata('admin_id')){
            
            $query = "
                SELECT
                    admin_id
                FROM betaadmins
                WHERE admin_id = ".$this->session->userdata('admin_id')."
                LIMIT 1";
                
            if($query = $this->db->query($query)){
                
                if($query->num_rows() > 0){
                    
                    return TRUE;
                
                } else {
                    
                    return FALSE;
                
                }
                
            } else {
                
                return FALSE;
            
            }
            
        } else {
            
            return FALSE;
        
        }
        
    }
    
    function loggedout(){
        
        if($this->session->userdata('admin_id') == FALSE){
            
            return TRUE;
        
        } else {
            
            return FALSE;
        
        }
        
    }
    
}
#2

[eluser]TWP Marketing[/eluser]
Did you create the session file in your db?
#3

[eluser]ShoeLace1291[/eluser]
Yes, I created the sessions table from the SQL given in the user guide.
#4

[eluser]PhilTem[/eluser]
Code:
function login(){
        
        if($this->email_address = ''){ // must be two equal signs

That's the only error I see so far.
#5

[eluser]TWP Marketing[/eluser]
In your model, I don't see where you set the local var $password. It is used for a test:
Code:
...
   if($admin['password'] == sha1($this->password)){
...
but will always fail unless $this->password contains something valid.
#6

[eluser]ShoeLace1291[/eluser]
I do that by calling the "initialize" function in the controller.

Code:
$configs = array(
            'email_address' => $this->input->post('email_address'),
            'password' => $this->input->post('password')
        );
        
        if($this->admin->loggedout()){
            
            $this->admin->initialize($configs);
#7

[eluser]ShoeLace1291[/eluser]
Thanks, Phil... that fixed it! I hate it when little errors like that cause everything to go haywire... lol.




Theme © iAndrew 2016 - Forum software by © MyBB