Creating Login form

php-login-formCreating a loginform in php is required in any normal PHP dynamic site. Following tutorial describe all the necessary steps to create login system in PHP site.

Section 1: Creating table of ‘members’ inside phpmyadmin

We need a databse table for all members records,login system.
‘members’ table will have following fields

 mid
 name
 email
 password
 contact
 address
 

Below screenshot for ‘members’ table to be created in phpmyadmin

php-mysql-login-database-table

section:2 Creating PHP loginform

Here we have to create a login form using HTML and PHP.Find below code for this

  <?php


if(isset($_POST['submit'])){
	
	
	$email=$_POST['email'];
	$password=$_POST['password'];
	
	
	$user->login($email,$password);
	
	$user->register();
	
	
}
 
 

?>

     <input type="text" name="email" value="<?php echo $_POST['email']; ?>" />
     <form action="" method="post">
<table width="244" height="107" border="0">
  <tr>
    <td>Email</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>password</td>
    <td><input type="password" name="password" value="<?php echo $_POST['password']; ?>"/></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="submit" value="Login" /></td>
  </tr>
</table>

 
</form>
       
 
 
 

in above code

 
if(isset($_POST['submit'])){
	
	
	$email=$_POST['email'];
	$password=$_POST['password'];
	
	}
 
 

It checks wheather user clicked submit button,after we get form field values of email and password.

php mysql video tutorial in Hindi language by UnitedWebSoft



Now here above we used PHP OOPS concept. We have created an object. access it’s method i.e ‘login’. Sent 2 parameters email and password.
So there has not too much login in this page. All our login codes inside in our class.php file.It looks like below

 <?php
 class user{
	
	function login($email,$password){
		$error=array();
	
		if($email==''){
		$error[]='email field is required';
	}
	
	if($password==''){
		$error[]='password field is required';
	}
	
	 
	
	$count=count($error);
	
	if($count>0){
		foreach($error as $value){
			echo $value."<br/>";
		}
	}else{
		//no error
		$q="SELECT * FROM members WHERE email='$email' AND password='$password'";
		$r=mysql_query($q);
		
		$count=mysql_num_rows($r); 
		
		if($count>0){
			$row=mysql_fetch_array($r);
		
			//matched
			$_SESSION['mid']=$row['mid'];
			header("Location:profile.php");
			
		}else{
			//error not valid login details
			echo "Sorry Invalid Login Details";
			
		}
		
		
	}
	
	}
	?>
 
 

From above

 class user{
	
	function login($email,$password){
		$error=array();
	
 

We have created a class ‘user’ and a method ‘login’.Then defined a null array for array validation .

 	if($email==''){
		$error[]='email field is required';
	}
	
	if($password==''){
		$error[]='password field is required';
	}
	
	 
	
	$count=count($error);
	
 
 

Here above we have checked whether user has left blank field for email and password. If user left blank, we store a message inside our ‘error’ array. Finally We have count no of value inside array.

 if($count>0){

		foreach($error as $value){
			echo $value."<br/>";
		}
	}else{
		//no error
		$q="SELECT * FROM members WHERE email='$email' AND password='$password'";
		$r=mysql_query($q);
		
		$count=mysql_num_rows($r); 
		
		if($count>0){
			$row=mysql_fetch_array($r);
		
			//matched
			$_SESSION['mid']=$row['mid'];
			header("Location:profile.php");
			
		}else{
			//error not valid login details
			foreach($error as $value){
				echo $value."<br/>";
			}
			
		}
 
 

Above we are checking whether count variable have some value or not. If count have value greater than ‘0’ . It means there have some error. So we have shown error using foreach loop. In else part ,we have created a session ‘mid’ and stored user ‘mid’ value from database table.We have fetched user row details using $row=mysql_fetch_array($r);.

So , after successfully login in user will be redirected to profile.php file where we will show user’s profile data like
name,email,address,contact etc.
I hope this tutorial will help you understanding PHP Login system.In next tutorial i will write about my profile page