Jump to content

php login username and password verification.


darkknightgaury

Recommended Posts

<?php

$username='jose';

$password='javier';

 

$con = mysql_connect("localhost", "admin","");

 

mysql_select_db("usernames",$con);

 

if (!$con) {

  die('Not connected : ' . mysql_error());

             }

 

$report = mysql_query("SELECT * FROM usernames where username like '$username%' and password like '$password%'");// i been debating whether i should include %

 

while ($row = mysql_fetch_array($report))

{

       $correctusername = $row["username"];/* here since i ran the query in report it seems like i am being redundant bit but it make sense to clear the results with the report query before i read it, I dont know is my first username password code. */

       $correctpassword= $row ["password"];

       

        if($correctpassword==$password & $correctusername==$username)

       

        echo "The username and password has been verified.";

       

      }

/*Yes. i realized this code is a begginers level dont bash me or talk down to me. */

 

?>

I would do it something like this

 

$sql = "SELECT * FROM usernames WHERE username='".$username."' AND password='".$password."' LIMIT 1"; // Since only 1 user should be able to have that combination
$result = mysql_query($sql);

if ($result)
    {
        $row = mysql_fetch_array($result);
        $correct_user = $row['username'];
        $correct_pass = $row['password'];
        
           if($correct_pass == $password && $correct_user == $username)
               { 
                     echo "The username and password has been verified.";
               }
     }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.