[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:
<?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!