06-27-2008, 07:29 AM
[eluser]zimco[/eluser]
I'm trying to create the most basic login area for my application: one admin user and password stored in a mysql table.
I tried following the tutorial by Alex Biddle that uses Erkanaauth but i've mucked it all up and can't get it to work.
Can someone tell me what i am doing wrong and how to fix it?
File locations:
application/libraries/Erkanaauth.php
application/helpers/erkanaauth_helper.php
application/controllers/admin/authcontroller.php
application/controllers/admin/login.php
application/views/admin/content-login.php
The mysql table was created with the following command:
CREATE TABLE `users` (
`id` smallint(5) UNSIGNED NOT NULL AUTO_INCREMENT,
`username` varchar(10) NOT NULL,
`password` varchar(10) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
);
The admin user was inserted into the database with
INSERT INTO users (username, password) VALUES ("admin", sha1("demo"));
Here's the code for my login.php controller:
Here's the code for my login view, content-login.php:
I'm trying to create the most basic login area for my application: one admin user and password stored in a mysql table.
I tried following the tutorial by Alex Biddle that uses Erkanaauth but i've mucked it all up and can't get it to work.
Can someone tell me what i am doing wrong and how to fix it?
File locations:
application/libraries/Erkanaauth.php
application/helpers/erkanaauth_helper.php
application/controllers/admin/authcontroller.php
application/controllers/admin/login.php
application/views/admin/content-login.php
The mysql table was created with the following command:
CREATE TABLE `users` (
`id` smallint(5) UNSIGNED NOT NULL AUTO_INCREMENT,
`username` varchar(10) NOT NULL,
`password` varchar(10) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
);
The admin user was inserted into the database with
INSERT INTO users (username, password) VALUES ("admin", sha1("demo"));
Here's the code for my login.php controller:
Code:
<?php
class Login extends Controller
{
function Login()
{
parent::Controller();
$this->base = $this->config->item('base_url');
$this->css = $this->config->item('css');
$this->load->library('validation');
$this->load->helper('form');
$this->load->library('Erkanaauth');
}
/*======================================================================*\
Function: index()
Purpose: Provide the Login entry page.
Input:
Output: Display login page view
\*======================================================================*/
function index()
{
$login_form_rules = array
(
'username' => 'callback_check_username',
'password' => 'required'
);
$login_form_fields = array
(
'username' => 'username',
'password' => 'password'
);
$this->validation->set_fields($login_form_fields);
$this->validation->set_rules($login_form_rules);
if ($this->validation->run() == FALSE)
{
//Load the view file
$data['css'] = $this->css;
$data['base'] = $this->base;
// set the header view
$data['header'] = $this->load->view('header', '');
// set the page content
$data['content'] = $this->load->view('admin/content-login', $data);
// set the footer view
$data['footer'] = $this->load->view('footer', '');
}
else
{
redirect('admin/success');
}
}
/*======================================================================*\
Function: check_username($username)
Purpose:
Input:
Output:
\*======================================================================*/
function check_username($username)
{
$this->load->helper('security');
$password = dohash($this->input->post('password'));
if ($this->erkanaauth->try_login(array('username' => $username, 'password' => $password)))
{
return True;
}
else
{
$this->validation->set_message('check_username', 'Incorrect login info.');
return False;
}
}
/*======================================================================*\
END
\*======================================================================*/
}
?>
Here's the code for my login view, content-login.php:
Code:
<div id="content">
<div id="loginform">
<h1>Login</h1>
<?php echo $this->validation->error_string; ?>
<?php $attributes = array('id' => 'login'); ?>
<?php echo form_open('admin/index', $attributes); ?>
<p>
<label for="username">Username: </label>
<?php
$username = array(
'username' => 'username',
'maxlength' => '10',
'size' => '25',
);
echo form_input($username);
?>
<p>
<label for="password">Password: </label>
<?php
$password = array(
'password' => 'password',
'maxlength' => '10',
'size' => '25',
);
echo form_input($password);
?>
</p>
<p>
<?php echo form_submit('submit', 'Submit'); ?>
</p>
<?php echo form_close(); ?>
</div>
</div>