FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Seeking help with relative and absolute paths
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Seeking help with relative and absolute paths [message #180817] Wed, 20 March 2013 04:45 Go to next message
The Frog is currently offline  The Frog
Messages: 6
Registered: March 2013
Karma: 0
Junior Member
Hi Everyone,

Just playing with some PHP and getting my head around some of the 'gotchas' that this language and its use contain. I am hoping that there is a simple answer to this issue I am facing...so here goes:

I am designing a site (shock and horror) where I have the different elements of the site broken into different directories under the root of the site. On my development machine this is var/www/sitename as the site root. In the site root I have an index.php file that works quite happily.

I have a directory under the site root called config, and another called scripts. In the config directory I have some basic stuff like constants that I define for the site (eg/ db.php for the database constants such as username, server, etc...). Referencing this with the following php from index.php works fine:
<?PHP require_once 'config/db.php';
require_once 'config/config.php';
//do stuff here
?>

Where I am having a problem is when I try to reference the files from a script that is in the scripts directory (or any other for that matter). I have tried to set a constant in the config/config.php file that points to the root of the site with the following:
define(APP_ROOT, realpath( dirname( __FILE__ ) ).'/');

This seems to be working for this test site, but I am still concerned that this wont work anywhere but the test site. I am also concerned that there seems to be no immediately obvious way to establish this value once for the site and simply be able to reference it without having to 'require_once' it in every script that might need it (which is most of them). I have tried to set things this way so that all scripts work with the APP_ROOT constant as the basis of referencing each other in a known way (ie/ absolute paths relative to root of the site).

I feel like I am missing something glaringly obvious as this feels to me like a kludge or work-around. I am not used to coding in PHP so I thought I'd ask those who know more than I do. Can anyone clarify for me if I am on the right track or is there a simpler and more reliable (ie/ define once and its done) way of approaching this?

Many thanks in advance

The Frog

Dev Machine:
Ubuntu 10.10
Apache 2.2.14
PHP 5.3
Re: Seeking help with relative and absolute paths [message #180818 is a reply to message #180817] Wed, 20 March 2013 05:45 Go to previous messageGo to next message
J.O. Aho is currently offline  J.O. Aho
Messages: 194
Registered: September 2010
Karma: 0
Senior Member
On 20/03/13 05:45, The Frog wrote:

> I have a directory under the site root called config, and another called scripts. In the config directory I have some basic stuff like constants that I define for the site (eg/ db.php for the database constants such as username, server, etc...). Referencing this with the following php from index.php works fine:
> <?PHP require_once 'config/db.php';
> require_once 'config/config.php';
> //do stuff here
> ?>
>
> Where I am having a problem is when I try to reference the files from a script that is in the scripts directory (or any other for that matter). I have tried to set a constant in the config/config.php file that points to the root of the site with the following:
> define(APP_ROOT, realpath( dirname( __FILE__ ) ).'/');

You have the super global $_SERVER['DOCUMENT_ROOT'], so you can use

require_once $_SERVER['DOCUMENT_ROOT'].'/config/db.php';

ypu can also us the include_path in php.ini and add the path to the
document root in which case you then can use:

require_once 'config/db.php';


--

//Aho
Re: Seeking help with relative and absolute paths [message #180821 is a reply to message #180817] Wed, 20 March 2013 10:46 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 3/20/2013 12:45 AM, The Frog wrote:
> Hi Everyone,
>
> Just playing with some PHP and getting my head around some of the 'gotchas' that this language and its use contain. I am hoping that there is a simple answer to this issue I am facing...so here goes:
>
> I am designing a site (shock and horror) where I have the different elements of the site broken into different directories under the root of the site. On my development machine this is var/www/sitename as the site root. In the site root I have an index.php file that works quite happily.
>
> I have a directory under the site root called config, and another called scripts. In the config directory I have some basic stuff like constants that I define for the site (eg/ db.php for the database constants such as username, server, etc...). Referencing this with the following php from index.php works fine:
> <?PHP require_once 'config/db.php';
> require_once 'config/config.php';
> //do stuff here
> ?>
>
> Where I am having a problem is when I try to reference the files from a script that is in the scripts directory (or any other for that matter). I have tried to set a constant in the config/config.php file that points to the root of the site with the following:
> define(APP_ROOT, realpath( dirname( __FILE__ ) ).'/');
>
> This seems to be working for this test site, but I am still concerned that this wont work anywhere but the test site. I am also concerned that there seems to be no immediately obvious way to establish this value once for the site and simply be able to reference it without having to 'require_once' it in every script that might need it (which is most of them). I have tried to set things this way so that all scripts work with the APP_ROOT constant as the basis of referencing each other in a known way (ie/ absolute paths relative to root of the site).
>
> I feel like I am missing something glaringly obvious as this feels to me like a kludge or work-around. I am not used to coding in PHP so I thought I'd ask those who know more than I do. Can anyone clarify for me if I am on the right track or is there a simpler and more reliable (ie/ define once and its done) way of approaching this?
>
> Many thanks in advance
>
> The Frog
>
> Dev Machine:
> Ubuntu 10.10
> Apache 2.2.14
> PHP 5.3
>

As Aho, said, use the superglobal $_SERVER['DOCUMENT_ROOT']. I don't
like using the include config directive - it's too easy to forget about
it and use the wrong copy of a file of the same name.

Also, I recommend you keep non-web accessible files such as
configuration files out of the web directory structure, such as
$_SERVER['DOCUMENT_ROOT'] . '/../include/config.php'. This just ensures
the file can't be accessed should there be a screwup in the web server
config (which shouldn't happen - but does).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Seeking help with relative and absolute paths [message #180824 is a reply to message #180821] Wed, 20 March 2013 18:10 Go to previous messageGo to next message
J.O. Aho is currently offline  J.O. Aho
Messages: 194
Registered: September 2010
Karma: 0
Senior Member
On 20/03/13 11:46, Jerry Stuckle wrote:
> On 3/20/2013 12:45 AM, The Frog wrote:
>> Hi Everyone,
>>
>> Just playing with some PHP and getting my head around some of the
>> 'gotchas' that this language and its use contain. I am hoping that
>> there is a simple answer to this issue I am facing...so here goes:
>>
>> I am designing a site (shock and horror) where I have the different
>> elements of the site broken into different directories under the root
>> of the site. On my development machine this is var/www/sitename as the
>> site root. In the site root I have an index.php file that works quite
>> happily.
>>
>> I have a directory under the site root called config, and another
>> called scripts. In the config directory I have some basic stuff like
>> constants that I define for the site (eg/ db.php for the database
>> constants such as username, server, etc...). Referencing this with the
>> following php from index.php works fine:
>> <?PHP require_once 'config/db.php';
>> require_once 'config/config.php';
>> //do stuff here
>> ?>
>>
>> Where I am having a problem is when I try to reference the files from
>> a script that is in the scripts directory (or any other for that
>> matter). I have tried to set a constant in the config/config.php file
>> that points to the root of the site with the following:
>> define(APP_ROOT, realpath( dirname( __FILE__ ) ).'/');
>>
>> This seems to be working for this test site, but I am still concerned
>> that this wont work anywhere but the test site. I am also concerned
>> that there seems to be no immediately obvious way to establish this
>> value once for the site and simply be able to reference it without
>> having to 'require_once' it in every script that might need it (which
>> is most of them). I have tried to set things this way so that all
>> scripts work with the APP_ROOT constant as the basis of referencing
>> each other in a known way (ie/ absolute paths relative to root of the
>> site).
>>
>> I feel like I am missing something glaringly obvious as this feels to
>> me like a kludge or work-around. I am not used to coding in PHP so I
>> thought I'd ask those who know more than I do. Can anyone clarify for
>> me if I am on the right track or is there a simpler and more reliable
>> (ie/ define once and its done) way of approaching this?
>>
>> Many thanks in advance
>>
>> The Frog
>>
>> Dev Machine:
>> Ubuntu 10.10
>> Apache 2.2.14
>> PHP 5.3
>>
>
> As Aho, said, use the superglobal $_SERVER['DOCUMENT_ROOT']. I don't
> like using the include config directive - it's too easy to forget about
> it and use the wrong copy of a file of the same name.

Yeah, the config has a lot of dangers, but it may fit some projects
better if they deploy the php.ini file for the site too (depending how
the web server is configured, you may have site specific configs instead
of one global), but that is for the OP to discover which way is best for
him, but I do agree with you with the simplicity of using the super global.


> Also, I recommend you keep non-web accessible files such as
> configuration files out of the web directory structure, such as
> $_SERVER['DOCUMENT_ROOT'] . '/../include/config.php'. This just ensures
> the file can't be accessed should there be a screwup in the web server
> config (which shouldn't happen - but does).

Sadly quite many projects don't follow this rule, I guess they want to
make things to work out of the box for those who has a less good web
host. Keeping files which you don't people to have direct access too are
good to keep out of the document root too.


--

//Aho
Re: Seeking help with relative and absolute paths [message #180859 is a reply to message #180824] Fri, 22 March 2013 04:24 Go to previous messageGo to next message
The Frog is currently offline  The Frog
Messages: 6
Registered: March 2013
Karma: 0
Junior Member
Thankyou very much for the replies. I appreciate the help. I have a follow-up question then regarding constants and setting them. If I define a constant say in the sites landing page (eg/index.php) will that constant then remain available for any scripts on pages that follow without having to 'require' them?

The reason I ask is one of ignorance on my part. Having consulted the php manual on constants I am unable to make a clear determination on their usage and scope. It would seem in my experimentation and learning that a constant does not in fact remain but in fact needs to be re-defined in each unique page change on the users part. While this can be accommodated in the scripts and pages that I write I am wondering if there is a better way? Define once and use many places?

Cheers

The Frog
Re: Seeking help with relative and absolute paths [message #180860 is a reply to message #180859] Fri, 22 March 2013 07:11 Go to previous messageGo to next message
J.O. Aho is currently offline  J.O. Aho
Messages: 194
Registered: September 2010
Karma: 0
Senior Member
On 22/03/13 05:24, The Frog wrote:
> Thankyou very much for the replies. I appreciate the help. I have a follow-up question then regarding constants and setting them. If I define a constant say in the sites landing page (eg/index.php) will that constant then remain available for any scripts on pages that follow without having to 'require' them?

<?php
DEFINE('A', true);
include "fileB.php";
?>

In this case the A would exists in fileB.php, but it's a quite iffy way
to do it, as if you include fileB.php from another script which don't
have the define, then the A will be missing and you will end up with an
error. A constant will only exists after it has been defined and will
not remain in memory when you load a new page.

> The reason I ask is one of ignorance on my part. Having consulted the php manual on constants I am unable to make a clear determination on their usage and scope. It would seem in my experimentation and learning that a constant does not in fact remain but in fact needs to be re-defined in each unique page change on the users part. While this can be accommodated in the scripts and pages that I write I am wondering if there is a better way? Define once and use many places?

A constant will not magically live into another page, if you want a
value to be set on pageA and then be accessible on pageB and pageC then
use $_SESSION[]

see: http://www.php.net/manual/en/intro.session.php


--

//Aho
Re: Seeking help with relative and absolute paths [message #180861 is a reply to message #180860] Fri, 22 March 2013 08:38 Go to previous messageGo to next message
The Frog is currently offline  The Frog
Messages: 6
Registered: March 2013
Karma: 0
Junior Member
Thankyou once again Aho. I was hoping to avoid using sessions as the
basis. I was thinking more on the lines of a global constant that
applies across the entire site. Such as making available the logins
etc for the backend DB. I don't know if this is safe to do in PHP but
I am hoping a more knowledgeable person might.

Cheers

The frog

--
Cheers

The Frog
Re: Seeking help with relative and absolute paths [message #180865 is a reply to message #180861] Fri, 22 March 2013 13:01 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 3/22/2013 4:38 AM, The Frog wrote:
> Thankyou once again Aho. I was hoping to avoid using sessions as the
> basis. I was thinking more on the lines of a global constant that
> applies across the entire site. Such as making available the logins etc
> for the backend DB. I don't know if this is safe to do in PHP but I am
> hoping a more knowledgeable person might.
>
> Cheers
>
> The frog
>

It's done quite often. Create a file such as config.inc.php, load it on
your system (preferably outside your document root, as I indicated
before). Include it where necessary.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Seeking help with relative and absolute paths [message #180866 is a reply to message #180860] Fri, 22 March 2013 13:12 Go to previous messageGo to next message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma: 0
Senior Member
On 3/22/2013 12:11 AM, J.O. Aho wrote:
> On 22/03/13 05:24, The Frog wrote:
>> Thankyou very much for the replies. I appreciate the help. I have a
>> follow-up question then regarding constants and setting them. If I
>> define a constant say in the sites landing page (eg/index.php) will
>> that constant then remain available for any scripts on pages that
>> follow without having to 'require' them?
>
> <?php
> DEFINE('A', true);
> include "fileB.php";
> ?>
>
> In this case the A would exists in fileB.php, but it's a quite iffy way
> to do it, as if you include fileB.php from another script which don't
> have the define, then the A will be missing and you will end up with an
> error. A constant will only exists after it has been defined and will
> not remain in memory when you load a new page.
>
>> The reason I ask is one of ignorance on my part. Having consulted the
>> php manual on constants I am unable to make a clear determination on
>> their usage and scope. It would seem in my experimentation and
>> learning that a constant does not in fact remain but in fact needs to
>> be re-defined in each unique page change on the users part. While this
>> can be accommodated in the scripts and pages that I write I am
>> wondering if there is a better way? Define once and use many places?
>
> A constant will not magically live into another page, if you want a
> value to be set on pageA and then be accessible on pageB and pageC then
> use $_SESSION[]
>
> see: http://www.php.net/manual/en/intro.session.php
>
>

I think you are true in the aspect of magically appear but if you code
it properly, the CONSTANT will be available for any script/page you need it.

For example:

If you create your config file and put your constants in it, any script
that includes that config file will have that constant visible which at
this point is not much functionally difference then setting data for
each page, but this provides a single location for setting site wide data.

And if you sink your config below the root as discussed earlier then
that data will also be relatively protected.

I use the config and defines for setting SQL criteria, typing friendly
root paths and server side peculiarities.

Then when you move from your Dev site to the Prod site, you only need to
change one file.

You can have separate configs for public and admin as well.

Hope that makes sense.
Re: Seeking help with relative and absolute paths [message #180867 is a reply to message #180861] Fri, 22 March 2013 13:33 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 22.03.2013 09:38, schrieb The Frog:
> Thankyou once again Aho. I was hoping to avoid using sessions as the basis. I was
> thinking more on the lines of a global constant that applies across the entire site.
> Such as making available the logins etc for the backend DB. I don't know if this is
> safe to do in PHP but I am hoping a more knowledgeable person might.
>

Like JS said. You normally have a config file that is included in every script. It
should be outside of document root, or in a subdirectory where the web server has no
access (security).

But of course you have session variables as well, but for a different goal: to record
the data of a particular user session.

/Str.
Re: Seeking help with relative and absolute paths [message #180872 is a reply to message #180866] Sat, 23 March 2013 12:57 Go to previous messageGo to next message
The Frog is currently offline  The Frog
Messages: 6
Registered: March 2013
Karma: 0
Junior Member
Thankyou all again for your replies. I understand now what was being
explained to me. I had my mindset in a different paradigm. Bit of
mental change going from apps to web. Thankyou for your patience with
my enquiries. Your help is very much appreciated.

--
Cheers

The Frog
Re: Seeking help with relative and absolute paths [message #180873 is a reply to message #180872] Sat, 23 March 2013 14:25 Go to previous messageGo to next message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma: 0
Senior Member
On 3/23/2013 5:57 AM, The Frog wrote:
> Thankyou all again for your replies. I understand now what was being
> explained to me. I had my mindset in a different paradigm. Bit of mental
> change going from apps to web. Thankyou for your patience with my
> enquiries. Your help is very much appreciated.
>

Well, the truth is, at time the OP needs to have patience with this group.

So much time is spent to 'one-up' the next person and show how superior
one idea is over the other, you often have to wade thru many posts that
tangent out everywhere and risk leaving more frustrated then when you
started.

Good Luck in progressing forward and don't be a stranger.

Scotty
Re: Seeking help with relative and absolute paths [message #180874 is a reply to message #180873] Sat, 23 March 2013 15:24 Go to previous messageGo to next message
J.O. Aho is currently offline  J.O. Aho
Messages: 194
Registered: September 2010
Karma: 0
Senior Member
On 23/03/13 15:25, Scott Johnson wrote:
> On 3/23/2013 5:57 AM, The Frog wrote:
>> Thankyou all again for your replies. I understand now what was being
>> explained to me. I had my mindset in a different paradigm. Bit of mental
>> change going from apps to web. Thankyou for your patience with my
>> enquiries. Your help is very much appreciated.
>>
>
> Well, the truth is, at time the OP needs to have patience with this group.
>
> So much time is spent to 'one-up' the next person and show how superior
> one idea is over the other, you often have to wade thru many posts that
> tangent out everywhere and risk leaving more frustrated then when you
> started.

I have to disagree, questions like this one has been asked quite many
times and using googles services would have found the answer.

The only time when your argument IMHO is valid, is when someone asks a
question never asked before, but that happens quite seldom nowadays.


--

//Aho
Re: Seeking help with relative and absolute paths [message #180875 is a reply to message #180874] Sun, 24 March 2013 04:09 Go to previous messageGo to next message
The Frog is currently offline  The Frog
Messages: 6
Registered: March 2013
Karma: 0
Junior Member
FWIW I have looked through the posts in this forum, as well as of
course Google searched my topic. There is certainly plenty of
'information' available, but there is little context or verification
to most of it. When learning a new topic I find it both prudent and
faster to ask those 'in the know' on something specific to what I am
working on. That us what I have done here and I suspect the learning
process us similar for many people. Call it interactive learning and
I for one am very grateful for your help and support.

--
Cheers

The Frog
Re: Seeking help with relative and absolute paths [message #180881 is a reply to message #180874] Sun, 24 March 2013 14:27 Go to previous messageGo to next message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma: 0
Senior Member
On 3/23/2013 8:24 AM, J.O. Aho wrote:
> On 23/03/13 15:25, Scott Johnson wrote:
>> On 3/23/2013 5:57 AM, The Frog wrote:
>>> Thankyou all again for your replies. I understand now what was being
>>> explained to me. I had my mindset in a different paradigm. Bit of mental
>>> change going from apps to web. Thankyou for your patience with my
>>> enquiries. Your help is very much appreciated.
>>>
>>
>> Well, the truth is, at time the OP needs to have patience with this
>> group.
>>
>> So much time is spent to 'one-up' the next person and show how superior
>> one idea is over the other, you often have to wade thru many posts that
>> tangent out everywhere and risk leaving more frustrated then when you
>> started.
>
> I have to disagree, questions like this one has been asked quite many
> times and using googles services would have found the answer.
>
> The only time when your argument IMHO is valid, is when someone asks a
> question never asked before, but that happens quite seldom nowadays.
>
>

I do agree that google or bing is the first line of defense. But as I
have personally found, sometimes you don't know what you don't know and
not quite sure how to word it.

This is where the NG is very valuable and the conversations heated or
otherwise are needed. We all I think look at the question coming from
different experience and read it (or into it) in a different way and try
to answer it from that direction. Sometimes we, the NG not me, nails it
right off. Other times it takes some massaging of the OP to get what he
needs out of him.

And like any social group, the longer it takes, the more chance that it
goes off rails. I actually find it entertaining and truth be told I
actually end up learning bit and pieces. That is why I always keep my
popcorn handy.

Scotty
Re: Seeking help with relative and absolute paths [message #180882 is a reply to message #180881] Sun, 24 March 2013 16:24 Go to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 3/24/2013 10:27 AM, Scott Johnson wrote:
> On 3/23/2013 8:24 AM, J.O. Aho wrote:
>> On 23/03/13 15:25, Scott Johnson wrote:
>>> On 3/23/2013 5:57 AM, The Frog wrote:
>>>> Thankyou all again for your replies. I understand now what was being
>>>> explained to me. I had my mindset in a different paradigm. Bit of
>>>> mental
>>>> change going from apps to web. Thankyou for your patience with my
>>>> enquiries. Your help is very much appreciated.
>>>>
>>>
>>> Well, the truth is, at time the OP needs to have patience with this
>>> group.
>>>
>>> So much time is spent to 'one-up' the next person and show how superior
>>> one idea is over the other, you often have to wade thru many posts that
>>> tangent out everywhere and risk leaving more frustrated then when you
>>> started.
>>
>> I have to disagree, questions like this one has been asked quite many
>> times and using googles services would have found the answer.
>>
>> The only time when your argument IMHO is valid, is when someone asks a
>> question never asked before, but that happens quite seldom nowadays.
>>
>>
>
> I do agree that google or bing is the first line of defense. But as I
> have personally found, sometimes you don't know what you don't know and
> not quite sure how to word it.
>
> This is where the NG is very valuable and the conversations heated or
> otherwise are needed. We all I think look at the question coming from
> different experience and read it (or into it) in a different way and try
> to answer it from that direction. Sometimes we, the NG not me, nails it
> right off. Other times it takes some massaging of the OP to get what he
> needs out of him.
>
> And like any social group, the longer it takes, the more chance that it
> goes off rails. I actually find it entertaining and truth be told I
> actually end up learning bit and pieces. That is why I always keep my
> popcorn handy.
>
> Scotty

Maybe what we need is an FAQ for the group.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Fatal error!
Next Topic: Hot list for BA, QA – Analyst, Java Developer, Business Analyst/ Project Coordinator & SAP Business Object Developer
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 21:21:04 GMT 2024

Total time taken to generate the page: 0.02099 seconds