Jump to content

Create Main Php Config Script File That Is Available To All Other Pages In Site


cbassett03

Recommended Posts

Is there a way that I can create a "configuration" script for a PHP-driven site, and have that page load first (to get the needed data and variables loaded), and not have to load it on each page using require() or include()?

 

Basically, I'm creating a 5-step wizard system, and I have it right now that the config.php file (which needs to be loaded before the wizard starts) defines and specifies the file locations and scripts in the wizard and in which order they are used. the config file also specifies how many steps are in the wizard, the name of the wizard, and other information.

 

Here is a little of what I"m putting in this config script file:

 


// the paths below specify where certain script files are located
// each page will dynamically create the HTML Form element's ACTION URL based on
// the "paths" given below and using the $pageIndex array elements to link files,
//
// A complete path would be coded as:  $root_Path . $process_path . $pageIndex[0][1]    (for the first page)
// which equates to:  /wizard/process/firstpage-process.php

$root_Path = "/wizard";	 // Location of the wizard main files
$process_path = "process"; // Location where page processing scripts are stored
$conn_path = "conn"; // location where DB connection scripts are stored
$ui_path = "ui";	 // Location of User-Interface script files

$pageIndex = array( array("firstpage.php","firstpage-process.php"), array("secondpage.php","secondpage-process.php"));

$pageCount = count($pageIndex);
$currentPage = 0;


NOTES:

1. I'm using a nested array so that it can be counted so that the wizard can calculate the total number of pages in the wizard on the go.

2. $currentPage is set by each page in the wizard, so it initially is set to 0 here, but the first page will set it to 1, etc.

3. I use the "path" definition idea so that pages can be moved and llayou have to do is modify this script and not have to go through each file and change them. Each file in the wizard will use these paths and will dynamically construct the FORM ACTION URL from this data in this snippet of code.

 

I'm doing this mainly so that the wizard can be updated at a later time (add pages, remove or rename pages, etc). I want to make it so that you only have to edit the

configuration in one place and not have to go through each page and update it if you decide to add a step (page) in the middle of the wizard.

 

 

But I'd like to load this config.php file once, and not have to load it on each page. Is there a way to do this? I don't think that register_globals is turned on, nor do I care to use global vars simply because I feel it's sort of a bad way of writing code. Should I just use a session? Right now, I'm just including the config.php file in every file that is used in the wizard.

 

Suggestions...

Edited by cbassett03
Link to comment
Share on other sites

You can set a file to load from within your php.ini (see the auto_prepend_file directive) , but this will then load on *all* requests. Whats wrong with using require ?

 

Problem is that I'm on a shared hosting system, and I don't think I can modify the PHP.INI settings (especially when it comes to autoloading files).

 

My concern with using require() over and over again is that doesn't that start to consume more memory after being dozens of times or does PHP "recover" memory when a page is not actively being used.

 

I am considering using the SESSION, but think I'm limited to 128MB total for SESSION variables, which this wizard wouldn't require that much, even with user data and my config settings being stored in the session, but from what I'd know of, you can't nest arrays in the $_SESSION variable.

 

What about require_once(). Can you use that on each page or would that not work. Up until now, I've developed sites mainly by hard-coding in filenames and path, but want to to take a different approach to this wizard since I may or may not be updating it in the future (someone else may take over the project in the future, so I'm trying to make it as easy to update/change as possible).

Edited by cbassett03
Link to comment
Share on other sites

Your overcomplicating things. Using require or require_once on each page makes no difference. Memory is used on a per request basis.

 

The directive within your ini I told you to look at actually uses include under the hood anyway, so it does exactly the same thing, you just wouldn't have to have the call at the top of your file.

 

As for your comment on the $_SESSION array. The $_SESSION array is just like any other array except it is persistent. You can nest all you like.

Link to comment
Share on other sites

I think you've put the entire problem on its head, which is causing you the majority of issues you're having.

 

What I'd do in your case, is:

  1. Instead of having multiple entrance files scattered all over the directory three, I'd have one entrance file ("index.php") at the root folder.
  2. Next is to reverse the order of the path and filename, so that my URLs would look like index.php/wizard/process/firststep/ or similar.
  3. Then I'd set it to include the configuration file, and do whatever else you'll need to do for each and every page viewed.
  4. Last step is to retrieve the URL to the page you want to show, from the URL, and then include said file.

 

Not only will this method make it a lot easier to do what you want, and greatly reduce the number of times you have to write the same code. It'll also allow you to add more steps simply by writing only the code that step needs, and then uploading it; All the common stuff is already done, and thus you won't have to worry about it.

 

PS: As trq said, you should be using constants for your configuration settings.

Edited by Christian F.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.