简体   繁体   English

PHP Header向上移动一个目录?

[英]PHP Header move up one directory?

How do I specify a file redirect to a file one level above the current file in PHP? 如何指定文件重定向到PHP当前文件上一级文件? Here is my schematic so far: 这是我到目前为止的原理图:

-landing.html
-ajax
  -checkLogin.php 

checklogin.php has the following code in it: checklogin.php包含以下代码:

header("location:dashboard.html");

This obviously doesn't work since landing.php is one level above. 这显然不起作用,因为landing.php是上面的一个级别。 How can I select a file one directory above? 如何在上面的一个目录中选择文件? I already tried ..landing.php , but seems like it will only take filenames. 我已经尝试了..landing.php ,但似乎它只会采用文件名。

You should really use absolute paths (at least relative to your document root). 您应该使用绝对路径(至少相对于您的文档根目录)。

Consider if you move checkLogin.php a directory deeper… 考虑一下如果你将checkLogin.php移动到更深的目录......

Any of the follow won't have a problem. 以下任何一个都不会有问题。

header("Location: /landing.html");
// or
header("Location: http://www.example.com/landing.html");

The accepted answer will take you to the root of the domain, not 1 folder up as specified. 接受的答案将带您到域的根目录,而不是指定的1个文件夹。

To go one folder up: 去一个文件夹:

header("Location: ../landing.html");

I think it is because you forgot the / after the .. 我想是因为你忘记了 /之后 ..

 
 
 
 
  
  
  header("Location: ../landing.php");
 
 
  

Update: As noted in comments, this is not up to specification , and should be an absolute URI. 更新:如评论中所述,这不符合规范 ,应该是绝对URI。 Another method if you can't get the absolute path for some reason is to use: 如果由于某种原因无法获得绝对路径,则另一种方法是使用:

$parent = dirname($_SERVER['REQUEST_URI']);
header("Location: $parent/landing.php");

This is the solution which helped me redirect back to the Root(Home) directory. 这是帮助我重定向回Root(Home)目录的解决方案。

<?php

    header('Refresh:0 , url=/ROOT_DIR/');

?>

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

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