Results 1 to 2 of 2

Thread: username and password verification

  1. #1
    Join Date
    Jul 2011
    Location
    hyderabad,India
    Posts
    58
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default username and password verification

    hi all,
    my database name is "test".in the "test" database
    i have created a table name called "log" which has two fields
    namely username and password.
    i have given username as "admin" and password as "admin123".
    now i have written one code using php so that when i click the submit button both the username as "admin" and password as "admin123" gets matched then it should direct to the next page...
    tell me how to check whether username and passwords are matching and if it matches it must point to the action part what we give in <form method="POST" action="www.php">
    kindly tell me what i must add to the below program.....
    below is the code in php......
    Code:
    <?php
    $host="localhost";  
    $username="root";  
    $password="";  
    $db_name="test"; 
    $tbl_name="log"; 
    
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
     
    $myusername=$_POST['myusername']; 
    $mypassword=$_POST['mypassword'];
    
    $myusername = stripslashes($myusername);
    $mypassword = stripslashes($mypassword);
    $myusername = mysql_real_escape_string($myusername);
    $mypassword = mysql_real_escape_string($mypassword);
    
    $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
    $result=mysql_query($sql);
    
    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);
    // If result matched $myusername and $mypassword, table row must be 1 row
    if($count==1)
    {
    // Register $myusername, $mypassword and redirect to file "login_success.php"
    session_register("myusername");
    session_register("mypassword"); 
    header("location:www.php");
    }
    else 
    {
    echo "Wrong Username or Password";
    }
    ?>

  2. #2
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Name your submit button something, and check if the submit button (which is technically an input) has been POSTed.


    PHP Code:
    <?php
    $host
    ="localhost";  
    $username="root";  
    $password="";  
    $db_name="test"
    $tbl_name="log"

    mysql_connect("$host""$username""$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
     
    $myusername=$_POST['myusername']; 
    $mypassword=$_POST['mypassword'];

    $myusername stripslashes($myusername);
    $mypassword stripslashes($mypassword);
    $myusername mysql_real_escape_string($myusername);
    $mypassword mysql_real_escape_string($mypassword);

    if(isset(
    $_POST['submit'])) {

    $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
    $result=mysql_query($sql);

    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);
    // If result matched $myusername and $mypassword, table row must be 1 row
    if($count==1)
    {
    // Register $myusername, $mypassword and redirect to file "login_success.php"
    session_register("myusername");
    session_register("mypassword"); 
    header("location:www.php");
    }
    else 
    {
    echo 
    "Wrong Username or Password";
    }

    }
    ?>
    Then just set your submit button with the name attribute set to submit.
    Code:
    <input type="submit" name="submit" value=" Log in " />
    - Josh

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •