Welcome Guest, Not a member yet? Register   Sign In
Problem using Database Sessions
#1

[eluser]Unknown[/eluser]
Hey there,
i am using CodeIgniter for about two weeks and i want to handle my sessions via the Database Method but this doesnt work.

I have a simple login form which is linked to the controller "login" (Code is on bottom). The Login Controller is using the method "process" to handle the login form. Process runs another method called validation in the Model "login_model". The validation method is just checking the typed parameters with the Database. When there are some correct values in the database for username and password the session will get started. The Validation Method returns true to the process method and this method should link to the homescreen view, because the login was ok.

The problem is, that there is no linking to the homescreen view, furthermore the login is correct.

I hope someone can help me with this.

Here is the code.

Controller Login

Code:
function __construct(){
        parent::__construct();
        
        $this->load->model('login_model');
        
        $this->load->helper('url');        
        $this->load->library('session');
    }
    
    public function index($msg = NULL){
        // Load our view to be displayed
        // to the user
        $data['msg'] = $msg;
        $this->load->view('login_view',$data);
    }
    
    public function process(){
        // Validate the user can login
        $result = $this->login_model->validate();
        if(! $result){
            // If user did not validate, then show them login page again
            $msg = '<font color=red>Der Benutzername oder das Passwort waren falsch. <br />Bitte wiederholen Sie den Vorgang</font><br />';
            $this->index($msg);
        }else{
            // If user did validate,  
            // Send them to members area  
            redirect('/home/index');
        }
    }        
}

And the Model

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Author: David Lambauer
* Description: Login model class
*/
class Login_model extends CI_Model{
    function __construct(){
        parent::__construct();
        $this->load->library('session');
    }
    
    public function validate(){
        // grab user input
        
        $this->load->database();
        $username = $this->security->xss_clean($this->input->post('username'));
        $password = $this->security->xss_clean($this->input->post('password'));
        
        
        // Prep the query
        $this->db->where('email', $username);
        $this->db->where('passwort', md5($password));
        
        // Run the query
        $query = $this->db->get('benutzer');
        // Let's check if there are any results
        //echo $query->num_rows;
        if($query->num_rows == 1)
        {
            // If there is a user, then create session data
            $row = $query->row();
            $data = array(      
                     'session_id'    => md5($_SERVER["REMOTE_ADDR"]),
                     'ip_address'    => $_SERVER["REMOTE_ADDR"],
                     'user_agent'    => $row->id,                    
                     'last_activity' => time(),
                     'email' => $row->email,
                     'validated' => true
            );
                    
            $this->session->set_userdata($data);
            
            
            return true;
        }
        // If the previous process did not validate
        // then return false.
        return false;
    }
}

Thanks for helping me!
#2

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums!

Enabling error reporting should help you. At the top of index.php, type:
Code:
ini_set('display_errors', '1');
error_reporting(E_ALL);

Just remember to remove it on the live site.

Code:
$query->num_rows // WRONG

// Sould be:
$query->num_rows()

Enabling error reporting should help you spot simple problems like that. Smile
#3

[eluser]Unknown[/eluser]
Thank you so much! Big Grin You're my daily hero Tongue





Theme © iAndrew 2016 - Forum software by © MyBB