application/controllers/login.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Login Controller
*
* @author: Wiredesignz (c) 2008-12-25
*/
class Login extends Controller {
function Login()
{
parent::Controller();
$this->load->model('users_model', 'users');
}
function index()
{
delete_cookie('ci_user'); // kill existing cookie
$path = implode('/', array_slice($this->uri->rsegments, 2)); //get return path
$login = (object) array('username' => '', 'password' => '', 'remember' => '');
$message = 'Enter your Username & Password to continue';
if ($_POST)
{
//all inputs use XSS_clean filter
$login->username = $this->input->post('username', TRUE);
$login->password = md5($this->input->post('password', TRUE)); //hash the password
$login->remember = $this->input->post('remember', TRUE);
if ($this->try_login($login)) redirect($path);
$message = 'Login failed. Please try again!';
}
if ($uid = get_cookie('ci_login', TRUE)) //check for auto-login cookie ('ci_login')
{
$user = $this->users->findBy("`uid` = '{$uid}'");
$login->username = $user->username;
$login->password = $user->password;
if ($this->try_login($login)) redirect($path);
}
$data = array
(
'title' => 'Login',
'username' => '',
'password' => '',
'checked' => '',
'message' => $message,
'action' => site_url().'login/'.$path,
'lost_usr' => site_url().'register/lost-user',
);
$this->load->view('login/form', $data, FALSE);
}
function try_login($login)
{
if ($login->password)
{
//find user, check password & create cookie if valid
if ($user = $this->users->findBy("`username` = '{$login->username}'") AND $login->password == $user->password)
{
set_cookie('ci_user', $user->uid, 0); //cookie expires on browser close
if ($login->remember) set_cookie('ci_login', $user->uid, 86500);
return TRUE;
}
}
return FALSE;
}
}application/views/login/form.php
<style type="text/css">
<!--
#login { font: 12px verdana; margin: 20px }
#login form { margin-top: 6px }
#login input { vertical-align: middle }
#login #sbmt, #login .chk { margin: 3px 6px 3px 70px }
#login .pwd { margin: 2px }
-->
</style>
<div id="login"><?php echo $message."\n"; ?>
<form action="<?php echo $action; ?>" method="post">
<div class="usr"><label for="usr">Username: </label>
<input size="22" type="text" name="username" id="usr" value="<?php echo $username; ?>" /></div>
<div class="pwd"><label for="pwd">Password: </label>
<input size="22" type="password" name="password" id="pwd" value="<?php echo $password; ?>" /></div>
<div class="chk"><input type="checkbox" <?php echo $checked; ?> name="remember" id="chk" value="on" /><label for="chk">Remember this login</label></div>
<div class="sbmt"><input type="submit" id="sbmt" value="Login" /><a href="http://<?php">lost password?</a></div>
</form>
</div>application/config/routes.php
*/
$route['default_controller'] = "welcome";
$route['scaffolding_trigger'] = "";
//login controller route override (enables path back to caller)
$route['login/(.*)'] = 'login/index';mysql users table
--
-- Table structure for table `users`
--
CREATE TABLE `users` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(25) NOT NULL,
`password` varchar(60) NOT NULL,
`fullname` varchar(50) NOT NULL,
`privileges` int(2) NOT NULL,
`uid` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
--
-- Data for table `users`
--
INSERT INTO `users` VALUES (1, 'admin', '21232f297a57a5a743894a0e4a801fc3', 'Administrator', 99, 'be4817c3d37d255db342d419be86185799f9d06c');Password = md5(‘admin’)