简体   繁体   English

PHP mysql中的用户名和密码验证

[英]username and password validation in php mysql

What I want to be able to do is: When a user enters their username and password in the form on the index.html page, if they match what is in the DB, then they get sent to the next page; 我想要做的是:当用户在index.html页面上的表单中输入用户名和密码时,如果他们与数据库中的内容匹配,则将他们发送到下一页; userlogin.php. userlogin.php。 If their username or password is incorrect then they are asked to re-enter their details on the index.html page, and displaying an error like, "Your username is Incorrect" or "Your password is Incorrect" above the form text box. 如果他们的用户名或密码不正确,则要求他们在index.html页面上重新输入其详细信息,并在表单文本框上方显示错误,例如“您的用户名不正确”或“您的密码不正确”。 I can paste this code if required. 如果需要,我可以粘贴此代码。

Can I change this text font color as well, to red for example? 我是否也可以将此文本字体颜色更改为红色?

This is the code I currently have for the userlogin.php page 这是我目前在userlogin.php页面上使用的代码

<?php
mysql_connect("Server", "root", "Gen") or die("Couldn't select database.");
mysql_select_db("generator") or die("Couldn't select database.");

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

$sql = "SELECT * FROM users WHERE Username = '$username' AND Password = '$password' ";
$result = mysql_query($sql) or die(mysql_error());
$numrows = mysql_num_rows($result);
if($numrows > 0)
   {
    echo 'Your in';
   }
else
   {
    echo 'Your not in';
   }
   ?>

There as sooo many things wrong with this code: 这段代码有很多错误之处:

1- you have an SQL injection hole. 1-您有一个SQL注入孔。

If I enter ' or 1=1 LIMIT 1 -- as a username, I will always get access, no matter what. 如果我输入' or 1=1 LIMIT 1 --作为用户名,无论如何我都会获得访问权限。
Change your code into. 将您的代码更改为。

$username = mysql_real_escape_string($_POST['username']); 
$password = mysql_real_escape_string($_POST['password']); 

See: How does the SQL injection from the "Bobby Tables" XKCD comic work? 请参阅: 来自“ Bobby Tables” XKCD漫画的SQL注入如何工作?

2- you are storing the password in the clear 2-您将密码明码存储
This is a huge no no. 这是一个很大的不。 Combined with the SQL-injection hole, it will take a hacker 5 minutes to get a list of all usernames and passwords on your site. 结合SQL注入漏洞,黑客需要5分钟才能获得您网站上所有用户名和密码的列表。

Store the password as a salted hash. 将密码存储为盐值哈希。

I like to use the username as the salt. 我喜欢使用用户名作为盐。

You store the password hash using: 您可以使用以下方式存储密码哈希:

INSERT INTO users (username, passhash) 
VALUES ('$username', SHA2(CONCAT('$password','$username'),512))

And you test the user credentials using: 然后您使用以下方法测试用户凭据:

SELECT * FROM users 
WHERE username = '$username' AND 
passhash = SHA2(CONCAT('$password','$username'),512)

See: Secure hash and salt for PHP passwords 请参阅: PHP密码的安全哈希和盐
And: What is "salt" when relating to MYSQL sha1? 并且: 与MYSQL sha1相关的“盐”是什么?

BTW, use SHA2 with a 512 keylength, SHA1 is no longer secure, and MD5 is even more broken. 顺便说一句,使用密钥长度为512的SHA2,SHA1不再安全,并且MD5更加损坏。

3- A login can only ever match against 1 user 3-登录只能与1个用户匹配
This code: 这段代码:

if($numrows > 0) 

Makes no sense, if you get 2 rows out of the database, that's a clear sign someone has hacked your system. 毫无意义,如果您从数据库中取出两行,则表明有人入侵了您的系统。 The test should be: 测试应为:

if($numrows > 1) { //send email to sysadmin that my site has been hacked }
else if ($numrows = 0) { echo "wrong username or password" }
else { echo "welcome dr. Falken" }

4- Don't die if there's an error, call a routine to restart the connection or something 4-如果有错误不要死,调用例程以重新启动连接或其他操作

This code: 这段代码:

$result = mysql_query($sql) or die(mysql_error());    

Is fine in testing, but in production you should do something like 在测试中很好,但是在生产中您应该做类似的事情

$result = mysql_query($sql);
if ($result) {
  //do the deed
} else {
  //call error recovery routine
}

The error recovery routine should reconnect to the server, log a error in the logbook. 错误恢复例程应重新连接到服务器,在日志中记录错误。 Is the error cannot be fixed, it should send an email to the sysadmin and only then die the server. 如果错误无法解决,则应向sysadmin发送电子邮件,然后终止服务器。

First of all, your code is vulnerable to SQL injection . 首先, 您的代码容易受到SQL注入的攻击 Use PDO and prepared statements to fix this. 使用PDO和准备好的语句来解决此问题。 Second of all, you're appearantly storing usernames unencrypted. 其次,您似乎正在存储未加密的用户名。 This is very unsafe . 这是非常不安全的 Use a hashing function to encrypt the passwords, and encrypt the submitted password before running the query to get a match. 使用哈希函数对密码进行加密,并在运行查询获得匹配之前对提交的密码进行加密。 Coloring the output is simple: 为输出着色很简单:

echo '<span style="color:red">Your not in</span>';

And use sessions to actually log the user in. After successfully querying the user table for the username/password combination, store the returned user_id in the $_SESSION variable. 并使用会话实际登录用户。成功查询用户表中的用户名/密码组合后,将返回的user_id存储在$_SESSION变量中。 On each page that needs to be secured, just check for the existence of $_SESSION['user_id'] ; 在需要保护的每个页面上,只需检查$_SESSION['user_id'] if it isn't there, your user needs to login so redirect him to the login form. 如果不存在,则您的用户需要登录,因此将其重定向到登录表单。

That should about do the trick for ya ;) 那应该为你做把戏;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM