简体   繁体   English

PHP头(位置:)重定向与根目录

[英]PHP header(location:) redirect with root directory

Is there a way to create a header('Location:'); 有没有办法创建header('Location:'); path that starts to the root directory. 从根目录开始的路径。

Examples Like: 例如:

formaction="/cert/forms/sessions/session-inc-info.php" 

or 要么

include($_SERVER['DOCUMENT_ROOT'].'/cert/forms/sessions/session-inc-info.php');  

cert is my root directory folder cert是我的根目录文件夹

ive been trying codes like 我一直在尝试代码

header('Location:'.$_SERVER['DOCUMENT_ROOT'].'/cert/forms/sessions/session-inc-info.php);

and

header('Location:/cert/forms/sessions/session-inc-info.php');

the only thing works for me is using ../ 唯一对我../是使用../

header('Location:../../../session-inc-info.php');

but it's hard for me because i have alot of folders and subfolders. 但这对我来说很难,因为我有很多文件夹和子文件夹。 and i have so many scripts that have to be redirected. 我有很多必须重定向的脚本。

Thanks. 谢谢。

so this is the sample folder structure 所以这是示例文件夹结构

在此输入图像描述

so the content.php will have a button that redirects to the session.php and i have done that already and now when the session.php script will have a header('Location:'); 所以content.php将有一个重定向到session.php的按钮,我已经完成了,现在当session.php脚本有一个header('Location:'); that must redirect to the dashboard.php . 必须重定向到dashboard.php。 now that is my problem. 现在这是我的问题。 i don't now how to create the path like 我现在不知道如何创建路径

cert---->dashboard----->dashboard.php CERT ---->仪表板-----> dashboard.php

header("Location: http://".$_SERVER['HTTP_HOST'].$formaction;

location应包含URL,而不是服务器文件路径

You could theoretically create a PHP File at the Root of your Application and call it anything you like: say redirect.php . 理论上,您可以在应用程序的根目录下创建一个PHP文件,并将其调用为您喜欢的任何内容:例如redirect.php Then inside of this redirect.php , you could put some code that might look something like this: 然后在这个redirect.php ,你可以放一些看起来像这样的代码:

<?php
    // CREATE AN ARRAY THAT WITH NUMERIC KEY
    // THAT WILL CORRESPOND TO THE FILE YOU WISH TO REDIRECT TO (ACTUALLY LOAD)
    // SINCE THIS IS A PSEUDO REDIRECT:
    $fileMap            = array(
        0   => "./file-1.php",
        1   => "./file-2.php",
        2   => "./file-3.php",
        3   => "./file-4.php",
        4   => "./file-5.php",
        5   => "./file-6.php",
        6   => "/cert/forms/sessions/session-inc-info.php",
        7   => "./../../session-inc-info.php",  //<== YOU MAY KEEP ADDING TO LIST
    );

    // CHECK THAT THERE IS A GET PARAM WITH NAME: to
    if(isset($_GET['to'])){
        $fileKey        = $_GET['to'];          //<== GET THE `to` PARAM FROM $_GET
        $loadableFile   = $fileMap[$fileKey];   //<== TRANSLATE `to` KEY TO A LOADABLE FILE

        // IF THE FILE [$loadableFile] EXISTS, LOAD IT
        // OTHERWISE REDIRECT TO MAIN-APPLICATION PAGE (HOMEPAGE)
        if(file_exists($loadableFile)){
            require_once $loadableFile;
        }else{
            header("location: index.php");      //<== ASSUMES index.php IS THE MAIN FILE.
            exit;
        }
    }

    // IF ALL FAILS, STILL REDIRECT TO THE MAIN-APPLICATION PAGE
    header("location: index.php");              //<== ASSUMES index.php IS THE MAIN FILE.

Then on each page where you wish to Perform a redirect, you may simply do something like this: 然后,在您希望执行重定向的每个页面上,您可以执行以下操作:

<?php
    // ASSUMING YOU WANT TO RE-DIRECT TO: `./../../session-inc-info.php`
    // AS YOU CAN SEE FROM THE `$fileMap` ARRAY, `./../../session-inc-info.php`
    // HAS THE KEY OF "7"
    // NOW ALL YOU CAN DO IS SIMPY APPEND "?to=7" TO `redirect.php` 
    // IN THE `header()` METHOD LIKE THIS:
    header("location: redirect.php?to=7"); 
    exit;
    /*EXPECTED TO LOAD THE FILE: `./../../session-inc-info.php`

NOTE: 注意:
This is not really a Redirect (as it is basically just loading[including] Files from a given Location/Path, however since it seems you are trying to load some file like: /cert/forms/sessions/session-inc-info.php , it is assumed that this may be an option. To do a real Redirect: just use the header() Function directly passing it the exact URL-Location to which to redirect. 这实际上不是重定向(因为它基本上只是从给定的位置/路径加载[包括]文件,但是因为它似乎正在尝试加载一些文件,如: /cert/forms/sessions/session-inc-info.php ,假设这可能是一个选项。 要做一个真正的重定向:只需使用header()函数直接传递给它重定向的确切URL-Location。

Cheers and Good-Luck... 干杯和好运......

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

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