是否建议在 php 中使用一个结合两个或多个内置清理函数的清理函数?

发布于 2024-12-25 15:30:20 字数 452 浏览 3 评论 0原文

是否可以使用一个函数来清理由于表单提交或任何其他请求而输入的输入。这节省了时间,但有效性和效率的问题仍然困扰着我。例如,

   function clearSpecialChars($str)
   {
     $str=htmlentities($str);
     $str=strip_tags($str);
     $str=mysql_real_escape_string($str);

     return $str;
   } 

当我收到表单提交时,我会这样做:

    $username=clearSpecialChars($_REQUEST['username']);

    $email=clearSpecialChars($_REQUEST['email']);

从根本上来说,我不希望用户输入任何 html 输入。

Is it okay to employ a function that sanitizes the incoming inputs due to a form submission or any other request. It is time saving but the question of effectivenss and efficiency still haunts me. For instance,

   function clearSpecialChars($str)
   {
     $str=htmlentities($str);
     $str=strip_tags($str);
     $str=mysql_real_escape_string($str);

     return $str;
   } 

so that when I get a form submission I do:

    $username=clearSpecialChars($_REQUEST['username']);

    $email=clearSpecialChars($_REQUEST['email']);

Fundamentally, I am not desiring any html inputs from the user.

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

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

发布评论

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

评论(2

迷途知返 2025-01-01 15:30:20

每个函数都有其自己的用途,您不应使用任何不符合其预期用途的函数。

  1. 在mysql查询中使用该参数之前,您应该使用mysql_real_escape_string。
  2. 您应该在输出到页面之前使用 htmlspecialchars。

就是这样。

each function serves its own purpose, you shouldn't use any function not for their intended use.

  1. you should use mysql_real_escape_string before using the parameter in mysql query.
  2. you should use htmlspecialchars before outputting to page.

that's about it.

殤城〤 2025-01-01 15:30:20

是的,您可以创建一个简单的函数来在使用值之前对其进行清理。我使用这样的函数:

function sanitize($value)
{
    return htmlentities(addslashes($value));
}

Which escape ' and " 并转换 html 实体中的所有适用字符。我的其他选项更复杂,但您可以从它开始。

Yes, you can create a simple function to sanitize a value before use it. I use a function like that:

function sanitize($value)
{
    return htmlentities(addslashes($value));
}

Which escape ' and " and convert all applicable character in html entities. Mine is more complicated with other option, but you can begin from it.

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