新旧版本 PHP 的魔法引用

发布于 2024-08-12 09:45:08 字数 1176 浏览 1 评论 0原文

这段代码应该确保干净的代码到达数据库,

它应该在早期版本的 PHP(早于 4.3.0)和更高版本的 php(早于 4.3.0)中工作,

它工作得很好,因为数据到达数据库没有问题,但我在浏览器上收到错误

$menu_name = mysql_prep($_POST['menu_name']);

是我调用 mysql_prep 函数的方式

function mysql_prep($value)
{

    $get_magic_quotes = get_magic_quotes_gpc();

    $new_enough_php = function_exists ("mysql_real_escape_string");  //check if php version is greater than 4.3.0

    if($new_enough_php) // if php is of a newer version 
    {
        //undo magic quotes effect so that mysql_real_escape_string can work well
        if ($get_magic_quotes)
        {
            $value = stripslashes ($value);
        }

        $value = mysql_real_escape_string($value);

    }
    else //mysql is older than 4.3.0    
    {
        //add slashes manually if magic quotes are off
        if(!$get_magic_quotes)
        {
            $value = addslashes ($value);
        }
        //if magic quotes already exist, slashes already exists
    }

    return $value;

    //$value = mysql_real_escape_string($value);

    //$value_without_slashes = stripslashes ($value);

    //return $value_without_slashes;

}

this code is supposed to ensure that clean code gets to the database

it is supposed to work in earlier versions of PHP (earlier than 4.3.0) and later versions of php (older than 4.3.0)

it works well because the data gets to the database without a problem but i get an error on the browser

$menu_name = mysql_prep($_POST['menu_name']);

is how i call the mysql_prep function

function mysql_prep($value)
{

    $get_magic_quotes = get_magic_quotes_gpc();

    $new_enough_php = function_exists ("mysql_real_escape_string");  //check if php version is greater than 4.3.0

    if($new_enough_php) // if php is of a newer version 
    {
        //undo magic quotes effect so that mysql_real_escape_string can work well
        if ($get_magic_quotes)
        {
            $value = stripslashes ($value);
        }

        $value = mysql_real_escape_string($value);

    }
    else //mysql is older than 4.3.0    
    {
        //add slashes manually if magic quotes are off
        if(!$get_magic_quotes)
        {
            $value = addslashes ($value);
        }
        //if magic quotes already exist, slashes already exists
    }

    return $value;

    //$value = mysql_real_escape_string($value);

    //$value_without_slashes = stripslashes ($value);

    //return $value_without_slashes;

}

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

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

发布评论

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

评论(2

枕头说它不想醒 2024-08-19 09:45:08

对于初学者来说,该函数可以缩短到大约 5 行(这样也更容易阅读)。

其次,当你调用该函数时,你是否连接到了MySQL?您必须连接 PHP 才能知道如何/什么要转义。 mysql_real_escape_string< 的手册并不是 100% 清楚/code>,但它是隐含的:

如果未指定链接标识符,则假定为 mysql_connect() 打开的最后一个链接。如果没有找到这样的链接,它将尝试创建一个,就像不带参数调用 mysql_connect() 一样。

如果仍然不能解决问题,我建议在执行查询之前打印查询并检查一切是否正常。

For starters, that function could be shortened to about 5 lines (it would be easier to read, too).

Secondly, are you connected to MySQL when you call that function? You must be connected for PHP to know how/what to escape. It's not 100% clear from the manual for mysql_real_escape_string, but it is implied:

If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments.

If that still doesn't solve it, I suggest printing the query prior to executing it and checking everything is in order.

丑丑阿 2024-08-19 09:45:08

您不应该在 DB 方法中执行此操作,如果您运行的是 PHP 5.3+,您可以将此代码放在页面的最顶部:

if (get_magic_quotes_gpc() === 1)
{
    $_GET = json_decode(stripslashes(json_encode($_GET, JSON_HEX_APOS)), true);
    $_POST = json_decode(stripslashes(json_encode($_POST, JSON_HEX_APOS)), true);
    $_COOKIE = json_decode(stripslashes(json_encode($_COOKIE, JSON_HEX_APOS)), true);
    $_REQUEST = json_decode(stripslashes(json_encode($_REQUEST, JSON_HEX_APOS)), true);
}

它处理键、值和多维数组中的魔术引号。

You shouldn't do that inside a DB method, if you're running PHP 5.3+ you can place this code on the topmost of your page:

if (get_magic_quotes_gpc() === 1)
{
    $_GET = json_decode(stripslashes(json_encode($_GET, JSON_HEX_APOS)), true);
    $_POST = json_decode(stripslashes(json_encode($_POST, JSON_HEX_APOS)), true);
    $_COOKIE = json_decode(stripslashes(json_encode($_COOKIE, JSON_HEX_APOS)), true);
    $_REQUEST = json_decode(stripslashes(json_encode($_REQUEST, JSON_HEX_APOS)), true);
}

It handles magic quotes in keys, values and multi-dimensional arrays.

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