I'm new to php for about a month and I decide to create my own website. Situation: My mysql server store user as md5 and password as md5. And a captcha on the login page
Let's have a look at my code
<?php
session_destroy();
$usermod=md5($_POST["user"]);
$passmod= md5($_POST["pass"]);
if(file_get_contents("http://www.opencaptcha.com/validate.php?ans=".$_POST['code']."&img=".$_POST['img'])=='pass')
{
$con=mysqli_connect("hidden","hidden","hidden","hidden");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result2 = mysqli_query($con,"SELECT * FROM Users
WHERE Username_login='$usermod'");
if($row2 = mysqli_fetch_array($result2))
{
$result = mysqli_query($con,"SELECT * FROM Users
WHERE Username_login='$usermod' AND Password='$passmod'");
if($row = mysqli_fetch_array($result))
{
echo "Thank you for logging in: ".$row['FirstName']." as ".$row['Username'];
session_start();
$_SESSION['user'] = $row['Username'];
$_SESSION['email'] = $row['Email_start']."@".$row['Email_domain'];
$_SESSION['name'] = $row['FirstName']." ".$row['LastName'];
header("Location: http://mspb.tk/login/welcome.php");
}
else
{
header("Location: http://mspb.tk/login/login.php?login=failed");
}}
else
{
header("Location: http://mspb.tk/login/login.php?username=failed");
}
}
else {
header("LOCATION:http://www.mspb.tk/login/login.php?opencaptcha=failed");
}
?>
That's it if anyone spot an sql injection and tell me I would be very happy :) Thank you very much Poom
There are a bunch of other security-related issues to note:
CRYPT_BLOWFISH
& generating random salts.password_hash
, this is done for you.No. Since you are md5'ing the only 2 _POST variables you're inserting into the query, you'll be fine. md5 always returns the hash as a 32-character hexadecimal number
no matter what the input.
Start by removing:
AND Password='$passmod'
I read that removing this makes it less vulnerable!
And Of course, md5() is supposed to NOT be used anymore these days, because they are too easy to bypass, with rainbow tables. Unfortunately I can't help you with the encryption process, I need some answers regarding these too(new as well :P)
So the best thing it to:
SELECT * from table where username=$username
After that you compare the $password the user entered with the password for that username/
It involves using mysqli_fetch_array($result, MYSQL_ASSOC)
, where $result
is a variable to your mysqli_query
.