简体   繁体   中英

PHP Header move up one directory?

How do I specify a file redirect to a file one level above the current file in PHP? Here is my schematic so far:

-landing.html
-ajax
  -checkLogin.php 

checklogin.php has the following code in it:

header("location:dashboard.html");

This obviously doesn't work since landing.php is one level above. How can I select a file one directory above? I already tried ..landing.php , but seems like it will only take filenames.

You should really use absolute paths (at least relative to your document root).

Consider if you move checkLogin.php a directory deeper…

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.

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. 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.

<?php

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

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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