PHP SQL Login


 

PHP SQL Login

This Application illustrates how to create a login page where authorized user can login and further process.

This Application illustrates how to create a login page where authorized user can login and further process.

PHP SQL Login

 This Application illustrates how to create a login page where authorized user can login and further process.

In this application we create a userlogin.php page where we create a form with two input fields and one submit button, in this form we validate  whether both input field has to be filled or not, if both input fields has blank then a alert message will be display, but if user successfully login then loginaction.php page will be called where we check the both filed from database table whether user name and password is available in the database table or not. If user name and password have been validated then send on welcome.php page and when user not authorized then send on error.php page with error message and a link for come back on userlogin.php page.

 

Source Code of userlogin.php 

<html>
  <title>User Login Page</title>
  <head>
  <script language="JavaScript">
  function validateForm(theForm){
    if(theForm.userid.value ==""){
      alert("Enter the name");
      theForm.userid.focus();
      return false;
    }
    if(theForm.password.value==""){
      alert("enter the password");
      theForm.password.focus();
      return false;
    }
    return true;
  }
  </script>
  </head>

  <body>
    <table border="1" cellspacing="0" cellpadding="0">
    <h2>Login Form</h2>
    <form method="POST" 
action="loginaction.php" onsubmit="return validateForm(this);">
      <font size="2"
<strong> Enter username and Password: </strong></font>

      <tr>
        <td><b> UserName</b></td>
        <td><input type="text" size="20" name="userid"></td><br>
      </tr>
      <tr>
        <td><b> Password </b></td>
        <td><input type="password" size="20" name="password"></td>
      </tr><br>
      <tr>
        <td><input type="submit" name="submit" value="submit"></td>
      </tr>
    </form>
  </body>  
  </table>
</html>

 

Source Code of loginaction.php 

<?
  if!isset($_SESSION) ) { session_start()}
  $database_db="test";
  $user_db="root";
  $password_db="root";
  $host_db="localhost";

  $link=mysql_connect($host_db,$user_db,$password_dbor die ("couldnot connect: ".mysql_error());
  mysql_select_db($database_db, $link)  or exit('Error Selecting database: '.mysql_error()); ;
  
  $userid=$_POST["userid"];
  $password=$_POST["password"];

  $errormessage = "";

  $sql="SELECT * FROM users  where 
username='$userid' and password='$password'"
;
  $result = mysql_query($sql, $link)  or exit('$sql failed: '.mysql_error())
  $num_rows = mysql_num_rows($result);
  if($num_rows==0){
    header("Location: error.php");
  else {
    header("Location: welcome.php");
    exit;
  }
?>

 

welcome.php

<h2>Login Success...</h2>

error.php

<h2>Login Failed...</h2>
<a href="userlogin.php">login again</a>

Download Source Code

Output:

Ads