The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

3 Pages V < 1 2 3 >  
Reply to this topicStart new topic
> PHP include function
Christian J
post Mar 26 2007, 09:19 AM
Post #21


.
********

Group: WDG Moderators
Posts: 9,661
Joined: 10-August 06
Member No.: 7



Here's another idea that might be simpler to use, but I haven't tested it much.

In the external script "functions.php":
CODE
<?php
/*
If "PHP $_SERVER['DOCUMENT_ROOT']" is the same as your Apache localhost/default directory (i.e. DocumentRoot in your offline Apache server's httpd.conf file) you should be offline. In this example the web project is stored in
"C:/Documents and Settings/Foo/My documents/PHP directory/Bar", so you want the offline server's "web" root to be "/Bar/":
*/
if($_SERVER['DOCUMENT_ROOT']=='C:/Documents and Settings/Foo/My documents/PHP directory')
{
    $web_root='/Bar/'; // offline server's "web" root
}

else
{
    $web_root='/'; // online web root
}
?>


In files located in the web root directory:
CODE
<?php include 'functions.php';
echo '<img src="'.$web_root.'dog.jpg" alt="">';
?>


In files located in child directories of the web root:
CODE
<?php include '../functions.php';
echo '<img src="'.$web_root.'dog.jpg" alt="">';
?>


This post has been edited by Christian J: Mar 26 2007, 10:42 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Mar 26 2007, 09:29 AM
Post #22


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,733
Joined: 9-August 06
Member No.: 6



So no one is going to explain to me? rolleyes.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Mar 26 2007, 10:40 AM
Post #23


.
********

Group: WDG Moderators
Posts: 9,661
Joined: 10-August 06
Member No.: 7



QUOTE(pandy @ Mar 26 2007, 05:08 AM) *

Now you've lost me. You still have to use different paths depending on where the file is so what's the gain? Why not use a path relative to the including file directly in the inlude()? blink.gif

You mean about my original scheme? The point is that you only define a page's directory once (when you create the PHP inclusion and function call):
CODE
<?php
include '../functions.php';
foo('../'); // should return <img src="../dog.jpg" alt="">
?>

Then if you later want to add say a link around the image you can just change functions.php to

CODE
<?php
function foo($dir_level)
{
    echo '<a href="'.$dir_level.'foo.html"><img src="'.$dir_level.'logo.jpg" alt=""></a>';
}
?>


This might be useful for content that's included on all pages, regardless of the page's directory level. Such content can be headers, logos, menus, STYLE/LINK elements, etc.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Mar 26 2007, 11:54 PM
Post #24


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,733
Joined: 9-August 06
Member No.: 6



Yes, I understand how it works but you still need different paths for pages located at different levels in the file structure. You can't just slop the same "tag" in everywhere as with include virtual. I thought that was the original question - any page on any server. blink.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Mar 27 2007, 04:22 AM
Post #25


.
********

Group: WDG Moderators
Posts: 9,661
Joined: 10-August 06
Member No.: 7



QUOTE(pandy @ Mar 27 2007, 06:54 AM) *

You can't just slop the same "tag" in everywhere as with include virtual. I thought that was the original question - any page on any server. blink.gif

But can you really do that with SSI include virtual? Note that my examples deal with situations where you want to use absolute URLs, but your site's offline version is located in a sub directory like http://localhost/foo while the online version is at the web root, like http://foo.com. So while relative URLs work both offline and online, absolute ones don't work since they'll base the offline URL on http://localhost, not http://localhost/foo.

Or to rephrase: I want several offline "web" roots, one for each online site, but you only get one localhost. sad.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Dr Z
post Mar 28 2007, 07:03 PM
Post #26


Advanced Member
****

Group: Members
Posts: 221
Joined: 23-August 06
Member No.: 11



Folks, finally I got it (I think)! I must have been lost in outer space...

Let's say that I have an include file menu.inc in the root directory:

With SSI, using relative paths (/left.inc) will work from any child or grand child directory.

With PHP, I cannot use relative path, but I have to use what I call "step-back" method:
From Child Directories ==> ../left,inc
From Grandchild Directories ==> ../../left.inc
etc. etc...

I just tried it, it works.

