bdee1 Posted January 23, 2009 Share Posted January 23, 2009 hi - i have an app in which i include functions.php in every file. some of the functions in functions.php open up an xml file. i have files from different locations in my file structure that call functions.php and when using the functiosn that open the xml file, sometimes it works and sometimes it doesnt because i am usign a relative path when openign the xml file. so for instance... in the root of my site i have index.php in \includes\ i have functions.php and in \data\ is my xml file in functions.php it opens the xml file like this: $xml = simplexml_load_file('../data/file.xml'); so i am wondering if there is some way that i can use a php function or something instead of the relative path so that my functions woudl work no matter where i call them from. any ideas?? Quote Link to comment https://forums.phpfreaks.com/topic/142059-file-path-for-included-files/ Share on other sites More sharing options...
trq Posted January 23, 2009 Share Posted January 23, 2009 so i am wondering if there is some way that i can use a php function or something instead of the relative path so that my functions woudl work no matter where i call them from. any ideas?? Yes. Use an absolute path starting at $_SERVER['DOCUMENT_ROOT']. Quote Link to comment https://forums.phpfreaks.com/topic/142059-file-path-for-included-files/#findComment-743946 Share on other sites More sharing options...
bdee1 Posted January 23, 2009 Author Share Posted January 23, 2009 so i am wondering if there is some way that i can use a php function or something instead of the relative path so that my functions woudl work no matter where i call them from. any ideas?? Yes. Use an absolute path starting at $_SERVER['DOCUMENT_ROOT']. thanks for the reply... i am not clear on what that does so i tried to echo it from the main page and i get the following: Undefined index: DOCUMENT_ROOT in C:\domains\blairdee.info\wwwroot\PHP\index.php on line 53 Quote Link to comment https://forums.phpfreaks.com/topic/142059-file-path-for-included-files/#findComment-743961 Share on other sites More sharing options...
uniflare Posted January 23, 2009 Share Posted January 23, 2009 Document Root is a global enviroment variable: http://uk2.php.net/reserved.variables You can also attain this variable via: getenv("DOCUMENT_ROOT"); To view a larger more detailed list of all the global variables and settings of your current apache/php installation, use: phpinfo(); Quote Link to comment https://forums.phpfreaks.com/topic/142059-file-path-for-included-files/#findComment-744074 Share on other sites More sharing options...
chronister Posted January 23, 2009 Share Posted January 23, 2009 This is a huge timesaver / stress reducer. When including files, call them like thorpe stated.. <?php include_once($_SERVER['DOCUMENT_ROOT'].'/path/to/file'); ?> I believe it works in other instances too. e.g. $xml = simplexml_load_file($_SERVER['DOCUMENT_ROOT'].'/data/file.xml'); Saved me LOADS of frustration when I discovered this. Nate Quote Link to comment https://forums.phpfreaks.com/topic/142059-file-path-for-included-files/#findComment-744105 Share on other sites More sharing options...
bdee1 Posted January 23, 2009 Author Share Posted January 23, 2009 Document Root is a global enviroment variable: http://uk2.php.net/reserved.variables You can also attain this variable via: getenv("DOCUMENT_ROOT"); To view a larger more detailed list of all the global variables and settings of your current apache/php installation, use: phpinfo(); ok i still dont follow... why am i getting... Undefined index: DOCUMENT_ROOT in C:\...\functions.php on line 168 Quote Link to comment https://forums.phpfreaks.com/topic/142059-file-path-for-included-files/#findComment-744218 Share on other sites More sharing options...
PFMaBiSmAd Posted January 23, 2009 Share Posted January 23, 2009 Based on the path in your error message, you are using IIS for a web server. You need to set the doc_root php.ini setting to point to your document_root folder. Quote Link to comment https://forums.phpfreaks.com/topic/142059-file-path-for-included-files/#findComment-744225 Share on other sites More sharing options...
bdee1 Posted January 23, 2009 Author Share Posted January 23, 2009 Based on the path in your error message, you are using IIS for a web server. You need to set the doc_root php.ini setting to point to your document_root folder. yes i am using IIS.... so assuming that ll my domains are in C:\inetpub\websites\sitename... what do i need to set doc_root to? i need php to function for several websites on my server - not just this one. Quote Link to comment https://forums.phpfreaks.com/topic/142059-file-path-for-included-files/#findComment-744231 Share on other sites More sharing options...
premiso Posted January 23, 2009 Share Posted January 23, 2009 If it were Apache this would actually be really easy. But maybe you can google Virtual Hosts for IIS, then you would need to modify your HOSTS file via c:/Windows/System32/Drivers/etc/hosts And add in different host names, like 127.0.0.1 site1.dev Then in your virtual host add site1.dev so that when it is called on your local server it will goto that site. I hate IIS, but maybe you can do some searching and solve this problem. Quote Link to comment https://forums.phpfreaks.com/topic/142059-file-path-for-included-files/#findComment-744244 Share on other sites More sharing options...
bdee1 Posted January 23, 2009 Author Share Posted January 23, 2009 If it were Apache this would actually be really easy. But maybe you can google Virtual Hosts for IIS, then you would need to modify your HOSTS file via c:/Windows/System32/Drivers/etc/hosts And add in different host names, like 127.0.0.1 site1.dev Then in your virtual host add site1.dev so that when it is called on your local server it will goto that site. I hate IIS, but maybe you can do some searching and solve this problem. ok that sounds like an incredible pain, so... i was thinking that i could just add something like $siteRoot = "C:\inetpub\websites\sitename" to a common.php file. and then just include common.php in every page. i was already including common.php in every page for another reason so i thought this woudl be easy. well the line i am having problems with (the one opening the xml file) in in siteroot\includes\functions.php. and functions.php could be called from any page anywhere in the site. si i will have a problem is i have to include common.php in functions.php. so i was thinking i could just include common.php in any page the includes functions.php (as long as in included common.php before i include functions.php. but for some reason when i try that, it says Undefined variable: siteRoot in C:\...\functions.php wouldnt functions.php be able to grab the $siteRoot variable from the include of common.php on the calling page (siteroot\index.php)?? Quote Link to comment https://forums.phpfreaks.com/topic/142059-file-path-for-included-files/#findComment-744259 Share on other sites More sharing options...
bdee1 Posted January 23, 2009 Author Share Posted January 23, 2009 ok so i can tell for sure that both common.php and functions.php are being included (in that order)... so why cant functions.php see the $siteroot variable defined in common.php?? Quote Link to comment https://forums.phpfreaks.com/topic/142059-file-path-for-included-files/#findComment-744271 Share on other sites More sharing options...
premiso Posted January 23, 2009 Share Posted January 23, 2009 If it were Apache this would actually be really easy. But maybe you can google Virtual Hosts for IIS, then you would need to modify your HOSTS file via c:/Windows/System32/Drivers/etc/hosts And add in different host names, like 127.0.0.1 site1.dev Then in your virtual host add site1.dev so that when it is called on your local server it will goto that site. I hate IIS, but maybe you can do some searching and solve this problem. ok that sounds like an incredible pain, so... i was thinking that i could just add something like $siteRoot = "C:\inetpub\websites\sitename" to a common.php file. and then just include common.php in every page. i was already including common.php in every page for another reason so i thought this woudl be easy. well the line i am having problems with (the one opening the xml file) in in siteroot\includes\functions.php. and functions.php could be called from any page anywhere in the site. si i will have a problem is i have to include common.php in functions.php. so i was thinking i could just include common.php in any page the includes functions.php (as long as in included common.php before i include functions.php. but for some reason when i try that, it says Undefined variable: siteRoot in C:\...\functions.php wouldnt functions.php be able to grab the $siteRoot variable from the include of common.php on the calling page (siteroot\index.php)?? For IIS it is probably a pain, for Apache it is actually really really simple and totally worth it for not having to change the server configuration file anytime you want to change servers. I think your logic is not seeing the total picture. I could just be gathering the wrong conclusion out of this, so here is what I think you are trying to figure out: You have multiple sites on your box. You want to be able to switch from siteA which is located at c:\wwwroot\siteA to siteB which is located at c:\wwwroot\siteB without having to modify the configuration file and change the document root each time. Right? If that is right, then doing that in commons.php will not help you, unless each page of your script goes and calls that identical page for siteB and includes it. Due to the simple fact that siteA files are probably not the same as siteB files. Unless SiteA is just a mirror of siteB, in which case your logic may work. If that is not the case, sorry man, but the work around is not that simple. If my impression was wrong, totally disregard everything I posted. To include something no matter what the path is, $_SERVER['DOCUMENT_ROOT'] is the key, just remember you still have to define /path/after/root/to/file to get it to include right. As for the $siteRoot not being defined, are you sure you are including the new version of the commons.php? EDIT: Are you also sure you are not trying to use $siteRoot inside a function or class? If you plan to do that you need to use define and make SITEROOT a constant to have a global scope. Quote Link to comment https://forums.phpfreaks.com/topic/142059-file-path-for-included-files/#findComment-744274 Share on other sites More sharing options...
PFMaBiSmAd Posted January 23, 2009 Share Posted January 23, 2009 This link looks like (I did not read the whole thing) it provides the answer as to how to set up a per-site php configuration under IIS - http://blogs.iis.net/ruslany/archive/2008/07/12/per-site-php-configuration-with-iis-fastcgi.aspx Quote Link to comment https://forums.phpfreaks.com/topic/142059-file-path-for-included-files/#findComment-744280 Share on other sites More sharing options...
bdee1 Posted January 23, 2009 Author Share Posted January 23, 2009 If it were Apache this would actually be really easy. But maybe you can google Virtual Hosts for IIS, then you would need to modify your HOSTS file via c:/Windows/System32/Drivers/etc/hosts And add in different host names, like 127.0.0.1 site1.dev Then in your virtual host add site1.dev so that when it is called on your local server it will goto that site. I hate IIS, but maybe you can do some searching and solve this problem. ok that sounds like an incredible pain, so... i was thinking that i could just add something like $siteRoot = "C:\inetpub\websites\sitename" to a common.php file. and then just include common.php in every page. i was already including common.php in every page for another reason so i thought this woudl be easy. well the line i am having problems with (the one opening the xml file) in in siteroot\includes\functions.php. and functions.php could be called from any page anywhere in the site. si i will have a problem is i have to include common.php in functions.php. so i was thinking i could just include common.php in any page the includes functions.php (as long as in included common.php before i include functions.php. but for some reason when i try that, it says Undefined variable: siteRoot in C:\...\functions.php wouldnt functions.php be able to grab the $siteRoot variable from the include of common.php on the calling page (siteroot\index.php)?? For IIS it is probably a pain, for Apache it is actually really really simple and totally worth it for not having to change the server configuration file anytime you want to change servers. I think your logic is not seeing the total picture. I could just be gathering the wrong conclusion out of this, so here is what I think you are trying to figure out: You have multiple sites on your box. You want to be able to switch from siteA which is located at c:\wwwroot\siteA to siteB which is located at c:\wwwroot\siteB without having to modify the configuration file and change the document root each time. Right? If that is right, then doing that in commons.php will not help you, unless each page of your script goes and calls that identical page for siteB and includes it. Due to the simple fact that siteA files are probably not the same as siteB files. Unless SiteA is just a mirror of siteB, in which case your logic may work. If that is not the case, sorry man, but the work around is not that simple. If my impression was wrong, totally disregard everything I posted. To include something no matter what the path is, $_SERVER['DOCUMENT_ROOT'] is the key, just remember you still have to define /path/after/root/to/file to get it to include right. As for the $siteRoot not being defined, are you sure you are including the new version of the commons.php? EDIT: Are you also sure you are not trying to use $siteRoot inside a function or class? If you plan to do that you need to use define and make SITEROOT a constant to have a global scope. yea i think you misunderstood... i would LIKE to have it set up so that i could use $_SERVER['DOCUMENT_ROOT'] regardless of what site i am on (i host several) on my server. but once i realized what a pain that woudl be on IIS, i changed my approach. i figured that by defining $siteRoot in the common.php file and then including that common.php file in ever file in my site, that might work... then for each site i would just have to have a common.php file which defines the $siteRoot for that site. Quote Link to comment https://forums.phpfreaks.com/topic/142059-file-path-for-included-files/#findComment-744286 Share on other sites More sharing options...
premiso Posted January 23, 2009 Share Posted January 23, 2009 Gotcha. Are you also sure you are not trying to use $siteRoot inside a function or class? If you plan to do that you need to use define() and make SITEROOT a constant to have a global scope. Could that be causing the issue? Other than that, I am not sure how IIS really works and maybe it changes scopes of stuff, I dunno. I would make it a constant anyways, just makes everything that much easier. Quote Link to comment https://forums.phpfreaks.com/topic/142059-file-path-for-included-files/#findComment-744297 Share on other sites More sharing options...
bdee1 Posted January 23, 2009 Author Share Posted January 23, 2009 Gotcha. Are you also sure you are not trying to use $siteRoot inside a function or class? If you plan to do that you need to use define() and make SITEROOT a constant to have a global scope. Could that be causing the issue? Other than that, I am not sure how IIS really works and maybe it changes scopes of stuff, I dunno. I would make it a constant anyways, just makes everything that much easier. yea that was it - i was tryign to access it from inside a function... made it a constant and now it seems to work... thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/142059-file-path-for-included-files/#findComment-744301 Share on other sites More sharing options...
PFMaBiSmAd Posted January 23, 2009 Share Posted January 23, 2009 Aside from my post above with a link on you how you can get IIS to use a different php configuration for each site getting lost in the over-use of quoting entire replies within replies.., is there a reason why you are using IIS. Are you doing anything that requires IIS? Please don't quote entire posts in your reply. We know you are replying to what has already been said in the thread because we just read it and don't need to see everything 2-3 times. Only quote a specific part of a post if you are specifically calling attention to that part of the post in your reply. If you are on topic with your reply there is no need to quote anything. When 90% of a post must be scrolled over because it is just a quote, it makes for a long read to just find that you wrote one or two relevant sentences at the end of the post. Quote Link to comment https://forums.phpfreaks.com/topic/142059-file-path-for-included-files/#findComment-744322 Share on other sites More sharing options...
bdee1 Posted January 23, 2009 Author Share Posted January 23, 2009 sorry abotu the over quoting - i will be sure to keep that in mind in the future. and yes I am usign IIS because i also host sites which use coldfusion and IIS. plus i use Helm control panel which uses IIS. Quote Link to comment https://forums.phpfreaks.com/topic/142059-file-path-for-included-files/#findComment-744325 Share on other sites More sharing options...
printf Posted January 23, 2009 Share Posted January 23, 2009 Just do something like... /* merge the application's environment variables */ $_SERVER = array_merge ( $_SERVER, $_ENV ); Then you will always find the document root in... $_SERVER['DOCUMENT_ROOT'] IIS 5 it's in $_SERVER, IIS 6 $_ENT, IIS 7 $_SERVER if running ISAPI, if running php cgi it's in $_ENT also it's the same as $_SERVER['APPL_PHYSICAL_PATH'] You also run this this little script to see what is configured... <?php print_r ( $_SERVER ); print_r ( $_ENV ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/142059-file-path-for-included-files/#findComment-744338 Share on other sites More sharing options...
PFMaBiSmAd Posted January 23, 2009 Share Posted January 23, 2009 The helm control panel should (I don't know this for a fact for php settings, only based on it doing this for dns zone entries) have a way of automatically setting up per-site settings using some sort of account/domain place holders in the settings. Edit: Similar to what printf posted, do a phpinfo(); statement to see what server/env variables there are to see if your document root path for each site is in one. If I remember correctly, there is a php.ini setting that controls if ENV settings are placed into the $_SERVER variables. Quote Link to comment https://forums.phpfreaks.com/topic/142059-file-path-for-included-files/#findComment-744348 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.