mysql_real_escape_string() leaving slashes in MySQL

前端 未结 9 1052
囚心锁ツ
囚心锁ツ 2020-12-16 16:41

I just moved to a new hosting company and now whenever a string gets escaped using:

mysql_real_escape_string($str);

the slashes remain in the

9条回答
  •  隐瞒了意图╮
    2020-12-16 17:18

    it sounds as though you have magic quotes turned on. Turning it off isn't too hard: just create a file in your root directory called .htaccess and put this line in it:

    php_flag magic_quotes off
    

    If that's not possible for whatever reason, or you want to change your application to be able to handle magic quotes, use this technique:

    Instead of accessing the request variables directly, use a function instead. That function can then check if magic quotes is on or off and strip out slashes accordingly. Simply running stripslashes() over everything won't work, because you'll get rid of slashes which you actually want.

    function getVar($key) {
        if (get_magic_quotes_gpc()) {
            return stripslashes($_POST[$key]);
        } else {
            return $_POST[$key];
        }
    }
    
    $x = getVar('x');
    

    Now that you've got that, all your incoming variables are ready to be escaped again and mysql_real_escape_string() won't stuff them up.

提交回复
热议问题