用PHP创建登录系统 [英] Creating a login system in PHP
问题描述
有人可以帮我吗?我的用户名是Blimeo,密码是"password",但是当我输入凭据时,就像我告诉我的那样,它显示"Access访问被拒绝".我100%确定我已正确配置mySQL数据库.
Can someone please help me? My username is Blimeo and my password is "password" but when I put my credentials in, it says "Access denied" like I told it to. I am 100% sure that I configured my mySQL database correctly.
<html>
<body>
<?php
echo sha1('Blimeo');
if (isset($_REQUEST['attempt'])) {
$link = mysql_connect('localhost', 'root', 'password') or die('Could not connect to database');
$user = mysql_real_escape_string($_POST['user']);
$password = sha1(mysql_real_escape_string($_POST['password']));
mysql_select_db('test_users');
$query = mysql_query(
"SELECT user
FROM users
WHERE user = '$user'
AND password = '$password'
") or die(mysql_error());
mysql_fetch_array($query);
$total = mysql_num_rows($query);
if ($total > 0) {
session_start();
$_SESSION['user'] = 'blah';
header('location: dashboard.php');
}
else {
echo '<br>Access denied!';
}
}
?>
<form method="post" action="login.php?attempt">
Enter your username:<input type="text" name="user"/><br/>
Enter your password:<input type="password" name="password"/><br/>
<input type="submit"/>
</form>
</body>
</html>
推荐答案
更新,2016
请仅使用现有的登录系统,几乎每个PHP框架都提供了现成的登录系统!绝对没有必要自己编写此代码,因为用户身份验证是一个大话题,编写认真,稳定和现代的登录解决方案将花费数月(数年).
Please only use existing login systems, which are provided out-of-the-box in nearly every PHP framework! There's absolutly no reason to write this by yourself, as user authentication is a big topic and it will take months (years) to write a serious, stable and modern login solution.
2012年以来的原始文本:
由于登录系统是一个安全问题,每个人都一遍又一遍地犯同样的错误,所以我可以明确地说:
As login systems are a security issue and EVERYBODY makes the same mistakes over and over again, i can clearly say:
采用专业脚本并遍历代码,以了解发生了什么,哈希和盐分是什么以及会话可能遇到的问题.
Take a professional script and work through the code to understand whats happening, what hashing and salting is and what problems session can have.
[已删除过时链接]
[removed outdating link]
以下是您可能需要的三个项目:
Here are three projects that might be what you need:
https://github.com/panique/php-login-one-file
https://github.com/panique/php-login-minimal
https://github.com/panique/huge
这篇关于用PHP创建登录系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!