UnitedForums - UK Web Hosting Forum UnitedHosting Community Hosting Forums
Network and Server StatusCustomer SupportUK Web Hosting
UnitedHostingUnitedHosting Sitemap UK Hosting ForumUK Web HostingWeb Hosting ForumsUK Reseller HostingWeb Host CommunityUK Managed Dedicated ServersHosting Help and SupportUK Domain Name Registration

Go Back   UnitedForums.co.uk > UnitedHosting Community > Website Development & Scripting

Reply
 
Thread Tools Rate Thread Display Modes
Old 17th February 2003, 02:52 AM   #1 (permalink)
dwilson
Registered User
 
Join Date: Nov 2002
Location: Kansas City MO
Posts: 549
Send a message via ICQ to dwilson Send a message via AIM to dwilson Send a message via Yahoo to dwilson
mySQL and Linux newbie

Let's say I had my database function type files (i.e., open, close, connect, etc.) in one directory and I had my config file, from which the functions get their data, in another directory, like the parent directory. How would I go about pointing to the other directory? I forgot my old dos days. (i.e., *./somedirectory, ../somedirectory, /somedirectory, etc.?)
__________________
Derik Wilson
dwilson is offline   Reply With Quote
Old 17th February 2003, 04:05 AM   #2 (permalink)
Euge
Registered User
 
Euge's Avatar
 
Join Date: Feb 2003
Location: Lost in confusion
Posts: 700
Re:mySQL and Linux newbie

There are several ways to do this. I'll list them in my personal preferred order from least to most effective:

1) Relative
2) include_path
3) Absolute

Relative: include by using dot notation (../file.inc). There are several disadvantages to using this notation. First, if you move files around, things will break. Second and most important, if you're like me you'll have nested includes. So a single file might include another file which includes another. The problem here is, referencing them relatively is always with respect to the original calling template. For example, if all my includes are in the lib directory, the example below will not work:
original.php -- include('./lib/include1.inc');
include1.inc -- include('./include2.inc');
because even though include1.inc and include2.inc are in the same directory, original.php is one level up, and the nested reference to ./include2.inc will fail. Confused? Don't worry. I suggest you don't use this notation. Its only good for portable apps, in which case you need to structure it such that your entire app will work with this notation.

include_path: In this case you use the include path configured with php. If you run phpinfo() find the value for the directive include_path. I think for our servers its the default value .:/php/includes:/usr/share/php This means for includes it will look first in the current directory of the executing template (.), the /php/includes directory, then the /usr/share/php directory. Obviously, we don't have access to these directories, but luckily we can modify the directive in our .htaccess file. So for the above example to work, you can specify /home/virtual/siteXX/fst/var/www/html/lib as your include_path and reference like this:
original.php -- include('include1.inc');
include1.inc -- include('include2.inc');
or if you specify /home/virtual/siteXX/fst/var/www/html as your include path:
original.php -- include('lib/include1.inc');
include1.inc -- include('lib/include2.inc')
To change the directive, add the following in your .htaccess file:
Code:
php_value include_path '/home/virtual/siteXX/fst/path/you/can/access'
This is not my preferred method because if I switch hosts, I'm not guaranteed .htaccess permissions, so its not as portable.

Absolute: This is the method I use and is most portable. You begin your include path with "/" and give the absolute path to the file relative to the server. But you don't want to hard code your server path. So do this:
Code:
include($_SERVER["DOCUMENT_ROOT"] . 'path relative to DocRoot');
So for the original example:
original.php -- include($_SERVER["DOCUMENT_ROOT"] . 'lib/include1.inc');
include1.inc -- include($_SERVER["DOCUMENT_ROOT"] . 'lib/include2.inc');

Man am I really verbose. Hope this helps.
Euge is offline   Reply With Quote
Old 17th February 2003, 04:37 AM   #3 (permalink)
dwilson
Registered User
 
Join Date: Nov 2002
Location: Kansas City MO
Posts: 549
Send a message via ICQ to dwilson Send a message via AIM to dwilson Send a message via Yahoo to dwilson
Re:mySQL and Linux newbie

Dang, I forgot about $_server!!! I just used that in a mailing script! I tried the first two and they didn't work but the absolute method will be what I need since later it will be on multiple servers. Thanks!
__________________
Derik Wilson
dwilson is offline   Reply With Quote
Old 17th February 2003, 04:02 PM   #4 (permalink)
dwilson
Registered User
 
Join Date: Nov 2002
Location: Kansas City MO
Posts: 549
Send a message via ICQ to dwilson Send a message via AIM to dwilson Send a message via Yahoo to dwilson
Re:mySQL and Linux newbie

By golly that worked. Thanks man!
__________________
Derik Wilson
dwilson is offline   Reply With Quote
Old 17th February 2003, 04:06 PM   #5 (permalink)
UH-Matt
Administrator
 
