简体   繁体   English

PHP 5.4 magic_quotes_gpc替代?

[英]php 5.4 magic_quotes_gpc alternative?

I was using php 5.2 earlier. 我之前使用的是php 5.2 Now I want to upgrade php 5.4. 现在我想升级php 5.4. Magic quotes are removed now. 魔术引号现在已删除。 I want to make my application work properly. 我想让我的应用程序正常工作。 Which function I should use for escaping data mysql_real_escape_string() or addslashes() ? 我应该使用哪个函数来转义数据mysql_real_escape_string()addslashes()

Which function from the above will give the same results as of magic_quotes_gpc setting?? 上面的哪个函数将提供与magic_quotes_gpc设置相同的结果?

It's always best to migrate to PDO and prepared statements as outlined by @alex above. 最好总是迁移到PDO和上面@alex概述的准备好的语句。

If that isn't feasible, absolutely escape incoming string data with mysql_real_escape_string() , and validate integer data, eg using filter_input() as shown in this answer. 如果那不可行,请使用mysql_real_escape_string()绝对转义传入的字符串数据,并验证整数数据,例如,如本答案所示,使用filter_input()

addslashes() is not a suitable escaping method for mySQL queries. 对于MySQL查询, addslashes()不是合适的转义方法。

Its better to use prepared statements as suggested here for security reasons. 出于安全原因,最好使用此处建议的准备好的语句。 Mysql_real_escape_string might not be suffiecient to prevent sql injection eg because multibyte character sets can be abused despite the escape function (). Mysql_real_escape_string可能不是suffiecient防止SQL注入例如,由于多字节字符集可以尽管逃生功能被滥用()。 mysql_real_escape_string() versus Prepared Statements . mysql_real_escape_string()与Prepared Statements的比较

Prepared statements in PHP can be used like this: PHP中的预处理语句可以这样使用:

  $stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (?, ?)");
  $stmt->bindParam(1, $name);
  $stmt->bindParam(2, $value);

More information on prepared statements in PHP . 有关PHP中准备好的语句的更多信息 So in conclusion, if you have the possibility to change your application to prepared statements, that would be the best way to handle. 因此,总而言之,如果您有可能将应用程序更改为准备好的语句,那将是最好的处理方式。

UPDATE (totally not recommended) UPDATE(完全不推荐)

If you really want to keep the state, use addslashes() for every $GET and $POST variable. 如果您确实想保留状态,则对每个$ GET和$ POST变量使用addlashes()。 It does the same manually what magic_quotes switched on did with all $GET and $POST variables. 它手动执行的操作与对$ GET和$ POST变量打开的magic_quotes所做的相同。 But i really guess its less work to use mysqli with mysqli_real_escape_string or better, prepared statements :) 但是我真的猜测将mysqli与mysqli_real_escape_string或更好的,准备好的语句一起使用的工作较少:)

http://php.net/manual/de/function.addslashes.php http://php.net/manual/de/function.addslashes.php

Because I can not introduce db layer on my application and I want a quick solution, I used addslashes() function because addslashes() escapes single quote ('), double quote ("), backslash () and NUL (the NULL byte) exactly what magic quotes escape. 因为无法在应用程序上引入db层,并且需要快速解决方案,所以我使用了adslashes()函数,因为addslashes()会转义单引号('),双引号(“),反斜杠()和NUL(NULL字节)魔术引号完全可以逃脱。

Code: 码:

    foreach (array('_COOKIE','_GET', '_POST') as $_SG) {
            foreach ($$_SG as $_SGK => $_SGV) {
                    $$_SGK = smartQuotes($_SGV);
            }
    }


    function smartQuotes($value)
    {
            if( is_array($value) ) {
                    return array_map("smartQuotes", $value);
            } else {
                    if( $value == '' ) {
                            $value = 'NULL';
                    } if( !is_numeric($value)) {
                            $value = addslashes($value);
                    }
                    return $value;
            }
    }

addslashes() gives the same results as of magic_quotes_gpc setting referring from Magic Quotes . addslashes()给出的结果与从Magic Quotes引用的magic_quotes_gpc设置相同。

When on, all ' (single-quote), " (double quote), \\ (backslash) and NULL characters are escaped with a backslash automatically. This is identical to what addslashes() does. 启用此选项后,所有的'(单引号),“(双引号),\\(反斜杠)和NULL字符都将自动以反斜杠转义。这与addslashes()相同。


Use magic_quotes_gpc on PHP 5.4 above 在以上PHP 5.4上使用magic_quotes_gpc

If you still want run magic_quotes_gpc on PHP 5.4 or higher version for your legacy code, you can use yidas/magic-quotes : 如果您仍然希望在PHP 5.4或更高版本上为旧版代码运行magic_quotes_gpc ,则可以使用yidas / magic-quotes

https://github.com/yidas/php-magic-quotes https://github.com/yidas/php-magic-quotes

We need to addslashes in Request, Post, Get & cookie. 我们需要在Request,Post,Get和Cookie中添加斜线。 You can achieve it below code. 您可以在代码下面实现。 Included below code in your common file . 通用文件中包含以下代码。

$la_magicQuotes = array('_REQUEST','_POST', '_GET','_COOKIE');
  foreach($la_magicQuotes as $la_superGlobal )
  { 
    if($$la_superGlobal && is_array($$la_superGlobal))    
      array_walk($$la_superGlobal, 'pr_addslashed_array');
  }


function pr_addslashed_array(&$la_val,$lc_key) 
{  
  if (is_array($la_val))
    array_walk($la_val,'pr_addslashed_array');
  else
    $la_val = pr_addslashed($la_val);   
}

function pr_addslashed($lc_string)
{
  return $lc_string = addslashes($lc_string);   
}

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

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