[eluser]codeign00b[/eluser]
Hello everybody
I'm looking for a help in authentication on my site. I have site where users must login to be able to access other part of the site. The problem is when I try to sign in I get the blank page. Sessions are stored in db but it seems like user is not recognized or something. If I write anything in e-mail and password field I get the same blank page.
Here is my code:
Controler (1):
Code:
<?php
class Reports extends CI_Controller {
function index()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if (!isset($is_logged_in) || $is_logged_in != true)
{
$this->session->set_userdata('redirect_url', 'reports');
redirect('signin');
die();
}
//reports controller part
//Load reports page
$data['main_content'] = 'view_reports';
$this->load->view('view_main', $data);
}
}
Signin controller:
Code:
<?php
class Signin extends CI_Controller {
function index()
{
//load libraries
$this->load->library('form_validation');
$this->load->library('session');
//Form validation rules
$this->form_validation->set_rules('useremail', 'E-mail', 'trim|required|valid_email|xss_clean');
$this->form_validation->set_rules('userpassword', 'Password', 'trim|required|xss_clean');
//Run Validation
if ($this->form_validation->run() == FALSE)
{
//page has not run or validation error
$data['main_content'] = 'view_signin';
$this->load->view('view_mainindex', $data);
}
else
{
$this->load->model('model_signin');
$query = $this->model_signin->validate();
if($query) //If user's credentials validate
{
$useremail = $this->input->post('useremail');
$this->session->set_userdata('is_logged_in','true');
$this->session->set_userdata('useremail', $this->input->post('useremail'));
redirect('reports');
}
else
{
$data['errors'] = '<p>The email address or password you entered is incorrect.</p>';
$data['main_content'] = 'view_signin';
$this->load->view('view_mainindex', $data);
}
}
}
function is_logged_in()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if (!isset($is_logged_in) || $is_logged_in != true)
{
redirect('signin');
die();
}
}
}
Signin model:
Code:
<?php
class Model_signin extends Model{
function validate()
{
$this->load->helper('date');
$this->load->helper('url');
$this->load->library('encrypt');
$salt = $this->config->item('salt');
$this->db->where('useremail', $this->input->post('useremail'));
$password = $this->input->post('userpassword');
$sha1_password = $this->encrypt->sha1($salt.$password);
$this->db->where('userpassword', $sha1_password);
$query = $this->db->get('user');
if($query->num_rows == 1)
{
//get userid and add it to session data
$this->db->where('useremail', $this->input->post('useremail'));
$password = $this->input->post('userpassword');
$sha1_password = $this->encrypt->sha1($salt.$password);
$this->db->where('userpassword', $sha1_password);
$userid = $this->db->get('user')->row()->userid;
//add timestamp to last loggedin field in user table
$data = array(
'last_login' => now(),
'last_ip' => $this->input->ip_address()
);
$this->db->where('userid', $userid);
$this->db->update('user', $data);
//set userid into session
$this->session->set_userdata('userid',$userid);
return true;
}
else
{
return false;
}
}
config.php:
Code:
$config['sess_cookie_name'] = 'somesite';
$config['sess_expiration'] = 0;
$config['sess_timeout'] = 7200;
$config['sess_encrypt_cookie'] = TRUE;
$config['sess_use_database'] = TRUE;
$config['sess_storage'] = 'database';
$config['sess_database'] = 'default';
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;
$config['sess_http_only'] = TRUE;
$config['sess_secure'] = TRUE;
What is wrong with this?