Saturday, September 17, 2011

Login Form CodeIgniter 2.0.2

Create link for Form Login

application/controller/main.php

function login()
{
$this->load->view('layout/head');
$this->load->view('login');
$this->load->view('layout/foot');
}


Create Form Login<

application/view/login.php:

<?php
$this->load->helper('html');
$this->load->helper('url');
$this->load->helper('form');
echo doctype('html4-strict');

echo"<html> <body>";

$act_form='index.php/main/pro_login';


echo form_open($act_form);

echo"

<table width='80%' border='0'>
<tr>
<td width='40'>". form_label('Username')."</td>
<td>: " . form_input('user', '', 'id=user') ."</td>
</tr>

<tr>
<td width='40'>". form_label('Password')."</td>
<td>: " . form_password('pass', '', 'id=pass') ."</td>
</tr>

<tr>
<td colspan='2'>".form_submit('mysubmit', 'SIMPAN')."</td>

</tr>

</table>
";

echo form_close();
?>
</body>
</html>

Create function pro_login for handle form login.
application/controller/main.php:

function pro_login()
{

$this->load->helper('url');

$username=$this->input->post('user');
$password=$this->input->post('pass');

if($this->main_model->prologin($username, $password))
{
$this->load->library('session');
$this->session->set_userdata('username', $username);
redirect(base_url().'index.php/main/loginarea', 'refresh');
}
else
{echo"Restricted Area";}

}


Create model for function pro_login().
application/models/main_model.php:

function prologin($user='', $pass='')
{

$this->db->where(array ('user'=>$user, 'pass'=>$pass));

$log=$this->db->get('user');
if($log->num_rows()==1)
{return TRUE;}
else
{return FALSE;}
}


Create function for success login
application/controller/main.php:

function loginarea()
{
$this->load->view('layout/head');
$this->load->view('loginarea');
$this->load->view('layout/foot');
}


Create Login Area
application/views/loginarea.php:

<html>
</head>
</head>

<body>
Welcome
<?php
$this->load->library('session');
echo $this->session->userdata('username');
?>
</body>
</html>

0 komentar:

Post a Comment

Thanks a lot for your attention...

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Best Web Hosting