Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Sql Mysql-example PHP SQL Login

Related Tutorials


 
 

Share on Google+Share on Google+

PHP SQL Login

Advertisement
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:

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 17, 2009

Related Tutorials

Discuss: PHP SQL Login   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:5
B1uB3rry
August 15, 2011
SQL Injection

hahaha. Your script is vulnerable to SQL injection... I hope you don't use this script on your site.
Yanga
February 17, 2012
SQL

please i will need a process of setting up an sql database please make it as simple as possible even if i can get a copy and paste version :) Thanks
subash
September 7, 2012
Doesnt work

Its not working for me. Some how when i open the php file using firefox it tends to download the file. Please help.
jwelth
September 26, 2012
conectivity in sql server

i want 2 create admin page in php and connectivity in sql server so plz guide me.....
md.ripon
September 22, 2013
html php sql oracol

html
DMCA.com