Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP header(location:) redirect with root directory

Tags:

php

Is there a way to create a 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

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

enter image description here

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:'); that must redirect to the dashboard.php . now that is my problem. i don't now how to create the path like

cert---->dashboard----->dashboard.php

like image 874
Vincent Dapiton Avatar asked Nov 17 '16 09:11

Vincent Dapiton


2 Answers

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

location should contain URL, not server file path

like image 89
AlexandrX Avatar answered Nov 15 '22 00:11

AlexandrX


You could theoretically create a PHP File at the Root of your Application and call it anything you like: say redirect.php. Then inside of this redirect.php, you could put some code that might look something like this:

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

Cheers and Good-Luck...

like image 26
Poiz Avatar answered Nov 14 '22 22:11

Poiz