在 PHP 中清理发往数据库的用户输入

发布于 2024-07-22 09:59:24 字数 1326 浏览 7 评论 0原文

我有这个代码:

$query = "select id from votes where username = '$user' and article_id  = $this->id";

我尝试使用这个代码来清理它:

$query = sprintf("select id from votes where username = '$user' and article_id = $this->id", 
    mysql_real_escape_string($user), 
    mysql_real_escape_string($password));

但是我在 mysql_real_escape 行中收到此错误:

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'mexautos'@'localhost' (using password: NO) in /home/mexautos/public_html/kiubbo/data/article.php on line 145 Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/mexautos/public_html/kiubbo/data/article.php on line 145 Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'mexautos'@'localhost' (using password: NO) in /home/mexautos/public_html/kiubbo/data/article.php on line 146 Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/mexautos/public_html/kiubbo/data/article.php on line 146

我在这里获取用户名,我不知道它是否足够安全:

function getUsername(){ return $this->username; }

Thx

I have this code:

$query = "select id from votes where username = '$user' and article_id  = $this->id";

I tried this code to sanitize it:

$query = sprintf("select id from votes where username = '$user' and article_id = $this->id", 
    mysql_real_escape_string($user), 
    mysql_real_escape_string($password));

but I get this error for the mysql_real_escape lines:

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'mexautos'@'localhost' (using password: NO) in /home/mexautos/public_html/kiubbo/data/article.php on line 145 Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/mexautos/public_html/kiubbo/data/article.php on line 145 Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'mexautos'@'localhost' (using password: NO) in /home/mexautos/public_html/kiubbo/data/article.php on line 146 Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/mexautos/public_html/kiubbo/data/article.php on line 146

I get the user name here, I dont know if its safe enough:

function getUsername(){ return $this->username; }

Thx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

空心↖ 2024-07-29 09:59:25

我建议使用成熟的数据库抽象层,例如 Zend_Db (有那里有很多)。 对于生产系统,我不建议实施您自己的自制解决方案。

I'd recommend using a mature DB abstraction layer like Zend_Db (there are tons of them out there). Implementing your own homebrew solution is not something I'd recommend for a production system.

美煞众生 2024-07-29 09:59:25

就像其他人所说,不是“$user”而是“%s”,并且您需要一个开放的连接。

@托马拉克
sprintf 更快 - 这就是使用它的原因 - 它是一个本机 C 函数。

Like the other said, not '$user' but '%s' and you need an open connection.

@Tomalak
sprintf is faster - that's the reason why to use it - it is a native C function.

寂寞笑我太脆弱 2024-07-29 09:59:24

您需要先建立 mysql 连接,然后才能使用 mysql_real_escape_string。

You need a mysql connection before you can use mysql_real_escape_string.

囍孤女 2024-07-29 09:59:24

我建议使用 准备好的语句 代替 sprintf

I would suggest using prepared statements for this instead of sprintf

余厌 2024-07-29 09:59:24

不确定这是否是导致您问题的原因,但我相信 sprintf 语句中的变量不应该是 '$user' 和 '$this->id',而应该是 '%s'

https://www.php.net/sprintf

Not sure if this is what's causing your problem, but I believe the variables in your sprintf statement shouldn't be '$user' and '$this->id', but they should be '%s'

https://www.php.net/sprintf

苏大泽ㄣ 2024-07-29 09:59:24

警告:mysql_real_escape_string()
[function.mysql-real-escape-string]:
用户访问被拒绝
'mexautos'@'localhost'(使用
密码:否)

警告:mysql_real_escape_string()
[function.mysql-real-escape-string]:A
无法链接到服务器
成立

您检查链接了吗? 是否活跃? 使用前需要先连接 mysql_real_escape_string()< /a>
你是不是忘记设置密码了?

尝试:(

mysql -u mexautos -p

如果没有密码,请输入 Enter)

另外,检查您的 sprintf() 函数,您需要使用 %s 绑定变量

$a = 'Foo';
$b = 'Bar';
$foo = sprintf('Foo Bar %s %s', $a, $b);

Warning: mysql_real_escape_string()
[function.mysql-real-escape-string]:
Access denied for user
'mexautos'@'localhost' (using
password: NO)

Warning: mysql_real_escape_string()
[function.mysql-real-escape-string]: A
link to the server could not be
established

Did you check the link ? Is it active ? You need to be connected before to use mysql_real_escape_string()
Don't you forget to set the password ?

Try:

mysql -u mexautos -p

(type Enter if no password)

Also, check out your sprintf() function, you need to use the %s to bind your variable

$a = 'Foo';
$b = 'Bar';
$foo = sprintf('Foo Bar %s %s', $a, $b);
梦里南柯 2024-07-29 09:59:24

您需要一个连接才能使用 mysql_real_escape_string(),因为它使用服务器的编码类型来帮助清理。

另外 sprintf() 应该看起来像这样

$query = sprintf("SELECT id FROM votes WHERE username = '%s' and article_id = %d", 
    mysql_real_escape_string($user), 
    mysql_real_escape_string($password));

You need a connection to use mysql_real_escape_string() because it uses the server's encoding type to help santitize.

Also the sprintf() should look something like this

$query = sprintf("SELECT id FROM votes WHERE username = '%s' and article_id = %d", 
    mysql_real_escape_string($user), 
    mysql_real_escape_string($password));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文