Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

global variable in absolute path?

Status
Not open for further replies.

mamabird

Programmer
Dec 19, 2006
15
US
Hello, I am new to PHP so I'm trying to struggle through this issue. I have taken over a site in which the previous developer didn't believe using a directory structure to keep files neatly organized - 90% of the pages in the site are all in the main directory. One of my goals is to clean this up by creating subdirectories for like files.

They did create a /includes/ directory where all the nav items reside, but it's set up so that when the files are included, again everything is in the main directory. As soon as I move some files down a level it breaks. I have links to js, css and images files all over the place and there will be various levels of directories.

Long story short I am going to replace all the current include code
Code:
<? include "includes/navbar.php"; ?>
with this code
Code:
<? include "/includes/navbar.php"; ?>
Problem is on the dev server the files are not in the root, but down 1 level. In production they are down 4 levels. I was thinking I could create a global variable, based on the server name, of the directory structure and add that to my include statement:
Code:
<? include DIRPATH."/includes/navbar.php"'
I don't want to define what DIRPATH is going to be in every single page - it won't ever change unless they get a new web master. [surprise]

SO...is there a way to put a global variable into memory once and have it stay there? Kinda like an application-level variable (I'm also a ColdFusion developer, and they have something like this).

Thanks in advance!
 
i think so.

first of all constants defined with define() are global in scope. so provided you have a script that you can guarantee gets run on every page, you can put the definition in that script and be comfortable that it will be globally available.

if you have no such script then create a php file and put the definition into it. then edit your php.ini file and add the directive
Code:
auto_prepend_file = 'path/to/filename.php'

this can be set in per directory php.ini files if you would rather.
 
you should be able to get the root folder via <?=$_SERVER['DOCUMENT_ROOT']?>
 
Thanks for the suggestions.

progman1010, the problem with your suggestion is that the production root is different from the development path. In production my root is and in development it's The reason being is that I have the development site on my personal web server which has numerous sites so they are all organized under subfolders. If I use <?=$_SERVER['DOCUMENT_ROOT']?> then it only gives me which will not work.

jpadie, I can't edit my .ini file because it's on a hosted server so I can't get access to it. How do I guarantee that I have a script run on every page unless I write it on every page (which I don't want to do)? I can't use /include/ because I still haven't solved for my original problem of not getting /include/ to work.

I'm looking for a solution similar to ColdFusion's Application.cfc where I can define application-level and session-level variables, and the file is run before any other file is rendered. I assume that's the same as auto_prepend_file, but what do I do if I can't control the .ini file?

Thanks!
 
jpadie, I can't edit my .ini file because it's on a hosted server so I can't get access to it.

typicaly cgi set ups will look first for a php.ini file in the directory that the script is being run from. so try creating local php files.

the coldfusion equivalent is created with the auto_prepend directive I mentioned above.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top