Databases| SQL| MySQL| Questions? | Software Development
 

PHP SQL Login Script

In this example, we have created a login application where we will use php script for validation.

PHP SQL Login Script

                         

This Application illustrates how to create a php validation script for login application.

In this example, we have created a login application where we will use php script for validation. If the user does not fill the user name or password then the error message is displayed in the next page. If user passes unauthentic credentials then error message  "Incorrect User Name OR Password Found" is displayed to the user. Success message is displayed if user fills correct information. All script is in the checklogin.php where we create a connection between database table and application to check whether user is valid or not.

 

 

 

 

Source Code of login.php

<html>
<head><title>Login Form</title></head>
<body>
  <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
  <tr>
    <form name="form1" method="post" action="checklogin.php">
    <td>
      <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
        <tr>
          <td colspan="3"><strong>Please Login Here</strong></td>
        </tr>
        <tr>
          <td width="78">Username</td>
          <td width="6">:</td>
          <td width="294"><input name="username" type="text" id="username"></td>
        </tr>
        <tr>
          <td>Password</td>
          <td>:</td>
          <td><input name="password" type="password" id="password"></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td><input type="submit" name="Submit" value="Login"></td>
        </tr>
      </table>
    </td>
    </form>
  </tr>
  </table>
</body>
</html>

 

Source Code of checklogin.php 

<?php
  $host="localhost";
  $username="root";
  $password="root";
  $db_name="test";
  
  mysql_connect("$host""$username""$password")or die("cannot connect");
  mysql_select_db("$db_name")or die("cannot select DB");

  $username=$_POST['username'];
  $password=$_POST['password'];

  $username = stripslashes($username);
  $password = stripslashes($password);
  $username = mysql_real_escape_string($username);
  $password = mysql_real_escape_string($password);

  $flag="OK";  
  $msg="";  

  if(strlen($username1){
    $msg=$msg."Please enter the user name<br>";
    $flag="NOTOK"
  }

  if(strlen($password){
    $msg=$msg."Please enter the password<br>";
    $flag="NOTOK";  
  }

  if($flag <>"OK"){
    echo "<left>$msg <br> <input type='button' value='Retry' onClick='history.go(-1)'></left>";
  }else{
    $sql="SELECT * FROM users WHERE username='$username' and password='$password'";
    $result=mysql_query($sql);

    $count=mysql_num_rows($result);
    
    if($count==1){
      session_register("username");
      session_register("password");
      header("location:loginsuccess.php");
    else {
      echo "Incorrect User Name OR Password Found";
      echo "<left><br><input type='button' value='Retry' onClick='history.go(-1)'></left>";
    }
  }
?>

 

Source Code of loginsuccess.php 

<?
  session_start();
  if(!session_is_registered(username)){
    header("location:login.php");
  }
?>

<html>
<body>
Login Successful
</body>
</html>

 

Download Source Code

 

Output:

 

                         

» View all related tutorials
Related Tags: sql mysql c query rest table select io return tab show if condition ie record where records ci ws sh

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
 
Tell A Friend
Your Friend Name

 

 
Recently Viewed
Software Solutions
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.