LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 08-17-2006, 08:36 AM   #1
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Rep: Reputation: 45
How do I call include statement in php, relative to htdocs root?


Hello, I have been doing some stuff in php and the number of files was starting to get overwhelming. I categorized everything into sub-directories, but now my include statements will not work. If I call an include with include_once("/myinclude.php"), it searches relative to the path that it's being called from. However, when I print $_SERVER['SELF_PHP'] (I think that's the one, I'm going by memory) It will print it like it should "/myinclude.php" (which is in the root directory). Is there something special you have to do to give paths relative to '/'?

I've tried setting an alias in my http.conf, but to no avail. (see original question). I'm really new to php, so sorry if the answer is obvious.

Thanks for the help!
...drkstr


**edit original question bellow**

I'm trying to alias '/' as the document root directory so links do not have to be entered in relative to the linker.

I have this in my httpd.conf:
Code:
<IfModule mod_alias.c>

Alias / "/var/www/htdocs/"

...
But I am still getting "no such file or directory" complaints. If I am in a sub directory, I have to '../link.php' and in the same directory, 'link.php'. But never '/link.php'

Any ideas what I missed?

Thanks!
...drkstr

Last edited by drkstr; 08-17-2006 at 01:14 PM.
 
Old 08-17-2006, 11:22 AM   #2
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Original Poster
Rep: Reputation: 45
This appears to be a problem with php (specificly when using an include statement). I tried linking to my htdocs root in an html and it worked fine.

I should probably try my luck in the proggraming forum.

sorry for the bother!
...drkstr
 
Old 08-18-2006, 02:23 AM   #3
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Original Poster
Rep: Reputation: 45
Well I was able to figure out a real clumsy work around. I wasn't able to find any "real" solution which suprises me. I am new to PHP so I could just have been looking for the wrong thing, but it doesn't look like there is anyway to dynamically declare the root directory.

If anyone else is looking for an answer, here is my work around.

I updated the /etc/apache/php/ini with the following line:
Code:
auto_prepend_file = /var/www/htdocs/global-vars.php
This server will "force" include the file at beginning of every php script. in 'global-vars.php' you just set a global var to "/var/www/htdocs" then all of your includes can be called as so:
Code:
$returnCode = include("$ROOT/folder/script.php")
Or however you wish to "include".

Also, FYI: this only applies to include files, any other kind of linking or url redirection still works by linking relative to the htdocs root.

regards,
...drkstr

**edit**
incase you are asking why I just don't link relative to the directory calling the include ...

The problem you run into is when you have lib files that use an include but might get called from diffrent directories. This is what tipped me off to the problem to begin with.

Last edited by drkstr; 08-18-2006 at 02:26 AM.
 
Old 08-18-2006, 08:57 PM   #4
Daga
Member
 
Registered: Apr 2006
Location: A comfy chair...
Distribution: Slackware
Posts: 111

Rep: Reputation: 15
Yeah, this would probably be better suited in the programming forum... but then I wouldn't have seen it...

Here is a trick that I use:

Code:
<VirtualHost *:80>
    DocumentRoot "/var/www/servername.com"
    # etc.

    php_value open_basedir "/var/www/servername.com"
    php_value include_path ".:/var/www/servername.com/include"
After setting that up in the httpd.conf file, you can use require("header.php") at the top of each document and the interpreter will be able to find the file in the include directory.
 
Old 08-18-2006, 10:29 PM   #5
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Original Poster
Rep: Reputation: 45
Awsome, thanks for the advice! Quick question though...
Quote:
; open_basedir, if set, limits all file operations to the defined directory
; and below. This directive makes most sense if used in a per-directory
; or per-virtualhost web server configuration file. This directive is
; *NOT* affected by whether Safe Mode is turned On or Off.
;open_basedir =
does this mean that my php files would not be able to preform actions on file outside the base directory. FOr instance, open '/etc/some_config_file' for reading? I saw this option when browsing though the php.ini for an answer, but thought it might not work for what the scripts are designed for. PHP is completely new to me, so I'm still trying to figure out all the details.

Thanks Daga!
...drkstr
 
Old 08-18-2006, 10:37 PM   #6
Daga
Member
 
Registered: Apr 2006
Location: A comfy chair...
Distribution: Slackware
Posts: 111

Rep: Reputation: 15
Quote:
Originally Posted by drkstr
does this mean that my php files would not be able to preform actions on file outside the base directory. FOr instance, open '/etc/some_config_file' for reading?
I tend to be paranoid about letting PHP edit files outside of the website's directory.... however, I just realized I have a script on one of my domains that violates that directive and still works. Crud. Now I'm confused
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: First call for votes for the GFDL position statement LXer Syndicated Linux News 0 02-26-2006 03:46 AM
how to include a shell scipt or call it at startup abhis_mail2002 Fedora 6 12-12-2005 11:30 PM
htdocs root permissions? technician Linux - Newbie 6 01-10-2005 02:47 PM
Couldn't open stream {localhost/imap : 143} in /usr/local/httpd/htdocs/view.php nshoboul Linux - Newbie 12 10-24-2003 02:23 PM
need perl fuction like c #include statement for header file mrtwice Programming 5 06-19-2003 02:42 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 11:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration