[eluser]Unknown[/eluser]
i am facing a problem in the follwing code that when my fields of username is either empty of any other name the session still gets created and account gets logined.
CONTROLLER:
function Addressbook()
{
parent::Controller();
$this->load->library('encodes');
$this->load->library('contacts');
$this->load->model('address_model');
$this->load->model('search_model');
$this->load->library('session');
}
function index()
{
if ($this->session->userdata('logged_in') == TRUE)
{
redirect('login/index');
}
$data['username'] = array('id' => 'username', 'name' => 'username');
$data['password'] = array('id' => 'password', 'name' => 'password');
$data['c'] = $this->address_model->index_get();
$this->load->view('addressbook',$data);
}
function process_login()
{
$username1 = $this->input->post('username');
$password1 = $this->input->post('password');
$s1 = $this->address_model->validate($username1,$password1);
return $s1;
}
function logout()
{
$this->session->sess_destroy();
redirect('addressbook/index');
}
MODEL:
function index_get()
{
$s = $this->db->get('addressbook');
return $s;
}
function validate($username1,$password1)
{
$query = $this->db->get_where('addressbook', array('email' => $username1));// change db to addressbook
foreach($query->result() as $row)
{
$t = $row->password;
}
if ($password1 == $t)
{
$data = array('username' => $username1,'logged_in' => TRUE,'flag' => TRUE);
$this->session->set_userdata($data);
redirect('addressbook/index');
}
else
{
$this->session->set_flashdata('message', '<div id="message">you have entered invalid username or password, please try again.</div>');
redirect('addressbook/index');
}
}
VIEW:
<?php $this->load->view("header");
?>
<?php echo form_open('addressbook/process_login') . "\n"; ?>
<?php echo $this->session->flashdata('message'); ?>
<p><label for="username">Username: </label><?php echo form_input($username); ?></p>
<p><label for="password">Password: </label><?php echo form_password($password); ?></p>
<p><input type="submit" value="Submit" /></p>
<P><h3><?php echo anchor('addressbook','Back'); ?></h3></P>
<?php echo form_close(); ?>
<html>
<head>
</head>
<body>
<table>
<?php
Print "<thead>";
Print "<tr>";
Print "<th>S.NO</th>";
Print "<th>NAME</th>";
Print "</tr>";
Print "</thead>";
foreach ($c->result() as $row)
{
Print "<tr>";
Print "<td>";
echo anchor('addressbook',$row->id);
Print"</td>";
Print"<td>";
echo anchor('addressbook/info/'.$row->id,$row->name." ".$row->midname." ".$row->lastname);
Print"</td>";
Print "</tr>";
}
?>
</table>
</body>
</html>