[eluser]ShoeLace1291[/eluser]
I have a login form that has two fields: email and password. Here are some descriptions of the callbacks I use for the email field:
email_exists: Finds number of rows in the table where email = string
is_active: if the column in the member's table row called "isActive" = 0, return false(member account is inactive
Callback for password:
correct_pass: Finds row based on post input from email field and returns false if provided password does not match password on record.
This is my controller:
Code:
<?php
class Login extends Controller {
function index(){
$this->template->head($title = "Member Login", FALSE);
if(($this->config->item('maintenance_mode') == TRUE) && ($this->session->userdata('permission') != 1)){
$variables = array(
'ERROR_HEADING' => "Maintenance Mode",
'ERROR_MESSAGE' => $this->config->item('maintenance_message')
);
$this->parser->parse('error_body', $variables);
} else {
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email Address', 'required|valid_email|email_exists|is_active');
$this->form_validation->set_rules('password', 'Password', 'required|correct_pass');
if($this->form_validation->run() == FALSE){
$variables = array(
'FORM_OPEN' => form_open('login', array('onsubmit' => 'validateLogin();')),
'VALIDATION_ERRORS' => validation_errors()
);
$this->parser->parse('form_login', $variables);
} else {
if(!$this->member->login($this->input->post('email'))){
$error = array(
'ERROR_HEADING' => "System Error",
'ERROR_MESSAGE' => "The system encountered an error. Please try again later.".mysql_error()
);
$this->parser->parse('error_body', $error);
} else {
$error = array(
'ERROR_HEADING' => "Thank You!",
'ERROR_MESSAGE' => "Welcome back! You are now being redirected back to the index.".redirect('main', 'location')
);
$this->parser->parse('error_body', $error);
}
}
}
$this->load->view('overall_footer');
}
function email_exists($str){
$query = $this->db->query("SELECT * FROM members WHERE emailAddress = '".$str."'");
if($query->num_rows() == 0){
$this->form_validation->set_message('email_exists', 'The email address you provided does not exist in our database.');
return FALSE;
} else {
return TRUE;
}
}
function is_active($str){
$query = $this->db->query("SELECT * FROM members WHERE emailAddress = '".$str."' LIMIT 1");
$member = $query->row();
if($query->num_rows() == 0){
$this->form_validation->set_message('is_active', 'The email address you provided does not exist in our database.');
return FALSE;
} else if($member->isActive == 0){
$this->form_validation->set_message('is_active', 'The account you are trying to login to has not been activated.');
return FALSE;
} else {
return TRUE;
}
}
function correct_pass($str){
$query = $this->db->query("SELECT * FROM members WHERE emailAddress = '[email protected]'");
$member = $query->row();
if($query->num_rows() == 0){
return FALSE;
} else if(sha1($str) != $member->password){
$this->form_validation->set_message('correct_pass', 'The password you entered is incorrect.');
return FALSE;
} else {
return TRUE;
}
}
}
This is my form:
Code:
<div id='mainBody'>
<div id='wideBox'>
<h1>Member Login</h1>
<ul>
{VALIDATION_ERRORS}
</ul>
{FORM_OPEN}
<table align='center' cellspacing='1' cellpadding='3' border='0'>
<tr>
<td align='right'>Email: </td>
<td align='left'><input type='text' name='email' size='30' /></td>
</tr><tr>
<td align='right'>Password: </td>
<td align='left'><input type='password' name='password' size='30' /></td>
</tr><tr>
<td align='center' colspan='2'><input type='submit' name='submit' value='Login' /></td>
</tr>
</table>
</div>
</div>