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
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
header("Location: http://".$_SERVER['HTTP_HOST'].$formaction;
location should contain URL, not server file path
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 thisredirect.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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With