Of course, relative directories are a bit easier to use, since if you have to move a page to a diifferent hierchial level, you do not have to change the reference. But the "step-back" method is not such big task, I can live with it.

Am I correct, or am I missing something?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Mar 28 2007, 08:18 PM
Post #27


.
********

Group: WDG Moderators
Posts: 9,661
Joined: 10-August 06
Member No.: 7



QUOTE(Dr Z @ Mar 29 2007, 02:03 AM) *

With SSI, using relative paths (/left.inc) will work from any child or grand child directory.

You can use it with PHP too:

CODE
include '/left.inc';

Not 100% sure of the terminology, but it seems an URL is called "relative" if it "omits some of the information needed to locate the referenced document": http://htmlhelp.com/faq/html/basics.html#relative-url

Until now I used to think URLs were absolute if they were independent of the HTML page's location in the directory hierarchy, like in "/", and relative if the URL depended on the HTML page's location, like in "../", but that seems to be wrong. unsure.gif

QUOTE
From Child Directories ==> ../left,inc
From Grandchild Directories ==> ../../left.inc
etc. etc...

Those paths are relative to the HTML page's location in the directory hierarchy. The problem occurs when you have content with URL's (images, links etc) in the inclusion file, since that content's URLs will go to different places depending on in which directory the HTML page is located.

QUOTE
if you have to move a page to a diifferent hierchial level, you do not have to change the reference.

Except on your offline server, where "/" resolves to "http://localhost/", which may not be what you want if your site project is in "localhost/foo".
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Mar 28 2007, 10:16 PM
Post #28


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



QUOTE(Christian J @ Mar 29 2007, 10:18 AM) *

QUOTE(Dr Z @ Mar 29 2007, 02:03 AM) *

With SSI, using relative paths (/left.inc) will work from any child or grand child directory.

You can use it with PHP too:

CODE
include '/left.inc';

Not 100% sure of the terminology, but it seems an URL is called "relative" if it "omits some of the information needed to locate the referenced document": http://htmlhelp.com/faq/html/basics.html#relative-url



You're confusing URLs with filenames. Obviously the two use the same basic hierarchical addressing mechanism, but they are not the same. (The PHP manual is curiously reticent about this, and I haven't actually tested, but...) The reference to a file in PHP is (like everything else) something about the file system, and not about the web. In a URL within your website, '/' refers to the root of the document tree; in a filename, '/' refers to the root file directory. It has to be this way, or you couldn't access a file outside the document tree - which is the safest place to keep anything you don't want to make directly public.

"Relative" means just what it says (this is not CSS!): a relative file reference or URL does not begin with a slash, and is relative to the current directory. The problem is remembering quite which is the current directory. In particular, if an include file in /home/brian/scripts itself includes "include '../img/fish.jpg', then this is a relative filename - but when the php interpreter is running, the current directory is that containing the file being served.

So yes, you can only use relative file addressing within an include file if the include file will only be used in a single directory, or unless every directory from which it is used has the same relative directory.

(E.g. you might have a number of directories corresponding to different parts of your web tree, and each one has a subdirectory pics; then an include script for doing something to do with images may refer to 'pics/$thisone' and know it will get the current pics directory.)

QUOTE

Until now I used to think URLs were absolute if they were independent of the HTML page's location in the directory hierarchy, like in "/", and relative if the URL depended on the HTML page's location, like in "../", but that seems to be wrong. unsure.gif


It appears to me to be right.

QUOTE


QUOTE
From Child Directories ==> ../left,inc
From Grandchild Directories ==> ../../left.inc
etc. etc...

Those paths are relative to the HTML page's location in the directory hierarchy. The problem occurs when you have content with URL's (images, links etc) in the inclusion file, since that content's URLs will go to different places depending on in which directory the HTML page is located.

QUOTE
if you have to move a page to a diifferent hierchial level, you do not have to change the reference.

Except on your offline server, where "/" resolves to "http://localhost/", which may not be what you want if your site project is in "localhost/foo".


Surely not. Surely if you have an offline _server_ (Apache, whatever), then it must be set up with a document tree, with the root normally at the top of the copy of the website you're serving?

Surely in "http://localhost", the 'localhost' refers to a web domain, being the local host. It isn't the name of a place in your filesystem. Do you have a directory called '/localhost' (or M$ bastardisation thereof)?