UH-Matt's Avatar
 
Join Date: Oct 2002
Location: London, UK
Posts: 8,135
Re:mySQL and Linux newbie

LOL

Everytime you get a problem you sound SHOCKED when it gets solved... Scripting is designed to work, not everything just breaks and stays broken
__________________
.
Matt
UnitedHosting Staff

For official support please use our helpdesk at UnitedSupport.co.uk

UnitedHosting proudly hosting more than 20,000 sites since 1998.
UH-Matt is offline   Reply With Quote
Old 17th February 2003, 04:26 PM   #6 (permalink)
dwilson
Registered User
 
Join Date: Nov 2002
Location: Kansas City MO
Posts: 549
Send a message via ICQ to dwilson Send a message via AIM to dwilson Send a message via Yahoo to dwilson
Re:mySQL and Linux newbie

I just get excited that I can move on.
__________________
Derik Wilson
dwilson is offline   Reply With Quote
Old 17th February 2003, 04:30 PM   #7 (permalink)
dwilson
Registered User
 
Join Date: Nov 2002
Location: Kansas City MO
Posts: 549
Send a message via ICQ to dwilson Send a message via AIM to dwilson Send a message via Yahoo to dwilson
Re:mySQL and Linux newbie

I do have another problem though. I am running out of resources or at least that is the error. I am trying to do my includes in one file and require that file when I need to. I have read that the error that I receive is either due to inappropriate access to a file or resources too ow in the php.ini file. Should I just include the path when needed instead of having a common module for includes? or maybe I can insert some code in the includes.php file to check whether I need certain paths?

Here is the error:

Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 3840 bytes) in /home/virtual/site9/fst/var/www/html/weusa_portal/includes/includes.php on line 2
__________________
Derik Wilson
dwilson is offline   Reply With Quote
Old 17th February 2003, 04:32 PM   #8 (permalink)
UH-Matt
Administrator
 
UH-Matt's Avatar
 
Join Date: Oct 2002
Location: London, UK
Posts: 8,135
Re:mySQL and Linux newbie

This error does not always mean an actual resource problem. I would double check through the script - the server is in no way struggling
__________________
.
Matt
UnitedHosting Staff

For official support please use our helpdesk at UnitedSupport.co.uk

UnitedHosting proudly hosting more than 20,000 sites since 1998.
UH-Matt is offline   Reply With Quote
Old 17th February 2003, 04:39 PM   #9 (permalink)
dwilson
Registered User
 
Join Date: Nov 2002
Location: Kansas City MO
Posts: 549
Send a message via ICQ to dwilson Send a message via AIM to dwilson Send a message via Yahoo to dwilson
Re:mySQL and Linux newbie

Wierd. All I am doing is:

(The calling script)

Code:
require ($_SERVER["DOCUMENT_ROOT"] . '/somedir/someotherdir/includes.php');
(the includes.php)
Code:
include ($_SERVER["DOCUMENT_ROOT"] . '/somedir/someotherdir2/config.php');
include ($_SERVER["DOCUMENT_ROOT"] . '/somedir/someotherdir2/mysql_connect.php');
It worked when I did the includes in the script that used those paths. I would really like to have a global include so that I will have access to everything I need up front. I suppose that this isn't very efficent anyway since this project could grow in size pretty quickly once I get the newbie stuff out of the way.

__________________
Derik Wilson
dwilson is offline   Reply With Quote
Old 17th February 2003, 04:43 PM   #10 (permalink)
UH-Matt
Administrator
 
UH-Matt's Avatar
 
Join Date: Oct 2002
Location: London, UK
Posts: 8,135
Re:mySQL and Linux newbie

Just place a config.php somewhere and include it at the top of your scripts, thats how most PHP scripts do it (i think).. no need for document root etc..
__________________
.
Matt
UnitedHosting Staff

For official support please use our helpdesk at UnitedSupport.co.uk

UnitedHosting proudly hosting more than 20,000 sites since 1998.
UH-Matt is offline   Reply With Quote
Old 17th February 2003, 04:44 PM   #11 (permalink)
dwilson
Registered User
 
Join Date: Nov 2002
Location: Kansas City MO
Posts: 549
Send a message via ICQ to dwilson Send a message via AIM to dwilson Send a message via Yahoo to dwilson
Re:mySQL and Linux newbie

