Home Sql Mysql-example PHP SQL Login



PHP SQL Login
Posted on: January 17, 2009 at 12:00 AM
This Application illustrates how to create a login page where only authorized user can login and process further.

PHP SQL Login

     

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

In this application, we have created a userlogin.php page which has a form with two input fields and one submit button. In this form, we validate  whether both input field are filled or not. If the input field is blank then an alert message will be displayed. If user fills the correct credentials then loginaction.php page will is called which works as the action page where we check both field 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 user is sent to the welcome.php page and if the user is not authorized then to the 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_db) or 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:

Related Tags for PHP SQL Login:
cprocessapplicationloginiouserpagethislogappcreatewheretoproceilauthcanliuseauthorizeceintrcaesagehowpropprateratescatsginessatauthorislleaandstrrtzssrocrithstatiapicaicaplprocesprndonlyononl


More Tutorials from this section

Ask Questions?    Discuss: PHP SQL Login   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.