简体   繁体   中英

php - header() does not work when it must redirect to the page from more then 2 nested folders

I have a .php file which is called when the form is submitted. By using isset($_POST["submit"]) {} else { Header() } in that file I am trying to prevent users to type direct URL to that file from browser. It works well when I have a situation like this:

Location of form submitting .php file

root_folder/folder_level_1/folder_level_2/form_submitting.php

Location of index page which header() redirects to

root_folder/folder_level_1/index.php

And the .php code of the form_submitting.php is:

<?php 
session_start();

    if(isset($_POST["submit"])) {
        // some code
    } else {
        header("Location: ../index.php");
        exit();
?>

But it does not work in this situation:

Location of form submitting .php file

root_folder/folder_level_1/folder_level_2/folder_level_3/form_submitting.php

Location of index page which header() redirects to

root_folder/folder_level_one/index.php

And the .php code of the form_submitting.php is:

<?php 
session_start();

    if(isset($_POST["submit"])) {
        // some code
    } else {
        header("Location: .../index.php");
        exit();
?>

I also tried to change header() parameters to this:

header("Location: ...index.php");
header("Location: .././index.php");
header("Location: ./../index.php");
header("Location: ../.index.php");
header("Location: ./..index.php");
header("Location: ".$_SERVER["DOCUMENT_ROOT"]."/folder_level_1/index.php");

And still, it does not work. It shows me Access forbidden! on web page instead to redirect me to index.php .

How to solve this? The help would be really appreciated.

You forget to write if. it should be if(isset($_POST['submit']...

       if( isset($_POST["submit"])) {
            // some code
        } else {
            header("Location: ../../index.php");
            exit();
    ?>

you forget closing quotes and also ../index.php if you want to go one directory up.

try, if you want to to go 2 folders up.

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

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