PHP 文件名在 header() 调用期间附加到 URL 中


PHP file names appending in URL during header() call

我在profile_page.php上有一个名为Send Message的按钮:

<form method='post'>
   <input type='submit' class='btn btn-info' name='sendmsg' value='Send Message'/>
</form>

我有以下简单的代码段,单击Send Message按钮时,应该将它们带到messages.php/$user - $user是他们的用户名:

if (isset($_POST['sendmsg'])){
    header("Location: messages.php?u=$user");
    }

现在,当我单击Send Message时,URL显示

http://localhost/profile_page/messages.php?u=freddy

为什么消息.php附加到profile_page.php?

这就是 header() 函数的作用。如果要将用户重定向到更具体的路由,则应在位置开头使用/来指定更多路由。例如,header("Location: /messages.php?u=$user");将导致http://localhost/messages.php?u=freddy