简体   繁体   English

如何使用 php header() 重定向到主页?

[英]How to use php header() to redirect to homepage?

I have a link in my dashboad page( http://localhost/dashboard.php ).我的仪表板页面中有一个链接( dashboad http://localhost/dashboard.php )。 The link is as following:链接如下:

<a class="nav-link" href="logout.php">
  <img class="icon mb-n1px" src="assets/icons/sign_out.svg" alt="category icon"> 
  Log Out
</a>

In the logout.php file, I have the following code:logout.php文件中,我有以下代码:

<?php   
    session_unset();
    header("Location: /"); 
  exit();
?>

So clicking on that link the user agent is redirected to http://localhost/ .因此,单击该链接,用户代理将重定向到http://localhost/ I want to redirect to http://localhost without the trailing slash.我想重定向到http://localhost没有斜杠。 My question is我的问题是

  1. Why does header("Location: ");为什么header("Location: "); not work?不行?
  2. Why does header("Location: ../");为什么header("Location: ../"); still lead to http://localhost/ .仍然导致http://localhost/
  3. Why does header("Location: /../");为什么header("Location: /../"); doesn't remove the trailing slash?不删除尾部斜杠?

PS: I have read the official docs https://www.php.net/manual/en/function.header.php and other SO posts but didn't find any explanation. PS:我已阅读官方文档https://www.php.net/manual/en/function.header.php和其他 SO 帖子,但没有找到任何解释。

You can use absolute url header("Location: http://localhost");您可以使用绝对 url header("Location: http://localhost");

  1. Because you need to specify where do you want to redirect因为你需要指定你想重定向到哪里
  2. Because http://localhost and http://localhost/ are equal因为http://localhosthttp://localhost/是相等的
  3. Because there is no upper level then /因为那时没有上层/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM