Forum Moderators: coopster
1.i have a config.php with all my paths variables in it. my question is how do i create a variable which points to config.php, from any ,anywhere in the directory structure at the moment my approach is shown below.also could you tell me if there a better method for doing this.
//this is at the top of each page in my site
require ('http://'.$_SERVER['SERVER_NAME'].'/allincludes.php');
allincludes.php
<?php
require ('pageclass.php');
require ('config.php');
session_start();
?>
2.Some other question
will a session started in an included file work for the page it was called from. Also will it work when the session that is started is in file thats two includes down?
3.will a object created in an included file, work for the page it was called from, and will it work even when the object being included is in file two includes down?
Separate related classes and functions into separate include files.
Then I suggest that you use require_once() for the libraries that you need on each .php file.
That reduces the load on the server, and improves the response time, because only the functions or classes that are relevant need to loaded.
In short, require only what you require for the page to work.
see:
[uk.php.net...]
The depth of the include or require is irrelevant. It acts PHP acts as if you had cut and pasted the included file into the place that it is included.
The manual says:
When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.
won't give you the code that you need. This will make a request for your webserver to interpret your allincludes.php and will only give your script the output from that file.
Really the best way that I've come across to manage your directory structure is to keep all related things together in their own directories, with your config file in the main directory where you can reference it by backstepping through the directory structure.
Example:
~/public_html/php_project1 is your base directory
-> classes/
-> php_project_class1.php
-> php_project_class2.php
-> php_project_class3.php
-> graphics/
-> graphic_file1.jpg
-> graphic_file2.jpg
-> graphic_file3.jpg
-> javascript/
-> css/
-> config.php
-> edit_something.php
-> edit_something_else.php
-> index.php
Using this method, to get config.php into index.php, you do this because it's in the same directory:
require_once('config.php'); To get at it from php_project_class1.php (in the classes directory):
require_once('../config.php'); Hope this helps.
-ix