Re: Relative paths in require_once problem (possibly all include
Re: Relative paths in require_once problem (possibly all include
am 03.01.2008 01:42:41 von Logos
On Jan 2, 3:48 pm, Jerry Stuckle wrote:
> Logos wrote:
> > On Dec 17 2007, 5:27 am, Jerry Stuckle
> > wrote:
> >> Royan wrote:
> >>> Thanks Steve, thats a great idea, i've especially liked that part :)
> >>>>> $relativePath = strlen($parsedUri) - strlen($relativeUri) - 1;
> >>> Unfortunately this approach works great only if you can modify PHP.ini
> >>> but when you are on virtual hosting, the only way you can modify
> >>> settings in PHP.ini is by calling ini_set() function which has to be
> >>> invoced from somewhere. And in my case this "somewhere" is global.inc
> >>> This file is meant to keep all global stuff so it has to be included
> >>> into each and every file in my project, but this is the original
> >>> problem -- i can'tincludeit. Seems to be a vicious circle.
> >>> The only solution i can think of right now is to use the absolute path
> >>> for "global.inc" in each call to require_once. Thus i can put your
> >>> code that calculates relative path in "global.inc" and use it across
> >>> all other files.
> >>> 2BKDotCom
> >>>>> logger.inc doesn't need toincludeglobal.inc as long as global.inc
> >>>>> has been included before logger.inc is included..
> >>> It appears I've made a mistake in my original post. In fact you don't
> >>> have toinclude"global.inc" into foo.php, the error would persist. If
> >>> you wish I can send you test files that replicate the problem
> >>> On Dec 17, 2:30 am, "Steve" wrote:
> >>>> "Royan" wrote in message
> >>>>news:8769e733-17fe-4083-865c-ccd496707023@e25g2000prg.go oglegroups.com...
> >>>>> Ok the problem is quite hard to explain, but i'll try to keep it as
> >>>>> simple as i can. Imagine I have the following structure of my files
> >>>>> and folders:
> >>>>> /root/global.inc
> >>>>> |__/files/foo.php
> >>>>> |__/utils
> >>>>> |__/logs/logger.inc
> >>>>> When I run foo.php I get the following error:
> >>>>> ==========
> >>>>> Fatal error: require_once() [function.require]: Failed opening
> >>>>> required '../../global.inc' (include_path='.;E:\www\root\') in E:\www
> >>>>> \root\utils\logs\logger.inc on line 3
> >>>>> ==========
> >>>>> That error occurs because
> >>>>> 1) "global.inc" is included ("required") into "logger.inc" and
> >>>>> "foo.php"
> >>>>> 2) "logger.inc" is included into "foo.php"
> >>>>> See, foo.php includes its file as "../global.inc" and logger.inc
> >>>>> "../../global.inc" (note relative path differs)
> >>>>> So if you now try to run "foo.php" the require_once from "logger.inc"
> >>>>> would start looking for "global.inc" relatively /root/files which is
> >>>>> wrong.
> >>>>> My question is... how do I make PHPincludefiles relative to their
> >>>>> location not their current "include" directory?
> >>>> i know what you mean. there are other solutions but this one was a quick fix
> >>>> for me and avoids some other setup/config difference on various systems.
> >>>> anyway, i use the following code. if you put it into a file called
> >>>> relative.path.php, save the file in your php.ini include_path. from then on
> >>>> in all of your scripts, all you have to do is this:
> >>>>
> >>>> require_once 'relative.path.php';
> >>>> require_once $relativePath . 'global.inc';
> >>>> ?>
> >>>> put that in logger.inc and foo.php and nothing blows up. here's the code for
> >>>> relative.path.php:
> >>>>
> >>>> $parsedUri = dirname($_SERVER['PHP_SELF']);
> >>>> $parsedUri .= substr($parsedUri, -1) != '/' ? '/' : '';
> >>>> $relativeUri = str_replace('/', '', $parsedUri);
> >>>> $relativePath = strlen($parsedUri) - strlen($relativeUri) - 1;
> >>>> if ($relativePath < 0){ $relativePath = 0; }
> >>>> $relativePath = str_repeat('../', $relativePath);
> >>>> if (!$relativePath){ $relativePath = './'; }
> >>>> ?>
> >>>> hth.
> >> Royan,
>
> >> BKdotcom has the right answer. Use $_SERVER['DOCUMENT_ROOT'] to get to
> >> the root directory of your site, then refer to everything form there.
>
> >> It works on all sites, and requires no modification the php.ini file.
> >> And the beauty of it is, if you have a different location for the root
> >> directory on your development site, the code still works in both places.
>
> >> --
> >> ==================
> >> Remove the "x" from my email address
> >> Jerry Stuckle
> >> JDS Computer Training Corp.
> >> jstuck...@attglobal.net
> >> ==================
>
> > FYI, $_SERVER['DOCUMENT_ROOT'] does NOT work on IIS, at least not with
> > PHP 5.
>
> It works just fine. I've got it going on my system here and on a
> Windows IIS VPS.
>
> You just have to have PHP installed as an ISAPI app - not a CGI.
>
> BTW - the same it true under Apache, IIRC.
Unfortunately our server is hosted, so I can't fiddle the install.
Also, for a newbie like myself, that would be a fairly arcane bit of
knowledge about server install/configuration.
I've been using 'require_once(dirname(__FILE__)."/../
someaught.php")'...likely there's some hideous downside to doing it
that way, but it certainly has saved me some headaches trying to
resolve dependency calls when a file includes a file that includes a
file...I tried using Maarten Manders' smartloader class with it's
__autoload trick, but couldn't get it to work. :P
Tyler
Re: Relative paths in require_once problem (possibly all include routines)
am 03.01.2008 05:20:24 von Jerry Stuckle
Logos wrote:
> On Jan 2, 3:48 pm, Jerry Stuckle wrote:
>> Logos wrote:
>>> On Dec 17 2007, 5:27 am, Jerry Stuckle
>>> wrote:
>>>> Royan wrote:
>>>>> Thanks Steve, thats a great idea, i've especially liked that part :)
>>>>>>> $relativePath = strlen($parsedUri) - strlen($relativeUri) - 1;
>>>>> Unfortunately this approach works great only if you can modify PHP.ini
>>>>> but when you are on virtual hosting, the only way you can modify
>>>>> settings in PHP.ini is by calling ini_set() function which has to be
>>>>> invoced from somewhere. And in my case this "somewhere" is global.inc
>>>>> This file is meant to keep all global stuff so it has to be included
>>>>> into each and every file in my project, but this is the original
>>>>> problem -- i can'tincludeit. Seems to be a vicious circle.
>>>>> The only solution i can think of right now is to use the absolute path
>>>>> for "global.inc" in each call to require_once. Thus i can put your
>>>>> code that calculates relative path in "global.inc" and use it across
>>>>> all other files.
>>>>> 2BKDotCom
>>>>>>> logger.inc doesn't need toincludeglobal.inc as long as global.inc
>>>>>>> has been included before logger.inc is included..
>>>>> It appears I've made a mistake in my original post. In fact you don't
>>>>> have toinclude"global.inc" into foo.php, the error would persist. If
>>>>> you wish I can send you test files that replicate the problem
>>>>> On Dec 17, 2:30 am, "Steve" wrote:
>>>>>> "Royan" wrote in message
>>>>>> news:8769e733-17fe-4083-865c-ccd496707023@e25g2000prg.google groups.com...
>>>>>>> Ok the problem is quite hard to explain, but i'll try to keep it as
>>>>>>> simple as i can. Imagine I have the following structure of my files
>>>>>>> and folders:
>>>>>>> /root/global.inc
>>>>>>> |__/files/foo.php
>>>>>>> |__/utils
>>>>>>> |__/logs/logger.inc
>>>>>>> When I run foo.php I get the following error:
>>>>>>> ==========
>>>>>>> Fatal error: require_once() [function.require]: Failed opening
>>>>>>> required '../../global.inc' (include_path='.;E:\www\root\') in E:\www
>>>>>>> \root\utils\logs\logger.inc on line 3
>>>>>>> ==========
>>>>>>> That error occurs because
>>>>>>> 1) "global.inc" is included ("required") into "logger.inc" and
>>>>>>> "foo.php"
>>>>>>> 2) "logger.inc" is included into "foo.php"
>>>>>>> See, foo.php includes its file as "../global.inc" and logger.inc
>>>>>>> "../../global.inc" (note relative path differs)
>>>>>>> So if you now try to run "foo.php" the require_once from "logger.inc"
>>>>>>> would start looking for "global.inc" relatively /root/files which is
>>>>>>> wrong.
>>>>>>> My question is... how do I make PHPincludefiles relative to their
>>>>>>> location not their current "include" directory?
>>>>>> i know what you mean. there are other solutions but this one was a quick fix
>>>>>> for me and avoids some other setup/config difference on various systems.
>>>>>> anyway, i use the following code. if you put it into a file called
>>>>>> relative.path.php, save the file in your php.ini include_path. from then on
>>>>>> in all of your scripts, all you have to do is this:
>>>>>>
>>>>>> require_once 'relative.path.php';
>>>>>> require_once $relativePath . 'global.inc';
>>>>>> ?>
>>>>>> put that in logger.inc and foo.php and nothing blows up. here's the code for
>>>>>> relative.path.php:
>>>>>>
>>>>>> $parsedUri = dirname($_SERVER['PHP_SELF']);
>>>>>> $parsedUri .= substr($parsedUri, -1) != '/' ? '/' : '';
>>>>>> $relativeUri = str_replace('/', '', $parsedUri);
>>>>>> $relativePath = strlen($parsedUri) - strlen($relativeUri) - 1;
>>>>>> if ($relativePath < 0){ $relativePath = 0; }
>>>>>> $relativePath = str_repeat('../', $relativePath);
>>>>>> if (!$relativePath){ $relativePath = './'; }
>>>>>> ?>
>>>>>> hth.
>>>> Royan,
>>>> BKdotcom has the right answer. Use $_SERVER['DOCUMENT_ROOT'] to get to
>>>> the root directory of your site, then refer to everything form there.
>>>> It works on all sites, and requires no modification the php.ini file.
>>>> And the beauty of it is, if you have a different location for the root
>>>> directory on your development site, the code still works in both places.
>>>> --
>>>> ==================
>>>> Remove the "x" from my email address
>>>> Jerry Stuckle
>>>> JDS Computer Training Corp.
>>>> jstuck...@attglobal.net
>>>> ==================
>>> FYI, $_SERVER['DOCUMENT_ROOT'] does NOT work on IIS, at least not with
>>> PHP 5.
>> It works just fine. I've got it going on my system here and on a
>> Windows IIS VPS.
>>
>> You just have to have PHP installed as an ISAPI app - not a CGI.
>>
>> BTW - the same it true under Apache, IIRC.
>
>
> Unfortunately our server is hosted, so I can't fiddle the install.
> Also, for a newbie like myself, that would be a fairly arcane bit of
> knowledge about server install/configuration.
>
Then get a new host. Anyone who runs PHP as a CGI isn't worth the cost
of hosting.
> I've been using 'require_once(dirname(__FILE__)."/../
> someaught.php")'...likely there's some hideous downside to doing it
> that way, but it certainly has saved me some headaches trying to
> resolve dependency calls when a file includes a file that includes a
> file...I tried using Maarten Manders' smartloader class with it's
> __autoload trick, but couldn't get it to work. :P
>
> Tyler
>
Other than the fact you are now path-dependent? That is, you can't move
the file to another directory without having to modify your code?
And I don't use __autoload. It can have significant overhead.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: Relative paths in require_once problem (possibly all include routines)
am 03.01.2008 05:53:56 von Michael Fesser
..oO(Jerry Stuckle)
>Logos wrote:
>>
>> Unfortunately our server is hosted, so I can't fiddle the install.
>> Also, for a newbie like myself, that would be a fairly arcane bit of
>> knowledge about server install/configuration.
>>
>
>Then get a new host. Anyone who runs PHP as a CGI isn't worth the cost
>of hosting.
There are reasons for running PHP as CGI/FastCGI, especially on a shared
server, where security should be more important than performance. And on
an Apache the $_SERVER['DOCUMENT_ROOT'] is available even with PHP/CGI.
Micha
Re: Relative paths in require_once problem (possibly all includeroutines)
am 03.01.2008 05:59:14 von Jerry Stuckle
Michael Fesser wrote:
> .oO(Jerry Stuckle)
>
>> Logos wrote:
>>> Unfortunately our server is hosted, so I can't fiddle the install.
>>> Also, for a newbie like myself, that would be a fairly arcane bit of
>>> knowledge about server install/configuration.
>>>
>> Then get a new host. Anyone who runs PHP as a CGI isn't worth the cost
>> of hosting.
>
> There are reasons for running PHP as CGI/FastCGI, especially on a shared
> server, where security should be more important than performance. And on
> an Apache the $_SERVER['DOCUMENT_ROOT'] is available even with PHP/CGI.
>
> Micha
>
Micha,
With correct configuration of the virtual host, PHP is secure, even as a
module.
But some hosts just don't know how to configure it properly.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: Relative paths in require_once problem (possibly all include routines)
am 03.01.2008 06:19:12 von Michael Fesser
..oO(Jerry Stuckle)
>Michael Fesser wrote:
>> There are reasons for running PHP as CGI/FastCGI, especially on a shared
>> server, where security should be more important than performance. And on
>> an Apache the $_SERVER['DOCUMENT_ROOT'] is available even with PHP/CGI.
>
>With correct configuration of the virtual host, PHP is secure, even as a
>module.
>
>But some hosts just don't know how to configure it properly.
I was not referring to safe_mode, open_basedir or similar restrictions
(an enabled safe_mode for example would be a reason for _me_ to change
host), but to the execution of scripts under the name and with the
permissions of the script owner.
Micha
Re: Relative paths in require_once problem (possibly all includeroutines)
am 03.01.2008 14:01:58 von Jerry Stuckle
Michael Fesser wrote:
> .oO(Jerry Stuckle)
>
>> Michael Fesser wrote:
>>> There are reasons for running PHP as CGI/FastCGI, especially on a shared
>>> server, where security should be more important than performance. And on
>>> an Apache the $_SERVER['DOCUMENT_ROOT'] is available even with PHP/CGI.
>> With correct configuration of the virtual host, PHP is secure, even as a
>> module.
>>
>> But some hosts just don't know how to configure it properly.
>
> I was not referring to safe_mode, open_basedir or similar restrictions
> (an enabled safe_mode for example would be a reason for _me_ to change
> host), but to the execution of scripts under the name and with the
> permissions of the script owner.
>
> Micha
>
I understand. And with appropriate configuration, PHP is quite secure.
But some people don't know how to configure it properly.
And a disabled safe_mode would be reason for _me_ to change hosts.
Someone so lax on security shouldn't be in business.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: Relative paths in require_once problem (possibly all include routines)
am 03.01.2008 18:37:57 von Michael Fesser
..oO(Jerry Stuckle)
>I understand. And with appropriate configuration, PHP is quite secure.
>
>But some people don't know how to configure it properly.
>
>And a disabled safe_mode would be reason for _me_ to change hosts.
>Someone so lax on security shouldn't be in business.
safe_mode will be removed in PHP 6:
| As safe_mode is a name that gives the wrong signals as making PHP
| safe, we all agreed that we should remove this function. It can never
| be made totally safe as there will always be ways to circumvent
| safe_mode through libraries. This kind of functionality also better
| belongs in the web server or other security scheme.
Micha