我可以重定向到一个内部链接使用php


can i redirect to an internal link using php?

我试图设置重定向到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
什么好主意吗?

不行。Location:标头用于HTTP客户端,HTTP并不真正关心锚片段。另外,rfc要求在该头文件中指定的url必须是完整的url。

现在,你有时可以用锚片段链接到实际的资源。

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

同时,你应该知道你的代码很容易受到SQL注入攻击,如果你还没有被黑客攻击的话,你将会被黑客攻击。学习使用PDO或类似的准备/参数化查询来避免这个问题。

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

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

你可能也想看看这个答案