Home >>Codeigniter Tutorial >Create login form in Codeigniter

Create login form in Codeigniter


Create login form in Codeigniter

In this tutorial we will understand how to create simple login form in CodeIgniter. For login page we will check student's email and password is valid then redirect on dashboard method. otherwise display an error message on login view page. We will use student table to login This is student table structure
Table Name : student
student_id int primary key auto_increment
name char(50)
email varchar(100)
password varchar(100)
mobile bigint
course char(100)

Controller

Copy the below given code in your User Controller.
<?php
class User extends CI_Controller 
{
	public function __construct()
	{
	parent::__construct();
	$this->load->database();
	$this->load->helper('url');
	}

	public function login()
	{
		
		if($this->input->post('login'))
		{
			$e=$this->input->post('email');
			$p=$this->input->post('pass');
	
			$que=$this->db->query("select * from student where email='".$e."' and password='$p'");
			$row = $que->num_rows();
			if($row)
			{
			redirect('User/dashboard');
			}
			else
			{
		$data['error']="<h3 style='color:red'>Invalid login details</h3>";
			}	
		}
		$this->load->view('login',@$data);		
	}
	
	function dashboard()
	{
	$this->load->view('dashboard');
	}
}
?>

Login View

Copy the below given code in your login.php View Page.
<!DOCTYPE html>
<html>
<head>
<title>Login form</title>
</head>

<body>
	<form method="post">
<table width="600" align="center" border="1" cellspacing="5" cellpadding="5">
 <tr>
		<td colspan="2"><?php echo @$error; ?></td>
	</tr>
  <tr>
    <td>Enter Your Email </td>
    <td><input type="text" name="email"/></td>
 </tr>

 <tr>
    <td width="230">Enter Your Password </td>
    <td width="329"><input type="password" name="pass"/></td>
  </tr> 
   <tr>
    <td colspan="2" align="center">
	<input type="submit" name="login" value="Login"/></td>
  </tr>
</table>

	</form>
</body>
</html>

Execute User Controller’s login method

Open your web browser and Pass : http://localhost/CodeIgniter/index.php/user/login
Output(if wrong email or Password)

Dashboard View

Copy the below given code in your dashboard.php View Page.
<!DOCTYPE html>
<html>
<head>
<title>Student Dashboard</title>
</head>

<body>
	<h1>Welcome to your dashboard...</h1>
</body>
</html>

Output(Dashboard page)

Popular Tutorials