Web Info and IT Lessons Blog...

Monday 23 February 2015

Simple Login System in PHP

The previous lesson of PHP Lessons Series was about Simple user registration panel in PHP and in this lesson we will learn to create a simple login system in PHP.

Simple Login System in PHP

In creating a PHP login system the following steps are necessary:

1. Create a login form using html form elements.
2. Send the login data filled in the html form to PHP via GET or Post Method.
3. Make database connection to MySql Database in PHP.
4. Check the user login information with the registered users information.
5. If the details are valid start a PHP Session for the user.

We will look into these steps one by one:

Creating a login form using html:

An html login form is very easy to implement. All we need is a text field for email, a password field and a submit button to submit the user data to PHP via GET and POST methods. A simple html login form is given below:


<form action="#" method="GET">
<table>
<tr>
<td>Email:</td><td><input type="text" name="email" /></td>
</tr>
<tr>
<td>Password:</td><td><input type="password" name="password" /></td>
</tr>
<tr>
<td><input type="submit" value="Submit" name="submit" /></td><td></td>
</tr>
</form>


Sending login form information to PHP:

Now the login data is sent to PHP once the submit button of the html form is clicked. So all we need to do is get the data filled in the login form through GET or POST array depending upon the form method being used. As we have are using GET method in the above form, the sample code to get the login details of user in php is given below:


if(isset($_GET['submit'])){
 $email = $_GET['email'];
 $password = $_GET['password'];
 
 echo 'Email: '.$email.' and Password: '.$password;
}

Making database connection to MySql Database in PHP:

This step is already covered in a great detail in a previous lesson about MySql Database Connection in PHP so i am going to skip this step. You can learn to connect to a mysql database in PHP in this lesson.

Check the user login information:

Once we have successfully connected to our database which holds the information of all registered users, we can check our form data with the valid users information in the database through this code given below:


$sql = "SELECT * FROM `user` WHERE `email` = '".$email."' 
        AND `password` = '".$password."'";
if($result = mysql_query($sql)){
 if(mysql_num_rows($result) > 0){
  echo 'Login data valid!';
 }else{
  echo 'Invalid Login Data!';
 }
}


MySqli implementation of the above code is given below:


$sql = "SELECT * FROM `user` WHERE `email` = '".$email."' 
        AND `password` = '".$password."'";
if($result = mysqli_query($connection, $sql)){
 if(mysqli_num_rows($result) > 0){
  echo 'Login data valid!';
 }else{
  echo 'Invalid Login Data!';
 }
}


PDO implementation of the above code is given below:


$sql = "SELECT * FROM `user` WHERE `email` = '".$email."' 
        AND `password` = '".$password."'";
$result = $connection->prepare($sql); 
$result->execute(); 
$number_of_rows = $result->fetchColumn();
if($number_of_rows > 0){
 echo 'Login data valid!';
}else{
 echo 'Invalid Login Data!';
}


Start a PHP Session of the user:

In this step we start a session for the logged in user to keep the user logged in until the user clicks the logout button. This piece of code is pretty simple:

$_SESSION['email'] = $email;

The final PHP code for a simple login system in PHP using mysql extension is given below:


session_start();
include 'dbConnection.php';

if(isset($_GET['submit'])){
 $email = $_GET['email'];
 $password = $_GET['password'];
 
 $sql = "SELECT * FROM `user` WHERE `email` = '".$email."' 
                AND `password` = '".$password."'";
 if($result = mysql_query($sql)){
  if(mysql_num_rows($result) > 0){
   echo 'Login data valid!';
   $_SESSION['email'] = $email;
  }else{
   echo 'Invalid Login Data!';
  }
 }
 
}


The final PHP code for a simple login system in PHP using mysqli extension is given below:


session_start();
include 'dbConnection.php';

if(isset($_GET['submit'])){
 $email = $_GET['email'];
 $password = $_GET['password'];
 
 $sql = "SELECT * FROM `user` WHERE `email` = '".$email."' 
                AND `password` = '".$password."'";
 if($result = mysqli_query($connection, $sql)){
  if(mysqli_num_rows($result) > 0){
   echo 'Login data valid!';
   $_SESSION['email'] = $email;
  }else{
   echo 'Invalid Login Data!';
  }
 }
 
}


The final PHP code for a simple login system in PHP using PDO extension is given below:


session_start();
include 'dbConnection.php';

if(isset($_GET['submit'])){
 $email = $_GET['email'];
 $password = $_GET['password'];
 
 $sql = "SELECT * FROM `user` WHERE `email` = '".$email."' 
                AND `password` = '".$password."'";
 $result = $connection->prepare($sql); 
 $result->execute(); 
 $number_of_rows = $result->fetchColumn();
 if($number_of_rows > 0){
  echo 'Login data valid!';
  $_SESSION['email'] = $email;
 }else{
  echo 'Invalid Login Data!';
 }
}


No comments:

Post a Comment