Config file or global parm?

Here is what I want to do:

I have a few variables that I want to use in almost everypage/script  how can I set these variables one time the use them as I wish?  I am trying to do somthing simmilar to the $_SERVER['DOCUMENT_ROOT"] call so I can access my root path of home/mysite/htdoocs/.

I would like to define this one time so if i move host I only have to redefine it in the one file.
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

danutzConnect With a Mentor Commented:
I don't think sessions are such a good idea in this case.

Define all these variabiles in a configuration file (something like and include that file in every script with a call to require('');
    $basePath = "/home/site/public_html";
    $anotherVar = "...";

all your script should include this file

Jaime OlivaresSoftware ArchitectCommented:
I think a global variable will be enough, but use $_SESSION instead.
I always have a common.php where I put all that I want to share to the entire site. So you can create your common file and write:

$_SESSION['MY_ROUTE'] = "/home/mysite/htdocs/";
I think a combination of both of the above suggestions may be useful

in the root of your site have a file callled settings.php

which will contain something along the lines of the following:

if ($_SERVER['PHP_SELF'] == "/settings.php") {
 die("you can not access this script directly");
$_SESSION['set'] = true;
//load session settings
$_SESSION['setting_name'] = "value";
$_SESSION['another_setting_name'] = "another value";

Then at the top of each of your pages:

if (!isset($_SESSION['set'])) {

The code flow is basically if the session configuration settings are not set then include the file that contains them and assign them to session vars. (NOTE: there is also a check in the settings.php so that if it is accessed directly - as opposed to being included - the script will die).

You can then access the settings anywhere in your site using $_SESSION['setting_name'].

The above example posted danutz will work just as well but it means you will be working with local variables instead, ie. $setting_name rather then $_SESSION['setting_name']. When working with settings i prefer to keep them seperate and use super globals to handle them as then when you modify the script you know exactly which variables are related to settings and which are not. If you use danutz method then i recommend that you add a prefix to the variable names to avoid confusing eg:



Good Luck

All Courses

From novice to tech pro — start learning today.