can i redirect to an internal link using php?

☆樱花仙子☆ 提交于 2019-12-24 12:04:06

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/16715792/can-i-redirect-to-an-internal-link-using-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!