存储在 MySQL 中的 URL 返回每个正斜杠带有额外的反斜杠 ( a/b.png => a\/b.png)

发布于 2024-12-03 13:34:47 字数 1510 浏览 1 评论 0 原文

我有一个带有 URL 字段的 MySql 数据库,其中包含带有正斜杠的普通 URL。当我使用 PHP Web 服务获取 URL 数据时,它显示每个正斜杠都有一个反斜杠:

http://example.com/iphone/images/test.png

显示为

http:\/\/example.com\/iphone\/images\/test.png

可能是什么问题?


这是获取我的数据的函数。

function getdata() {

    // Check for required parameters
    if (isset($_POST["genre"])) {

        // Put parameters into local variables
        $genre = $_POST["genre"];

        // Final result array
        $final_result = array();

        // Look up in database
        $user_id = 0;
        $stmt = $this->db->prepare('SELECT ID, BAND, VENUE, GENRE, DATE, THUMBNAIL_URL, DESCRIPTION FROM shows WHERE GENRE=?');
        $stmt->bind_param("s", $genre);
        $stmt->execute();
        $stmt->bind_result($id, $band_result, $venue_result, $genre, $date, $thumbnail_url, $description);
        while ($stmt->fetch()) {


            $thumbnail_url = stripslashes($thumbnail_url);

            $result = array(
            "id" => $id, "band" => $band_result, "venue" => $venue_result, "genre" => $genre, "date" => $date, "thumbnail_url" => $thumbnail_url, "description" => $description,
            );
            $final_result[] = $result;
            continue;
        }
        $stmt->close();





        sendResponse(200, json_encode($final_result));
        return true;
    }
    sendResponse(400, 'Invalid request');
    return false;
}

I have a MySql database with a URL field that has normal URLs with forward slashes. When I get the URL data with PHP web service it shows up with a backward slash for each forward slash:

http://example.com/iphone/images/test.png

shows as

http:\/\/example.com\/iphone\/images\/test.png

What could be the problem?


Here is the function that gets my data.

function getdata() {

    // Check for required parameters
    if (isset($_POST["genre"])) {

        // Put parameters into local variables
        $genre = $_POST["genre"];

        // Final result array
        $final_result = array();

        // Look up in database
        $user_id = 0;
        $stmt = $this->db->prepare('SELECT ID, BAND, VENUE, GENRE, DATE, THUMBNAIL_URL, DESCRIPTION FROM shows WHERE GENRE=?');
        $stmt->bind_param("s", $genre);
        $stmt->execute();
        $stmt->bind_result($id, $band_result, $venue_result, $genre, $date, $thumbnail_url, $description);
        while ($stmt->fetch()) {


            $thumbnail_url = stripslashes($thumbnail_url);

            $result = array(
            "id" => $id, "band" => $band_result, "venue" => $venue_result, "genre" => $genre, "date" => $date, "thumbnail_url" => $thumbnail_url, "description" => $description,
            );
            $final_result[] = $result;
            continue;
        }
        $stmt->close();





        sendResponse(200, json_encode($final_result));
        return true;
    }
    sendResponse(400, 'Invalid request');
    return false;
}

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

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

发布评论

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

评论(2

君勿笑 2024-12-10 13:34:47

发生这种情况的原因是您的主机很可能具有 magic_quotes(通过转义用户提交的输入来加强安全性的徒劳尝试)。

因此,最好检查其是否已启用。 (特别是如果您的脚本达到其他配置)

然后,如果在插入数据库之前处理删除那些不需要的额外斜杠,仍然始终使用 mysql_real_escape_sting()PDO 准备好的语句转义到数据库中,否则会出现错误或mysql 注入将随之而来。

if(get_magic_quotes_gpc()) {
  $genre = stripslashes($_POST["genre"]);
}

The reason this can happen is your host most likely has magic_quotes enabled and it can change from host to host. (a futile attempt to strengthen security by escaping user submitted inputs).

So its always best to check if its enabled. (especially if your script reaches other configurations)

Then if so handle the stripping of thos unwanted extra slashes before you insert into database, still always use mysql_real_escape_sting() or PDO prepared statements to escape into the database else errors or mysql injections will follow.

if(get_magic_quotes_gpc()) {
  $genre = stripslashes($_POST["genre"]);
}
孤单情人 2024-12-10 13:34:47

如果它在数据库中显示为斜杠;没关系。

如果您想在将它们从数据库中取出后在 PHP 中删除它们,请通过 stripslashes() 运行它;

$url = stripslashes($url);

如果您想在进入数据库之前删除斜杠...与上面相同,但在这样做之前请三思:)

If it's showing up in the database with slashes; that's fine.

If you want to remove them in your PHP after getting them out of the database, run it through stripslashes();

$url = stripslashes($url);

If you want to strip the slashes before it goes into the database... same as above, but think twice before you do :)

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