Ok. Thanks. (see I didn't get all excited that time. lol) :-X
__________________
Derik Wilson
dwilson is offline   Reply With Quote
Old 17th February 2003, 04:45 PM   #12 (permalink)
UH-Matt
Administrator
 
UH-Matt's Avatar
 
Join Date: Oct 2002
Location: London, UK
Posts: 8,135
Re:mySQL and Linux newbie

Download a basic PHP/MySQL script and examine it. You can often reuse a lot of perfectly good code.
__________________
.
Matt
UnitedHosting Staff

For official support please use our helpdesk at UnitedSupport.co.uk

UnitedHosting proudly hosting more than 20,000 sites since 1998.
UH-Matt is offline   Reply With Quote
Old 17th February 2003, 04:48 PM   #13 (permalink)
dwilson
Registered User
 
Join Date: Nov 2002
Location: Kansas City MO
Posts: 549
Send a message via ICQ to dwilson Send a message via AIM to dwilson Send a message via Yahoo to dwilson
Re:mySQL and Linux newbie

Yeah but I want to do everything from scratch so that I understand what's going on. Someone could write a script giving away all my site details and I would never know if I didn't know PHP.

I think I found the problem (described above) to be "programmer to keyboard interface." It was really dumb. I had the calling module listed in the include. Oops. I removed it and it works. I will go with your suggestion above (about the config at top of scripts).
__________________
Derik Wilson
dwilson is offline   Reply With Quote
Old 17th February 2003, 04:50 PM   #14 (permalink)
UH-Matt
Administrator
 
UH-Matt's Avatar
 
Join Date: Oct 2002
Location: London, UK
Posts: 8,135
Re:mySQL and Linux newbie

I didnt mean copy a script, I meant to look at its structure and how the author calls / includes various things.

Get an idea of a good "style" of coding PHP then make something original of your own.
__________________
.
Matt
UnitedHosting Staff

For official support please use our helpdesk at UnitedSupport.co.uk

UnitedHosting proudly hosting more than 20,000 sites since 1998.
UH-Matt is offline   Reply With Quote
Old 18th February 2003, 09:12 PM   #15 (permalink)
evo4ever
Registered User
 
Join Date: Jan 2003
Location: Liverpool, UK
Posts: 95
Re:mySQL and Linux newbie

You don't really need to express $_SERVER['DOCUMENT_ROOT']. document root is a predefined var. you can just use $DOCUMENT_ROOT. Just saves some typing.

[edit]
If you dont want to explicitly type the path of the file you want to include. you can do this:

Code:
$dir = $DOCUMENT_ROOT.strrev(strstr(strrev($PHP_SELF),"/"));
and then:

Code:
include($dir."filename.php");
Just make sure its in the same folder as the calling script.
[/edit]
__________________
::Programming Guru::
->Services<-
evo4ever is offline   Reply With Quote
Old 18th February 2003, 11:44 PM   #16 (permalink)
Euge
Registered User
 
Euge's Avatar
 
Join Date: Feb 2003
Location: Lost in confusion
Posts: 700
Re:mySQL and Linux newbie

Since newer versions of php are defaulting register_globals to off, it is safer to explicitly use the super global _SERVER
Euge is offline   Reply With Quote
Old 19th February 2003, 12:45 AM   #17 (permalink)
dwilson
Registered User
 
Join Date: Nov 2002
Location: Kansas City MO
Posts: 549
Send a message via ICQ to dwilson Send a message via AIM to dwilson Send a message via Yahoo to dwilson
Re:mySQL and Linux newbie

Yup that's what i'm doing. If global variables were off would I be able to use &quot;../filname.php&quot; or would it not see that with the globals turned off? I am assuming not,
__________________
Derik Wilson
dwilson is offline   Reply With Quote
Old 19th February 2003, 01:21 AM   #18 (permalink)
Euge
Registered User
 
Euge's Avatar
 
Join Date: Feb 2003
Location: Lost in confusion
Posts: 700
Re:mySQL and Linux newbie

Relative references in your includes would still work.

with register_globals on, php makes assumptions on variables. So it allows you to specify $DOCUMENT_ROOT and it would look for a variable with this name in several namespaces before it assumes it is not set. This is a security flaw.

Newer versions are defaulting register_globals to off, so you have to explicity reference get, post, cookie, server, and environment variables otherwise it will assume the variable hasn't been defined.
Euge is offline   Reply With Quote
Old 20th February 2003, 12:55 AM   #19 (permalink)
evo4ever
Registered User
 
Join Date: Jan 2003
Location: Liverpool, UK
Posts: 95
Re:mySQL and Linux newbie

Which is a shame becuase ive been working with reg globs=on since early php3. I agree with you though euge, using the super globs is safer to say the least. Ill just have to get used to the idea that reg globs will be defaulted to off in the very near future.
__________________
::Programming Guru::
->Services<-
evo4ever is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


All times are GMT. The time now is 09:18 PM.

UK Web Hosting  |  UK Reseller Hosting  |  UK Dedicated Servers UnitedHosting  |  UnitedSupport  |  UnitedForums  |  SEO by vBSEO 3.0.0
Copyright © 1998-2008 United Communications Limited. All Rights Reserved. Registered in England and Wales 3651923 - VAT Reg No. 737662309