Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Config file or global parm?

Posted on 2004-08-12
3
Medium Priority
?
255 Views
Last Modified: 2013-11-18
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.
0
Comment
Question by:bliesveld
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11790177
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_start();
$_SESSION['MY_ROUTE'] = "/home/mysite/htdocs/";
0
 
LVL 1

Accepted Solution

by:
danutz earned 1200 total points
ID: 11790521
I don't think sessions are such a good idea in this case.

Define all these variabiles in a configuration file (something like your_configuration.inc.php) and include that file in every script with a call to require('your_configuration.inc.php');

your_configuration.inc.php
<?php
    $basePath = "/home/site/public_html";
    $anotherVar = "...";
?>

all your script should include this file

<?php
    require('your_configuration.inc.php');
   [...]
?>
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11791514
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:

<?php
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";
//etc
?>

Then at the top of each of your pages:

<?php
session_start();
if (!isset($_SESSION['set'])) {
 include($_SERVER['DOCUMENT_ROOT']."/settings.php");
}
?>

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:

$config_name
$config_another_name

etc

Good Luck

|)iablo
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question