【问题标题】:can i redirect to an internal link using php?我可以使用 php 重定向到内部链接吗?
【发布时间】:2013-05-23 13:53:21
【问题描述】:

我正在尝试使用 php 设置重定向到内部链接。基本上,我正在使用 jquerymobile 做一个应用程序,并且导航需要您浏览 div 而不是文档。但是,我想出的代码看起来像这样

if (isset($_POST['insert'])) {
    $post = $_POST['wish'];
    $fk_id = $_SESSION['id'];
    $succes = "";
    $succes .= "<h1>SUCCES</h1>";
    $insert_wish_sql = "INSERT INTO wishlist(wish_id, wish, fk_id, datetime) VALUES ('', '$post', '$fk_id' , CURDATE())";//insert new post
    $res = mysql_query($insert_wish_sql);

    if ($insert_wish_sql) {
        header('Location:#wishlist');
    }
}

我也试过 Location:index.php#wishlist
有什么想法吗?

【问题讨论】:

标签: php hyperlink


【解决方案1】:

不,你不能。 Location: 标头用于 HTTP 客户端,HTTP 并不真正关心锚片段。此外,RFC 要求该标头中指定的 URL 是完整的 URL。

现在,您有时可以避免使用锚片段链接到实际资源。

header('Location: http://example.com/someResource#wishlist');

此外,您应该知道您的代码很容易受到 SQL 注入攻击,如果您还没有被黑客入侵,那么您将会被黑客入侵。 Learn to use prepared/parameterized queries with PDO 或类似的东西来避免这个问题。

【讨论】:

    【解决方案2】:

    您想要实现的是在 HTML 中加载锚点。 您应该尝试重定向到绝对路径,如下所示:

    header('Location: http://host.name/index.php#wishlist');
    

    您可能还想查看this answer。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-21
      • 1970-01-01
      • 2014-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      相关资源
      最近更新 更多