In today’s tutorial, we will see how to create Login page using PHP and MySQL. Generally the data are stored in the database, the way you enter it. But some information, you may want to hide like passwords. In this code we will encrypt the passwords, which keeps your sensitive information secure.

Example :

$password=”freefeast”;
$encrypted_password = md5(password);
echo $encrypted_password;

Here we will use md5() function to encrypt password in this login page. It will look like this when you store encrypted password.

Login Page in PHP

 

For this example, we will require to create emp_login table.

create table Emp_login (username varchar(30), password varchar(50) )

Example : HTML Code 

<html>
<center>
<head>  <h2>Login page </center></h2> </head>
<body>
<form name="f1" method="post" action="login.php">
  <table border="1" align="center">
 
 <tr align="center"> <th colspan="2">  Employee Managment System   </th></tr>
 <tr align="center"> <td>User Name : </td> 
                     <td><input type="text" name="txtuser" ></td>
  </tr>
 <tr align="center">  <td>Password : </td>
                      <td><input type="password" name="txtpwd"/></td>
  </tr>
  <tr align="center"><td colspan="2">
  <input type="submit" name="login" value="login"></td></tr>
  </table>
</form>
</body>
</center>
</html>

Output :

LOgin Page

 

PHP Code :

Here we have matched username and password with the database value and if we get exactly one record, it means successful / authenticated login.

<?php
if(isset ($_REQUEST["login"]))
{
    $tu=$_REQUEST["txtuser"];
    $tp=$_REQUEST["txtpwd"];

    $con=mysql_connect("localhost","root","");  // Database Connection
    if(!$con)
    {
              die("Connection Error");
    }
   else
   {
        $db=mysql_select_db("Employee",$con);  // Selecting Database
        if(!$db)
        {
            die("Database Not Found");
        }
        else
        {
               $qry="(select * from Emp_login where username='".$tu."' and 
                                                    password='".md5($tp)."')";
               $res=mysql_query($qry);
               $r=mysql_num_rows($res);
               if($r==1)
               {
                     header("location:temp.php");
               }
               else
               {
                      echo "Invalid......";
               }
         }
     }
 }
?>

In this code we have used :

  • md5() : md stands for Message-Digest, This function is used to encrypt & decrypt data. It actually calculates the hash of the string.
  • mysql_connect() : Used to open connection in MySQL and takes three parameter i.e. SERVER,USER_NAME and PASSWORD. we take default values for these parameters i.e. localhost, root and empty string respectively. This function returns a MySQL link identifier on successful connection and return false if fails to open connection.
  • mysql_error() : this returns the text of the error message  from previous MySQL operation and empty string if no error occurred.
  • mysql_select_db() : Sets the current active database on the server which is associated with specified link identifier returned by mysql_connect().
  • mysql_query() : This sends a query to current active database on the server. It takes SQL query as parameter.
  • mysql_fetch_row() : This function get a result row as an enumerated array. It returns numerical array corresponds to the fetched row and moves the internal data pointer ahead.   mysql_fetch_row() fetches one row of data from the result associated with the specified result identifier. The row is returned as an array and each result column is stored in array offset which starts at offset 0.
  • mysql_num_rows() : This function will returns the number of rows affected.
  • header() : With this  function you can redirect to the another page without any click or link.                                                                                Ex : Header(‘Location : https://www.freefeast.info’);             //But the header() function, you have to call before any of the actual output is sent may be HTML tags, blank lines.

Complete Code of Create Login Page using PHP and MySQL :

<html>
<center>
<head>  <h2>Login page </center></h2> </head>
<body>
<form name="f1" method="post" action="login.php">
  <table border="1" align="center">
 
 <tr align="center"> <th colspan="2">  Employee Managment System   </th></tr>
 <tr align="center"> <td>User Name : </td> 
                     <td><input type="text" name="txtuser" ></td>
  </tr>
 <tr align="center">  <td>Password : </td>
                      <td><input type="password" name="txtpwd"/></td>
  </tr>
  <tr align="center"><td colspan="2"><input type="submit" name="login" value="login"></td></tr>
  </table>
</form>
</body>
</center>
</html>
if(isset ($_REQUEST["login"]))
{
    $tu=$_REQUEST["txtuser"];
    $tp=$_REQUEST["txtpwd"];

    $con=mysql_connect("localhost","root","");  // Database Connection
    if(!$con)
    {
              die("Connection Error");
    }
   else
   {
        $db=mysql_select_db("Employee",$con);  // Selecting Table
        if(!$db)
        {
            die("Database Not Found");
        }
        else
        {
               $qry="(select * from Emp_login where username='".$tu."' and password='".md5($tp)."')";
               $res=mysql_query($qry);
               $r=mysql_num_rows($res);
               if($r==1)
               {
                     header("location:temp.php");
               }
               else
               {
                      echo "Invalid......";
               }
         }
     }
 }
?>