can i redirect to an internal link using php?

2019-07-29 10:39发布

I am trying to set a redirect to an internal link with php. Basicaly, i am doing an app using jquerymobile, and the navigation requires that you navigate through divs rather than documents. However, the code i figured out looks like this

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');
    }
}

I also tryied Location:index.php#wishlist
any ideas?

标签: php hyperlink
2条回答
霸刀☆藐视天下
2楼-- · 2019-07-29 11:05

No, you can't. The Location: header is for the HTTP client, and HTTP doesn't really care about the anchor fragment. Plus, the RFCs require that URLs specified in that header be full URLs.

Now, what you can get away with sometimes is linking to the actual resource with the anchor fragment.

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

Also, you should know that your code is wide open to SQL injection attacks, and you will be hacked if you haven't been already. Learn to use prepared/parameterized queries with PDO or similar to avoid this problem.

查看更多
Animai°情兽
3楼-- · 2019-07-29 11:07

What you're trying to achieve is loading an anchor in HTML. You should try redirecting to an absolute path, like this:

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

You might also want to check out this answer.

查看更多
登录 后发表回答