Browse Source

Enabled sessions

master
Rogier Neeleman 9 years ago
parent
commit
b622c2ddc9
  1. 5
      README.md
  2. 2
      application/config/config_example.php
  3. 61
      application/controllers/Login.php
  4. 13
      application/models/Login_model.php
  5. 8
      application/views/login.php

5
README.md

@ -7,5 +7,8 @@ This is a score system for scout patrols in the Netherlands.
* Copy the whole tree to a webserver with php support. * Copy the whole tree to a webserver with php support.
* In /application/config/ copy config_example.php to config.php. * In /application/config/ copy config_example.php to config.php.
* In /application/config/ copy database_example.php to database.php. * In /application/config/ copy database_example.php to database.php.
* Edit the config.php and edit the variable base_url, language. * Edit the config.php and edit the variables:
* base_url, the URL of the website.
* language, your language. Dutch or English.
* sess_*, your session cookie preferences.
* Edit the database.php to your needs. * Edit the database.php to your needs.

2
application/config/config_example.php

@ -368,7 +368,7 @@ $config['encryption_key'] = '';
| |
*/ */
$config['sess_driver'] = 'files'; $config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session'; $config['sess_cookie_name'] = 'session';
$config['sess_expiration'] = 7200; $config['sess_expiration'] = 7200;
$config['sess_save_path'] = NULL; $config['sess_save_path'] = NULL;
$config['sess_match_ip'] = FALSE; $config['sess_match_ip'] = FALSE;

61
application/controllers/Login.php

@ -13,6 +13,31 @@ class Login extends CI_Controller
} }
public function index() public function index()
{
// redirect if session exists
$this->load->library('session');
if(! $this->session->userdata('validated')){
redirect('/dashboard');
}
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if (! ($this->input->post('username') AND $this->input->post('password')))
{
$this->_showlogin();
}
elseif ($this->form_validation->run() == FALSE)
{
$this->_showlogin('No username or password.');
} else {
// check login
$this->_checklogin();
}
}
private function _showlogin($errormsg = NULL)
{ {
// Load model // Load model
$this->load->model('Regio_model'); $this->load->model('Regio_model');
@ -23,20 +48,16 @@ class Login extends CI_Controller
$data['regio'][$regio['id']] = $regio['naam']; $data['regio'][$regio['id']] = $regio['naam'];
} }
if ($errormsg)
{
$data['errormsg'] = $errormsg;
}
// Header // Header
$this->load->view('header'); $this->load->view('header');
$this->form_validation->set_rules('username', 'Username', 'required'); // login page
$this->form_validation->set_rules('password', 'Password', 'required'); $this->load->view('login', $data);
if ($this->form_validation->run() == FALSE)
{
// login page
$this->load->view('login', $data);
} else {
// check login
$this->_checklogin();
}
// Footer // Footer
$this->load->view('footer'); $this->load->view('footer');
@ -46,15 +67,25 @@ class Login extends CI_Controller
{ {
// Load model // Load model
$this->load->model('Login_model'); $this->load->model('Login_model');
$logincheck = $this->Login_model->check_user(); $userdata = $this->Login_model->check_user();
if ($logincheck == FALSE) { if ($userdata == FALSE) {
echo "No user "; $this->_showlogin('Wrong username or password');
} else { } else {
echo "Ok"; $this->_startsession($userdata);
} }
} }
private function _startsession($userdata)
{
// Start session with user data
$this->load->library('session');
$this->session->set_userdata($userdata);
// Redirect to dashboard
redirect('/dashboard');
}
} }
?> ?>

13
application/models/Login_model.php

@ -20,7 +20,7 @@ class Login_model extends CI_Model
$regioid = $this->security->xss_clean($this->input->post('regioid')); $regioid = $this->security->xss_clean($this->input->post('regioid'));
// Search for user // Search for user
$this->db->select('id'); $this->db->select('id, regioid');
$this->db->where('username', $username); $this->db->where('username', $username);
$this->db->where('password', sha1($password)); $this->db->where('password', sha1($password));
$this->db->where('regioid', $regioid); $this->db->where('regioid', $regioid);
@ -30,7 +30,16 @@ class Login_model extends CI_Model
if ($query->num_rows() == 1) if ($query->num_rows() == 1)
{ {
return TRUE; $row = $query->row();
$userdata = array(
'id' => $row->id,
'regio' => $row->regioid,
'validated' => true
);
// Sent user information
return $userdata;
} else { } else {
return FALSE; return FALSE;
} }

8
application/views/login.php

@ -8,7 +8,13 @@
<div class='col_3'> <div class='col_3'>
</div> </div>
<div class='col_4 right'> <div class='col_4 right'>
<?php echo validation_errors(); ?> <?php
// Viewing error's
if (isset($errormsg)) { ?>
<div class="notice error">
<?php echo $errormsg; ?>
</div>
<?php } ?>
<?php echo form_open(base_url('login/')); ?> <?php echo form_open(base_url('login/')); ?>
<label for='text2'>Username:</label> <label for='text2'>Username:</label>
<?php echo form_input('username'); ?><br> <?php echo form_input('username'); ?><br>

Loading…
Cancel
Save