[eluser]tdktank59[/eluser]
So I've just started using Code Igniter recently and I figured I'd start with a simple login form with validation.
Yes I know there is backend pro and what not, but I'm teaching myself by doing.
So basicaly ive gotten everything from the user guide
authentication.php
Code:
<?php
class Authentication extends Controller {
function Authentication()
{
parent::Controller();
}
function index()
{
/*$this->session->set_userdata('login',FALSE);
$data = array( 'login' => $this->session->userdata('login') );
$this->load->view('authentication_message',$data);*/
if ($this->session->userdata('login') == TRUE)
{
redirect('welcome','refresh');
}
else
{
$this->login();
}
}
function login()
{
$this->load->library('validation');
$rules['username'] = "trim|required|min_length[5]|max_length[25]|callback_username_check";
$rules['password'] = "trim|required|min_length[5]|max_length[25]|md5";
$this->validation->set_rules($rules);
$fields['username'] = 'Username';
$fields['password'] = 'Password';
$this->validation->set_fields($fields);
$this->validation->set_error_delimiters('<div class="error">', '</div>');
if ($this->validation->run() == FALSE)
{
$data = array ( 'param' => array( 'username' => array( 'name' => 'username',
'id' => 'username',
'value' => $this->validation->username,
'maxlength' => '25',
'size' => '20'),
'password' => array( 'name' => 'password',
'id' => 'password',
'value' => $this->validation->password,
'maxlength' => '25',
'size' => '20'),
'submit' => array( 'name' => 'submit',
'id' => 'submit',
'value' => 'Submit')
));
$this->load->view('login_form',$data);
}
else
{
$this->load->view('formsuccess');
}
}
function username_check($username)
{
if ($username == 'username')
{
return TRUE;
}
else
{
$this->validation->set_message('username_check', 'Invalid username used');
return FALSE;
}
}
}
login_form.php
Code:
<html>
<head>
<title>My Form</title>
<style type="text/css">
body {
font-family: monospace;
font-size: 12px;
}
ul {
padding: 0px;
margin: 0px;
list-style: none;
}
ul li {
padding: 2px;
}
#form {
width: 300px;
margin: auto auto auto auto;
position: relative;
}
fieldset {
border: 1px solid #CCCCCC;
}
legend {
font-weight: bold;
font-size: 16px;
}
form {
margin: 0px;
padding: 0px;
}
.login label {
font-weight: bold;
}
.error {
width: 300px;
margin: auto auto auto auto;
position: relative;
background-color: #FFFED0;
border: 1px dotted #FFA29B;
padding: 5px;
text-align: center;
margin-bottom: 5px;
}
</style>
</head>
<body>
<?php echo $this->validation->error_string; ?>
<div id="form">
<fieldset>
<legend>Login</legend>
<?php echo form_open('authentication/login'); ?>
<ul class="login">
<li><label for="username">Username:<?php echo form_input($param['username']); ?></label></li>
<li><label for="password">Password:<?php echo form_password($param['password']); ?></label></li>
<li><?php echo form_submit($param['submit']); ?></li>
</ul>
<?php echo form_close(); ?>
</fieldset>
</div>
</body>
</html>
However for login I want to match the username with the password.
Using whats already included with code igniter how would I do this?
Note I would be using a database
Table: user
Fields: username, password