Login Form and Authentication in PHP


This post is about the Simple login form and Authentication in PHP.This Authentication is very basically used for an little application.More about Secure and Advanced Authentication will be discussed in later posts.


This is Simple login form you can used in any application . This consists of user name and password field and submit button.

Below is html code of Login Form




index.php
------------

<html>
  <head>
       <title>Login</title>
   </head>
   <body>
   <fieldset style="margin-right:600px;">
   <legend>Login</legend>
   <form action="auth.php" name="login" method="post">
       <table>
        <?php
            if(isset($_GET['msg'])){
        ?>
        <tr>
            <td><?php echo $_GET['msg'];?>
            </td>
        </tr>
           
        <?php       
           
            }
        ?>
        <tr>
            <td>User Name</td>
            <td><input type="text" name="user_name"/></td>
        </tr>
        <tr>
            <td>Password</td>
            <td><input type="password" name="password"/></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input type="submit" name="submit" value="submit"/></td>
        </tr>
   </form>
   </fieldset>
  </body>
  </html>

After putting login details this form will submit for authentication.Following is Authentication Code.

auth.php
-----------
 
<?php
 $username = $_POST['user_name'];
 $pass = $_POST['password'];
 if($username == "john" AND $pass == "abc"){
     header("location:welcome.php");

 }
 else{
     header("location:index.php?msg=Invalid user name or password");
 }
?> 


If user is authenticated successfully then it will direct to Welcome page otherwise it will redirect to index page with error message.

Welcome.php
--------------

<html>
  <head>
       <title>Login</title>
   </head>
   <body>
      <h1>Welcome !</h1>
  </body>
  </html>



 Thanks For Reading..........
 

 

No comments:

Post a Comment