如何在 php 中清理从 url 到 mysql 的简单 _GET 输入

发布于 2024-11-09 17:40:26 字数 609 浏览 9 评论 0原文

我有一个简单的 php 脚本,希望人们可以简单地访问 http://mydomain.com/get.php?id=TEXTMASH-1FD0066D-F75A-5D0C-6784-2FA0D055B0D7 之类的内容并启动这个文本块来自我的 mysql 数据库。我对 php 很陌生。知道所有预期的 ID 都应采用 TEXTMASH-1FD0066D-F75A-5D0C-6784-2FA0D055B0D7 格式,如何“清理” _GET 以使其不易受到任何攻击?我看过一些指南,但我只是不明白。有人介意给我看看吗?我真的很感激。

<?php 

include_once("../../extern/dbinclude.php");

$id= $_GET['id'];

$result = mysql_query("SELECT text FROM mytable WHERE id='$id'")
or die(mysql_error());  

$row = mysql_fetch_array($result);

if($row)
{
echo nl2br($row['text']);
} else {
?>
<h2>Invalid URL</h2>
<?php
}

I have a simple php script where hopefully people can simply go to something like http://mydomain.com/get.php?id=TEXTMASH-1FD0066D-F75A-5D0C-6784-2FA0D055B0D7 and pull up this text block from my mysql db. I'm very new to php. Knowing that all of expected IDs should be something in the format of TEXTMASH-1FD0066D-F75A-5D0C-6784-2FA0D055B0D7 how can I "sanitize" the _GET so it's not vulnerable to any attacks? I've looked at some guides but, i'm just not getting it. Anyone mind showing me? I'd really appreciate it.

<?php 

include_once("../../extern/dbinclude.php");

$id= $_GET['id'];

$result = mysql_query("SELECT text FROM mytable WHERE id='$id'")
or die(mysql_error());  

$row = mysql_fetch_array($result);

if($row)
{
echo nl2br($row['text']);
} else {
?>
<h2>Invalid URL</h2>
<?php
}

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

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

发布评论

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

评论(3

月下伊人醉 2024-11-16 17:40:26

您可以使用 mysql_real_escape_string 或可能的正则表达式来检查输入。适合您情况的一种可能的正则表达式:

$pattern = '/^TEXTMASH-[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/';
if(!preg_match($pattern, $id))
{
    echo "No dice >:(";
    exit;
}

You could either use mysql_real_escape_string or possibly a regex to check the input. One possible regex for your case:

$pattern = '/^TEXTMASH-[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/';
if(!preg_match($pattern, $id))
{
    echo "No dice >:(";
    exit;
}
猫卆 2024-11-16 17:40:26

到目前为止,最好的方法是通过准备好的声明。到目前为止,其他答案建议使用 mysql_real_escape_string ,它可以工作,但很容易被开发人员和 PITA 错过,不得不一直使用。由于很容易错过并且很难找到这些错误,因此它不是一个可靠或可维护的解决方案。

准备好的语句可以消除过程中的这种痛苦,并自动适当地转义事情。

您可以在SO其他地方找到我们的更多信息。

By far the best way to do this is through prepared statements. The other answers so far have suggested using mysql_real_escape_string, which works, but is easily missed by a developer and a PITA to have to use all the time. Because it's so easy to miss and so hard to locate those errors, it's not a reliable or maintainable solution.

Prepared statements take this pain out of the process and automatically escape things appropriately.

You can find our more info elsewhere on SO.

一抹苦笑 2024-11-16 17:40:26

清理输入的两个主要方面:

  1. 获取输入时进行验证。查看用于进行验证的 PHP 过滤器函数。就您而言,ID 相当复杂,您可能需要一个自定义验证函数(可能使用正则表达式)。但是,我不明白为什么在可以使用 INTeger ID 的情况下还要使用如此复杂的 ID(这是典型的情况)。
  2. 使用 mysql_real_escape_string()在将用户变量插入数据库之前准备好的语句

每当您显示字符串变量时,请确保转义任何 HTML 字符。

Two main aspects to sanitizing the input:

  1. Validate when you get it. Check out the PHP filter functions for doing validation. In your case, the ID is rather complex, and you might need a custom validation function (using regex, possibly) for this. However, I don't see why you're even using such a complex ID when you could use an INTeger ID, as is typical.
  2. Use mysql_real_escape_string() or prepared statements before inserting a user variable into the database.

And whenever you display a string variable, make sure to escape any HTML characters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文