I can see your problem - you use a single server to provide copies of several different websites, so that the document tree is one level higher than you would expect. I would have thought there is a way of configuring the server to serve more than one - of course there is, because that's what happens on a real live shared server like mine.




User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Dr Z
post Mar 29 2007, 12:05 AM
Post #29


Advanced Member
****

Group: Members
Posts: 221
Joined: 23-August 06
Member No.: 11



QUOTE(Christian J @ Mar 27 2007, 01:22 AM) *

[Or to rephrase: I want several offline "web" roots, one for each online site, but you only get one localhost. sad.gif

Why not use vhosts for different sites. The configuration required in the config file are not that complicated, and should you need assistance I will gladly try my best to help. The best part of it is that you can access each vhost by typing its name in the address bar.

The only trick is to add the name of each host in the hosts file, which is not easy to locate. Every time I need to add another host name I have look around to find out where it is. Here is the path (Windows XP):
windows/system32/drivers/etc/hosts

When you open this file, very probably, you will see an entry like:
127.0.0.1 localhost

Let's say you want to add vhosts named abc and xyz you could:
  • Tack on the new vhost names to the same line (seperated by spaces) like:
    127.0.0.1 localhost xyz abc
  • Create a newline for each, like
    127.0.0.1 localhost
    127.0.0.1 xyz
    127.0.0.1 abc
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Dr Z
post Mar 29 2007, 12:20 AM
Post #30


Advanced Member
****

Group: Members
Posts: 221
Joined: 23-August 06
Member No.: 11



In reply to most recent posts by Brian & Christian:

Usising the same emple of file to be included named left.inc which resides in the root directory:

If I try to include the file in a file that resides in a child directory:
/left.inc does not work
However ../left.inc works

That is where the main question lies.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Mar 29 2007, 01:36 AM
Post #31


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



QUOTE(Dr Z @ Mar 29 2007, 02:20 PM) *

In reply to most recent posts by Brian & Christian:

Usising the same emple of file to be included named left.inc which resides in the root directory:


What do you mean by "root directory"? Do you really mean root of the file system? This is what php would mean.

QUOTE


If I try to include the file in a file that resides in a child directory:
/left.inc does not work
However ../left.inc works

That is where the main question lies.


Again, what do you mean by "a child directory"? Every directory except root is a child - the child of its parent, which is not necessarily root.

If you want to include a file that is one level below root (eg /etc/mail.rc), then

include 'etc/mail.rc'; // will work
include '../mail.rc'; // will obviously not work, unless the current directory is a child of /etc (unlikely!)

You can check this here:
http://imaginatorium.org/private/test.php

Look, this stuff is Really Simple. It was thought up a long time ago by some able people. Please take the time to sort it out - which mostly means learning to be careful about terminology.

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Mar 29 2007, 06:28 AM
Post #32


.
********

Group: WDG Moderators
Posts: 9,661
Joined: 10-August 06
Member No.: 7



QUOTE(Brian Chandler @ Mar 29 2007, 05:16 AM) *

"Relative" means just what it says (this is not CSS!): a relative file reference or URL does not begin with a slash, and is relative to the current directory.
QUOTE

Until now I used to think URLs were absolute if they were independent of the HTML page's location in the directory hierarchy, like in "/", and relative if the URL depended on the HTML page's location, like in "../", but that seems to be wrong. unsure.gif

It appears to me to be right.

Yet the FAQ says

"If the relative URL begins with a single slash (e.g., /faq/html/), then it will inherit the base URL's scheme and network location."

and

"the browser resolves relative URLs, not the server. The server sees only the resulting absolute URL."

These examples seem to confirm this:
http://en.wikipedia.org/wiki/Uniform_Resou...f_absolute_URIs
http://en.wikipedia.org/wiki/Uniform_Resou...f_relative_URIs

QUOTE
QUOTE
Except on your offline server, where "/" resolves to "http://localhost/", which may not be what you want if your site project is in "localhost/foo".

Surely not. Surely if you have an offline _server_ (Apache, whatever), then it must be set up with a document tree, with the root normally at the top of the copy of the website you're serving?

Yes, except that you usually have several web site copies you want to test.

QUOTE
Surely in "http://localhost", the 'localhost' refers to a web domain, being the local host. It isn't the name of a place in your filesystem. Do you have a directory called '/localhost' (or M$ bastardisation thereof)?

The file system isn't the issue. You map a file system directory to http://localhost with the DocumentRoot directive in httpd.conf.

QUOTE
I would have thought there is a way of configuring the server to serve more than one - of course there is, because that's what happens on a real live shared server like mine.

The difference is that online servers use different domain names/IP numbers to separate each account. But how do you do that with your offline server with only one localhost?

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Mar 29 2007, 06:39 AM
Post #33


.
********

Group: WDG Moderators
Posts: 9,661
Joined: 10-August 06
Member No.: 7



QUOTE(Dr Z @ Mar 29 2007, 07:05 AM) *

QUOTE(Christian J @ Mar 27 2007, 01:22 AM) *

[Or to rephrase: I want several offline "web" roots, one for each online site, but you only get one localhost. sad.gif

Why not use vhosts for different sites.

This sounds promising. I remember someone suggested it before, but I gave up for reasons I can't remember.

QUOTE
The best part of it is that you can access each vhost by typing its name in the address bar.

How does such an address look? Like "http://abc"?

QUOTE
Let's say you want to add vhosts named abc and xyz you could:
  • Tack on the new vhost names to the same line (seperated by spaces) like:
    127.0.0.1 localhost xyz abc
  • Create a newline for each, like
    127.0.0.1 localhost
    127.0.0.1 xyz
    127.0.0.1 abc

Don't you have to specify each vhost name in httpd.conf as well?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Mar 29 2007, 11:45 AM
Post #34


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



QUOTE(Christian J @ Mar 29 2007, 08:28 PM) *

QUOTE(Brian Chandler @ Mar 29 2007, 05:16 AM) *

"Relative" means just what it says (this is not CSS!): a relative file reference or URL does not begin with a slash, and is relative to the current directory.
QUOTE

Until now I used to think URLs were absolute if they were independent of the HTML page's location in the directory hierarchy, like in "/", and relative if the URL depended on the HTML page's location, like in "../", but that seems to be wrong. unsure.gif

It appears to me to be right.

Yet the FAQ says

"If the relative URL begins with a single slash (e.g., /faq/html/), then it will inherit the base URL's scheme and network location."

and

"the browser resolves relative URLs, not the server. The server sees only the resulting absolute URL."

These examples seem to confirm this:
http://en.wikipedia.org/wiki/Uniform_Resou...f_absolute_URIs
http://en.wikipedia.org/wiki/Uniform_Resou...f_relative_URIs



OK: there's a bit of a grey zone - such a URL is "relative" with respect to domains (/robots.txt is the same whatever domain you're in), but "absolute" with respect to the document tree. Look at how they name the example:

/relative/URI/with/absolute/path/to/resource.txt

So there's room for terminological sloppiness, but nothing changes the obvious interpretation of what the bits mean.

The rest I don't really know about, only ever had one website - perhaps you need this vhosts thing.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Darin McGrew
post Mar 29 2007, 01:11 PM
Post #35


WDG Member
********

Group: Root Admin
Posts: 8,365
Joined: 4-August 06
From: Mountain View, CA
Member No.: 3



FWIW, I've used the terms
absolute - a fully qualified path, e.g.: http://www.example.com/foo.html
relative - a path relative to the current document, e.g.: foo.html or ../bar.html
root relative - a path relative to the server's root, e.g.:
/foo.html or /baz/bar.html

But yeah, there is a fair bit of slop in the terminology.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Dr Z
post Mar 29 2007, 02:48 PM
Post #36


Advanced Member
****

Group: Members
Posts: 221
Joined: 23-August 06
Member No.: 11



If the name of the vhost is "abc", then the address would be "http://abc"

And also, I forgot to mention earlier, you can set up independent cgi-bin for each vhost.

I will try to go step by step using the name xyz for the the vhost to be created:

First step is the directories:
In the apache directory, s subdirectory named vhosts exists. Inside the vhosts driectory create a sub-direcroy named xyz. Inside the xyz directory we need to create two more directories: htdocs and cgi-bin.

Second step would be to enter xyz as a host in the hosts file.

Third step is the config file. However, the place of the lines I will refer to may be different in different versions of Apache.

Make sure that the following lines are in the file, if not add/or modify the lines:
Listen 80
ServerName localhost:80

Vhosts section (section 3) is generally at the very end of the file:
Make sure the following line exists:
NameVirtualHost 127.0.0.1

Now we need to add the following lines for this host, as follows:
<VirtualHost 127.0.0.1>
ServerName xyz
ServerAdmin info@xyz.com
DocumentRoot "C:/Program Files/Apache Group/Apache2/vhosts/xyz/htdocs/"
ScriptAlias /cgi-bin/ "C:/Program Files/Apache Group/Apache2/vhosts/xyz/cgi-bin/"
</VirtualHost>

Please note that you could add two more lines to configure logs. For-off line testing I do not have any need for them, so do not include those lines.

When writing the above I used my config file to make sure that I did not miss anything. However, it does not mean that I did not. If it does not work for any reason, please let me know.


User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Mar 30 2007, 06:51 AM
Post #37


.
********

Group: WDG Moderators
Posts: 9,661
Joined: 10-August 06
Member No.: 7



Finally got it to work (after the usual trial & error), thanks a lot Dr Z! smile.gif

The disadvantage is of course that you must change three things for every new project: the hosts file, the vhosts directory and the httpd.conf file. With my old method I could simply add another project directory in my existing PHP directory, and it would be accessible from localhost right away.


User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Mar 30 2007, 07:08 AM
Post #38


.
********

Group: WDG Moderators
Posts: 9,661
Joined: 10-August 06
Member No.: 7



QUOTE(Dr Z @ Mar 29 2007, 07:20 AM) *

If I try to include the file in a file that resides in a child directory:
/left.inc does not work
However ../left.inc works

That is where the main question lies.

If it's just for the inclusion file path try pandy's solution:

CODE
<?php
include($_SERVER['DOCUMENT_ROOT'].'/menu.inc');
?>

though I don't think you need the slash. Seems you do, if either of the servers' document root is specified without ending slash.

This post has been edited by Christian J: Mar 30 2007, 07:20 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Dr Z
post Apr 1 2007, 11:10 PM
Post #39


Advanced Member
****

Group: Members
Posts: 221
Joined: 23-August 06
Member No.: 11



A very interesting observation paths relative to root (using Darin's definition.

First, I would like to point out that I have Apache 2 running on my PC and in my laptop (more recent) I installed Apache 2.2.

Again, starting with example of the file left.inc, that resides in the root directory, path /left.inc will work any where in the directory tree, but the interesting part starts when you are in the root directory (i.e. both files are in the same directory)!!

Apache 2:
In the root directory path /left.inc will not work, but left.inc will.
Apache 2.2:
In the root directory both paths /left.inc and left.inc will work.

This is not only true for SSI include function but all referenced files, including external Style Sheets.

I need a sanity check on this observation. If anyone of you folks who has either of these versions of Apache installed under windows, please double check me and provide some feed-back. Am I right or am I imagining things?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Apr 2 2007, 07:13 AM
Post #40


.
********

Group: WDG Moderators
Posts: 9,661
Joined: 10-August 06
Member No.: 7



QUOTE(Dr Z @ Apr 2 2007, 06:10 AM) *

the interesting part starts when you are in the root directory (i.e. both files are in the same directory)!!

I assume you mean the web root, AKA DocumentRoot (which is set to "......../htdocs" in my case).

QUOTE
Apache 2:
In the root directory path /left.inc will not work, but left.inc will.

For me it works in Apache 2/Windows. Test code:

CODE
<?php
echo $_SERVER['DOCUMENT_ROOT'].'<br>';
include $_SERVER['DOCUMENT_ROOT'].'/left.inc';
?>


QUOTE
This is not only true for SSI include function but all referenced files, including external Style Sheets.

Could it be that you set your web root higher than than you think in httpd.conf? E.g.

CODE
DocumentRoot "........./Apache2/vhosts/foo"

while your HTML files are in

CODE
........./Apache2/vhosts/foo/htdocs

?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

3 Pages V < 1 2 3 >
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 28th April 2024 - 04:51 AM