简体   繁体   English

PHP:mysql_connect不返回FALSE

[英]PHP: mysql_connect not returning FALSE

I have a form where the user enters their database information and can click a link that uses AJAX to send the credentials to this page. 我有一个表单,用户可以在其中输入他们的数据库信息,并且可以单击使用AJAX将凭据发送到此页面的链接。 The problem I have is that as long as they enter the correct host name the script returns TRUE . 我的问题是,只要他们输入正确的主机名,脚本就会返回TRUE

Is there another way to test this so that it will return FALSE if the username and password are not valid? 还有另一种测试方法,以便如果用户名和密码无效,它将返回FALSE

$h  =   urldecode($_GET['h']);
$u  =   urldecode($_GET['u']);
$p  =   urldecode($_GET['p']);

$con = mysql_connect($h, $u, $p);

if(!$con){
    echo 'Could not connect';
}

else{
    echo 'Connected';
}

Solved! 解决了!

For future reference, the issue was that there where entries in the mysql user table for user = "Any". 供将来参考,问题是在mysql用户表中的user =“ Any”项存在。 I removed those users and the script worked as expected. 我删除了这些用户,脚本按预期工作。 I updated this post to include a screen shot for anyone having similar problems. 我更新了这篇文章,为所有遇到类似问题的人提供了屏幕截图。 Thanks to Fabio below for the suggestion! 感谢下面的法比奥的建议!

MYSQL中的用户表包括“任何”用户的条目。

That's because mysql_connect uses some defaults when connecting which should be root for the username and the blank string for the password if I correctly remember it. 这是因为mysql_connect在连接时使用一些默认值,如果我没有记错的话,它应该是用户名的root和密码的空字符串。 Alternatively could be the username under which the webserver runs. 另外,也可以是网络服务器运行时使用的用户名。

This could means that your db server accepts passwordless root connections (from the webserver machine), which is pretty dangerous. 这可能意味着您的数据库服务器接受(来自Web服务器计算机的)无密码的根连接,这非常危险。 You should review your database configuration and user list. 您应该查看数据库配置和用户列表。

From a security point of view your code is not very safe, db credentials are transmitted in cleartext, and as a rule of thumb db credentials should not be entered by end users (unless you're writing a PhpMyAdmin like tool). 从安全角度来看,您的代码不是很安全,db凭据以明文形式传输,并且作为经验法则,最终用户不应输入db凭据(除非您正在编写类似PhpMyAdmin的工具)。

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

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