在 PHP 版本 5.2.14 或 PHP 版本 6 的同等版本上使用 get_magic_quotes_gpc

发布于 2024-09-29 22:16:02 字数 429 浏览 1 评论 0原文

我们的网站使用的是 PHP 版本 5.2.14

最近我们的主机可能更改了 magic-quote 定义,我提出了建议的解决方案 [代码如下]

  1. 这个解决方案适用于 PHP 版本 5.2.14 吗?
  2. 当我们升级到 PHP 版本 6 时,我应该更改哪些内容?
// 代码:

函数 fHandleQuotes($s) {
  如果(get_magic_quotes_gpc())
    返回($s);
  返回(添加斜杠($s));
}

。 。 。
// 用法:

。 。 。 
$query = "UPDATE myTable SET myField = '" . fHandleQuotes($_POST['fieldName']) 。 “'”;
。 。 。 

Our site is using PHP Version 5.2.14

Lately our hoster probably changed magic-quote defenition, and I came up with the suggested solution [code bellow]

  1. Is this solution OK for PHP Version 5.2.14 ?
  2. What should I change when we upgrade to PHP version 6 ?
// Code:

function fHandleQuotes($s) {
  if (get_magic_quotes_gpc())
    return ($s);
  return (addslashes($s));
}

. . .
// Usage:

. . . 
$query = "UPDATE myTable SET myField = '" . fHandleQuotes($_POST['fieldName']) . "'";
. . . 

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

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

发布评论

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

评论(2

允世 2024-10-06 22:16:02

在 PHP 6 中 magic_quotes 将被删除!
现在您可以使用这个功能了。

if(  ( function_exists("get_magic_quotes_gpc") && get_magic_quotes_gpc() ) || ini_get('magic_quotes_sybase')  ){
    foreach($_GET as $k => $v) $_GET[$k] = stripslashes($v);
    foreach($_POST as $k => $v) $_POST[$k] = stripslashes($v);
    foreach($_COOKIE as $k => $v) $_COOKIE[$k] = stripslashes($v);
}

In PHP 6 magic_quotes will be removed!
Now you can use this function.

if(  ( function_exists("get_magic_quotes_gpc") && get_magic_quotes_gpc() ) || ini_get('magic_quotes_sybase')  ){
    foreach($_GET as $k => $v) $_GET[$k] = stripslashes($v);
    foreach($_POST as $k => $v) $_POST[$k] = stripslashes($v);
    foreach($_COOKIE as $k => $v) $_COOKIE[$k] = stripslashes($v);
}
梦在深巷 2024-10-06 22:16:02

阅读本文并了解为什么不应使用魔术引号:
http://php.net/manual/en/security.magicquotes.disabling.php

使用该页面上的示例之一,并将 stripslashes 替换为 addslashes。但是,是的,您的解决方案可能有效。不过,在启动时仅使用 $_GET = array_map("addslashes", $_GET); 会更快且更少干扰。更好的是使用 mysql_real_escape_string 而不是 addslashes 。 (但是你的数据库连接必须已经建立才能工作。)

另外我想向你推荐这个:http://sourceforge.net/p/php7framework/wiki/input/ - 因为它允许您逐步重写应用程序以使用 $_GET->q["fieldName"]< /code> 用于(不太安全)魔术引用字段,或简单地 $_POST->sql["fieldName"] 用于(更安全)编码字段。
您甚至可以使用 $_REQUEST->sql->always() 为所有正常 $_REQUEST["fieldName"] 访问默认启用过滤器。尽管这对于某些应用程序来说可能有点过大了。

Read this and why you shouldn't use magic quotes:
http://php.net/manual/en/security.magicquotes.disabling.php

Use one of the examples on that page and replace stripslashes with addslashes. But yes, your solution probably works. Though it would be faster and less intrusive to just use $_GET = array_map("addslashes", $_GET); once at startup. Even better would be to use mysql_real_escape_string instead of addslashes thereon. (But your database connection must already be established for this to work.)

Also I'd like to spamrecommend you this: http://sourceforge.net/p/php7framework/wiki/input/ - because it allows you to progressively rewrite your application to use $_GET->q["fieldName"] for (not so secure) magic quoted fields, or simply $_POST->sql["fieldName"] for (more secure) encoded fields.
You can even use $_REQUEST->sql->always() to enable the filter per default for all normal $_REQUEST["fieldName"] accesses. Though that might be overkill for some applications.

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