PHP Header move up one directory?

后端 未结 4 1794
情歌与酒
情歌与酒 2020-12-09 23:54

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 


        
相关标签:
4条回答
  • 2020-12-10 00:13

    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");
    
    0 讨论(0)
  • 2020-12-10 00:13

    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");
    
    0 讨论(0)
  • 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");
    
    0 讨论(0)
  • 2020-12-10 00:32

    This is the solution which helped me redirect back to the Root(Home) directory.

    <?php
    
        header('Refresh:0 , url=/ROOT_DIR/');
    
    ?>
    
    0 讨论(0)
提交回复
热议问题