473,795 Members | 2,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

weird include() problem - HELP my library is #?@*ed!!

Hey,

I've written a custom HTML library using many PHP scripts as seperate
files (just like I'd do a java project) and I'm having some problems
where I'm including scripts in different directories from already
included scripts... basically where there'a an include chain spanning
multiple directories.

I've reduced the problem to its core:

Imagine you have a website consisting of 4 PHP files:

/top_site.php
/inlcudes/top_lib.php
/includes/lib/lib1/lib1.php
/includes/lib/lib2/lib2.php

the contents of which are:

top_site.php = <? include_once("i ncludes/top_lib.php"); ?>
top_lib.php = <? include_once("l ib/lib1/lib1.php"); ?>
lib1.php = <? include_once(". ./lib2/lib2.php"); ?>
lib2.php = <? echo "Hello World!"; ?>

now when I go to http://YOUR_WEBSITE_ROOT/top_site.php I get an error
saying that the include within lib1.php can't find lib2.php...

....BUT if I go directly to
http://YOUR_WEBSITE_ROOT/includes/lib1/lib1.php then you'll see that
it includes lib2.php fine!!!

Try it for yourself; it won't take a minute to set up.

I've checked my include path and it looks fine (having '.' in it), so
I'm basically stuck there. Oh, and I'm running PHP version 4.2.2 on
Linux RH9/Apache 2.

Has anyone else seen this before? Have any ideas? Cos I don't really
want to have to convert everything over to absolute (not sure if
this'll work either) and I've written LOTS of code! D'OH!

Thanks,
Rob Long.
Jul 17 '05 #1
6 2726

"Rob Long" <bo******@gmx.n et> wrote in message news:95******** *************** **@posting.goog le.com...
Hey,

I've written a custom HTML library using many PHP scripts as seperate
files (just like I'd do a java project) and I'm having some problems
where I'm including scripts in different directories from already
included scripts... basically where there'a an include chain spanning
multiple directories.

I've reduced the problem to its core:

Imagine you have a website consisting of 4 PHP files:

/top_site.php
/inlcudes/top_lib.php
/includes/lib/lib1/lib1.php
/includes/lib/lib2/lib2.php

the contents of which are:

top_site.php = <? include_once("i ncludes/top_lib.php"); ?>
top_lib.php = <? include_once("l ib/lib1/lib1.php"); ?>
lib1.php = <? include_once(". ./lib2/lib2.php"); ?>
lib2.php = <? echo "Hello World!"; ?>

now when I go to http://YOUR_WEBSITE_ROOT/top_site.php I get an error
saying that the include within lib1.php can't find lib2.php...

...BUT if I go directly to
http://YOUR_WEBSITE_ROOT/includes/lib1/lib1.php then you'll see that
it includes lib2.php fine!!!

Try it for yourself; it won't take a minute to set up.

Hi Rob,

The includes are relative to the web address - not relative to the code.

So http://YOUR_WEBSITE_ROOT/top_site.php is looking for /webroot/../lib2/lib2.php - which doesn't exist

and http://YOUR_WEBSITE_ROOT/includes/lib1/lib1.php is looking for
/webroot/includes/lib1/../lib2/lib2.php - which does exist.
What you want is something like:

require_once ($_SERVER['DOCUMENT_ROOT']."/includes/lib2/lib2.php");
The way I use the includes is like this:

Imagine your directory as follows

/index.php
/main.php
/inc/menu.php
/content1/index.php
/content1/main.php
/content2/index.php
/content2/main.php

File /index.php:
require_once ($_SERVER['DOCUMENT_ROOT']."/inc/menu.php");
require_once ("main.php") ;

File /inc/menu.php
Option 1 Option 2 Option 3<br>

Files /content1/index.php and /content2/index.php:
require_once ($_SERVER['DOCUMENT_ROOT']."/index.php");

/main.php
Welcome to LocalHost!

/content1/main.php
Welcome to Content 1!

/content2/main.php
Welcome to Content 2!
What does all of this give you?

http://localhost/
Option 1 Option 2 Option 3
Welcome to LocalHost!

http://localhost/content1/
Option 1 Option 2 Option 3
Welcome to Content 1!

http://localhost/content2/
Option 1 Option 2 Option 3
Welcome to Content 2!
Now updating Menu updates the menu for all pages on the site. Same could be done for headers, footers, etc.
-CF
Jul 17 '05 #2
Rob Long wrote:
Cos I don't really want to have to convert everything over to absolute


Try

<?php
set_include_pat h(get_include_p ath() .
':/:/includes:/includes/lib/lib1:/includes/lib/lib2');
?>

to have php use all those directories as the source for the include.
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #3
"ChronoFish " <de**@chronofis h.com> wrote in message news:<kwZKb.248 50$ti2.13240@la keread03>...
"Rob Long" <bo******@gmx.n et> wrote in message news:95******** *************** **@posting.goog le.com...
What you want is something like:

require_once ($_SERVER['DOCUMENT_ROOT']."/includes/lib2/lib2.php");


Thanks a lot mate. Shame that the PHP parser isn't smart enough to
have some kind of include counter, writing all the includes to the
output script at the end, therefore allowing include paths to be
relative from the SCRIPT IN WHICH THEY ARE DECLARED! Which makes a lot
more sense to me. You wouldn't have any problems taking a library as a
package and moving it from project to project etc.

Sigh.

Rob.
Jul 17 '05 #4
Rob Long wrote:
Thanks a lot mate. Shame that the PHP parser isn't smart enough to
have some kind of include counter, writing all the includes to the
output script at the end, therefore allowing include paths to be
relative from the SCRIPT IN WHICH THEY ARE DECLARED! Which makes a lot
more sense to me. You wouldn't have any problems taking a library as a
package and moving it from project to project etc.


Try this

#v+
<?php
$rel1 = '../../include.php';
$rel2 = 'forward/path/include.php';

include preg_replace('@ ^(.*/)[^/]+$@', '$1', __FILE__) . $rel1;
include preg_replace('@ ^(.*/)[^/]+$@', '$1', __FILE__) . $rel2;
?>
#v-

Happy Coding :)
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #5
I noticed that Message-ID: <kwZKb.24850$ti 2.13240@lakerea d03> from
ChronoFish contained the following:
Try it for yourself; it won't take a minute to set up.

Hi Rob,

The includes are relative to the web address - not relative to the code.


How come I have a file in a subdirectory which has

include '1.php';

which includes file in that subdirectory just fine.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #6

"Geoff Berrow" <bl******@ckdog .co.uk> wrote in message news:sv******** *************** *********@4ax.c om...
I noticed that Message-ID: <kwZKb.24850$ti 2.13240@lakerea d03> from
ChronoFish contained the following:

The includes are relative to the web address - not relative to the code.


How come I have a file in a subdirectory which has

include '1.php';

which includes file in that subdirectory just fine.


Don't know. If your search path includes the subdirectory, or if the subdirectory is the directory you're looking at it would work.
Otherwise it shouldn't.

-CF
Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2463
by: Kupo | last post by:
Hi, I'm currently writing website using php. My problem is, when I do: include("../../library/file.php"); // this is from e.g /level1/level2/something.php it works. However, when I use: include("../../../library/file.php"); // and this one from /level1/level2/level3/something.php
9
13117
by: bill | last post by:
Forget the exact definition of difference between, #include <foo.h> and #include "bar.h" Normally foo.h is a standard header file, so it's path is not defined in compiler option, but I am curious how compiler find it.
1
2406
by: Jim Bancroft | last post by:
Hi, I've used VC++ 6 for my previous C++ work and am having a little trouble acclimating to VS .Net 2003, mainly in establishing where to look for additional header and library files. I'm creating a console app and need to include a few custom headers, as well as a library file, from my hard drive. I've used .Net 2003 for C# projects often enough, but never C++. Should I go to Project--->Properties, choose the Linker folder, then
3
2083
by: jchimanzi | last post by:
I am trying to develop a small program which does library books. I have tried the code below but it still does not work. Can someone help me to debug the program and advice accordingly. ii) // This program get or request the customer to enter their details and or details of the book. # include <iostream.h> # include “Library.h” int Main () { Void display Customer details (Char*Char, String); Void display books details (Char*Char,...
4
5616
by: jchimanzi | last post by:
Please can someone help in correcting the following attempt of the questions below. I have included my attempt at the problem I have been asked to write class definations which does the following among other things in C++. a) Library needs to able to get the following information about its customers: firstname, last name, address and telephone numbers. b) the library needs to be able to add customers and books to its records. Assume...
5
335
by: thegreatgonzo | last post by:
I have a little problem. Read this please: //library.h class myClass { public: myClass(); static myClass *instance(); private: static myClass *self;
1
1672
by: £ukasz Z±bik | last post by:
I have project that uses managed c++ where I use c - library, this library contains some variables named generic, during compilation I get error: Error 1 error C2146: syntax error : missing ';' before identifier 'generic', Problem is that in dotNet generic is a keyword, and I cannot change this name becouse I have only lib file and includes. How to solve this problem? -- pozdrawiam £ukasz
4
9018
by: Travis | last post by:
I'm not a Makefile expert. I have a real small program (just a main.cpp) that needs to tie into an existing library. Now other programs I've seen that use the library just do #include "Library.h" but and that works. Of course that doesn't work for me because when I do g++ main.cpp -o prog, it has no idea what to do with Library.h. I take a look at the other people's make files and it's absolute voodoo to me. So is there an easy way to...
2
1212
by: dgpnich | last post by:
I am using msaccess with db2 but this seems to apply to any odbc linked table. I do not want the libary to be included in the link name example MISCMFIL_NAMMSP where MISCMFIL is the libray and NAMMSP is the actual table. When I link the table by default it returns MISCMFIL_NAMMSP I can manually rename the link but with the number of tables and links I need to process I would prefer that the library not be included at the creation of the...
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10438
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.