Table of Contents
Creating a login form in PHP is required in any normal PHP dynamic site. Following tutorial describe all the necessary steps to create login system in PHP site.
So there has not too much login in this page. All our login codes inside in our class.php file.It looks like below
From above
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
Section 1: Creating table of 'members' inside phpmyadmin
We need a database 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 
section:2 Creating PHP login form
Here we have to create a login form using HTML and PHP.Find below code for this <?php
if(isset($_POST)){
$email=$_POST;
$password=$_POST;
$user->login($email,$password);
$user->register();
}
?> <input type="text" name="email" value="<?php echo $_POST; ?>" /> <form action="" method="post"> <table width="244" height="107" border="0"> <tr> <td>Email</td> <td> </td> </tr> <tr> <td>password</td> <td><input type="password" name="password" value="<?php echo $_POST; ?>"/></td> </tr> <tr> <td> </td> <td><input type="submit" name="submit" value="Login" /></td> </tr> </table> </form>
in above code if(isset($_POST)){
$email=$_POST;
$password=$_POST;
}
It checks wheather user clicked submit button,after we get form field values of email and password. $user=new user();
$user->login($email,$password);
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;
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;
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