Log.php 返回“无效用户”来自 MySQL 数据库
我正在本地计算机上托管一个网站(在 Mac 上使用 MAMP Pro),并且需要将托管切换到另一台本地 Mac。我已经复制了网站的所有文件和 MySQL 表,并检查服务器和 MySQL 是否运行正常。一切似乎都很好,除了当我尝试登录时登录系统返回“无效用户”,即使我输入了正确的用户信息(我已经尝试了一些用户只是为了确定)。
处理登录的 log.php 看起来像这样:
<?
session_name("MyLogin");
session_start();
if($_GET['action'] == "login") {
$conn = mysql_connect("localhost","root","password"); // your MySQL connection data
$db = mysql_select_db("nick"); //put your database name in here
$name = $_POST['user'];
$q_user = mysql_query("SELECT * FROM USERS WHERE login='$name'");
if(mysql_num_rows($q_user) == 1) {
$query = mysql_query("SELECT * FROM USERS WHERE login='$name'");
$data = mysql_fetch_array($query);
if($_POST['pwd'] == $data['password']) {
$_SESSION["name"] = $name;
header("Location: http://monthlymixup.com/may.php"); // success page. put the URL you want
exit;
} else {
header("Location: login.php?login=failed&cause=".urlencode('Wrong Password'));
exit;
}
} else {
header("Location: login.php?login=failed&cause=".urlencode('Invalid User'));
exit;
}
}
// if the session is not registered
if(session_is_registered("name") == false) {
header("Location: login.php");
}
?>
我已经暂时删除了上面代码中的密码。
我想知道我可以采取什么步骤来解决这个问题,如果有任何帮助,我将不胜感激。
谢谢,
尼克
I am hosting a website from a local computer (using MAMP Pro on a Mac), and need to switch the hosting to another local Mac. I have copied across all of the files for my website, and the MySQL tables, and checked that the server and MySQL are running OK. Everything seems to be fine, except that the login system is returning "Invalid User" when I try to log in, even though I am entering the correct user info (I have tried a few users just to be sure).
The log.php that handles the login looks like this:
<?
session_name("MyLogin");
session_start();
if($_GET['action'] == "login") {
$conn = mysql_connect("localhost","root","password"); // your MySQL connection data
$db = mysql_select_db("nick"); //put your database name in here
$name = $_POST['user'];
$q_user = mysql_query("SELECT * FROM USERS WHERE login='$name'");
if(mysql_num_rows($q_user) == 1) {
$query = mysql_query("SELECT * FROM USERS WHERE login='$name'");
$data = mysql_fetch_array($query);
if($_POST['pwd'] == $data['password']) {
$_SESSION["name"] = $name;
header("Location: http://monthlymixup.com/may.php"); // success page. put the URL you want
exit;
} else {
header("Location: login.php?login=failed&cause=".urlencode('Wrong Password'));
exit;
}
} else {
header("Location: login.php?login=failed&cause=".urlencode('Invalid User'));
exit;
}
}
// if the session is not registered
if(session_is_registered("name") == false) {
header("Location: login.php");
}
?>
I have temporarily removed the password in the above code.
I wonder what steps I can take to troubleshoot this issue, and would be grateful for any help.
Thanks,
Nick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
遇到这个问题时的一些常用技巧。
echo $query
;mysql_error()
是否输出任何内容。var_dump()
和print_r()
以确保它们符合预期。exit()
行,以便您可以确定脚本的中断位置。使用上述确定的任何内容进行修复或评论。
A few common techniques when I encounter this issue.
echo $query
;mysql_error()
outputs anything after you run your queries.var_dump()
andprint_r()
on your data objects to ensure they are as expected.exit()
lines so you can determine where the script is breaking.Fix or comment back with anything determined by the above.
您的代码执行查询来查找具有给定用户名的用户,然后检查具有该用户名的行数是否正好为 1。
您看到“无效用户”错误的唯一方法是如果有 0 个具有该用户名的用户或超过 1 个用户使用该用户名。
查看表的内容并检查属于哪一种情况(我推荐 http://sequelpro.com用于在 Mac 上查看数据库内容)。您还可以使用sequel pro 来测试您的查询。
Your code does a query to find a user with the given username, and then checks if the number of rows with that username is exactly 1.
The only way you could see the 'Invalid User' error is if there are 0 users with that username or more than 1 user with that username.
Have a look at the contents of the table and check which of these is the case (I recommend http://sequelpro.com for viewing database contents on a Mac). You can also use sequel pro to test your queries.