Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Including scripts on same domain, but a different machine

Status
Not open for further replies.

Maccaday

Technical User
Dec 9, 2003
71
GB
Hi,

Background: I'm wanting to include a script that will be included into many other scripts (probably most of the scripts that I will write). This path from the root to the script will remain the same, though the script itself may change physical location in a server cluster, and the scripts that include it could be anywhere in the cluster.

My question has a few parts:

1) Assuming a file lies on the same domain (but not necessarily the same machine), is there much of a performance penalty in including a php script by using the full path (including rather than just the relative location? (I'd include files with an extenstion other than .php so that they would definitely not be parsed.)

2) (This is perhaps more of a DNS question.)

How are local domain names resolved with PHP? Specifically, if your server domain is and your current machine holds some sub-directories e.g. does PHP think that your current machine is the whole of even if it only holds those sub-directories?

Does this have to do with server settings in the configuration files PHP, of the web server that PHP is running on, the DNS records on the local machine, or purely the DNS server that the machine connects to on the local network?

3) Is the best way to include this file something like:

<?php
...
$root = "_SERVER[SERVER_NAME]"."/path/to/script";
include "$root";
...
?>

or can anyone think of a better way?

Thanks in advance for any responses.
 
if you want to include from another server, think about the following:

if you upload a php file on another server and run an include, the included file will be the output of the php script, not the code itself!

this is due to the fact that the webserver will execute the code and display the html to the user. instead of displaying to the user, in this case, it will be included.

I dont know if this is what you want to do.
If you wish to include code, you need to do something to make the webserver not execute the code, rename file to .inc can be one way.

ps. by renaming the file to .inc, anyone can read the file and find security issues or just steal your entire code, if they wish.

when including, you have options of relative or full path.
full path will be like: include("
while a relative include, can be:
include("path/to/file.ext"); where as the including script is in the docroot in this example!

if you wish to include from docroot, when your file is not in docroot:
include("/path/to/file.ext");
this will work even if the file including is in doc_root/foo/bar/wtf/omg

I dont know about dns issues, but did you try looking in the php manual for this issue?

I would also recommend googling.

Olav Alexander Mjelde
Admin & Webmaster
 
Olav,

Thanks for your response.

I did actually think about both the first and the second issues that you mentioned. I actually hinted at the not including the output when I said that I would change the extension to something other than .php, so this could be .inc as you mention, or something else, but it wasn't necessarily that clear - sorry.

As for others being able to access files, I do actually want people to be able to access the includes, since it is an open-source project, and I would be promoting the fact that people could access them. However, it should be possible to set up the security features such that the files are only accessible to one IP address or user anyway, so that if I wanted to hide them, I could. This could be done either through a web server, where the include script would need to include an authentication step.

I'm afraid that your comments about relative paths do not match what I've experienced. Before posting this question, I tested whether relative paths are from the root directory or from the current directory of the script, and it is the latter. If I have a deep directory structure, therefore, I would either need to work out how many levels to go up, before coming down, or I would need to go straight to the document root. As far as I can see, the only way to do this is to use the domain name, and then the path relative to the root.

I suspect that if I have the individual servers accessing a DNS server that I set up, with the records for my site(s) on it, then when the servers try to find out where ' is, they will find it to be the main server address. Since I will be having replicated sites, all in clusters, this may need some fiddling.

I did have a look in the PHP manual, but I will do again more thoroughly. And online as well.

I think that it should be a DNS issue, in which case I should be able to set it up just fine. I would imagine it to be a bug if PHP internally handled DNS issues, and considering the length of time that PHP has been around, it would have been checked by now, however I will have a better look online.

In any case, I shall be caching scripts, so speed issues shouldn't be too much of an issue, but considering the complexity of the site that I'm setting up, I want to make sure I'm not sending requests to the wrong computers.

Thanks again for your responses.

Cheers.
 
hmm, are you sure you wrote correct when testing includes?

if file1.php is located at: and file2.php is located at:
I would think that if you wish to include file2.php in file1.php, you could either do:

include("or: include("/file/2/file2.php");

I must say I havent tested this in a situation where the files where on different servers and share one domain.

I remember I had to scratch my head when including files from a subdomain, even if the subdomain was on the same server, inside the doc_root folder.

Olav Alexander Mjelde
Admin & Webmaster
 
Olav,

Yes I'm sure.

The two includes for your example would be either:

include "
(note that there are no brackets, since include is a keyword, not a function)

or:

include "../2/file2.php"

This second one uses the relative file notation of "../" to go up one level. If you needed to go up two levels, you would write: "../../", and so on.

This is why if you have scripts of an arbitrary depth in a directory structure, then you would need to add n copies of "../" in order to get back to the root directory, where n is the number of subdirectories that you need to go down to hit the file.

Your example of 'include "/file/2/file2.php"' would take you to ' which is not what you want at all.

A simple test can be done of the above if you're not convinced, but this is true if the files are on the same server. If they are on different servers, then I suspect that what would happen would be that if you needed to go up more levels than the root directory on the server that the file was on, then PHP would carry on up the directory structure on that computer. Hopefully, PHP wouldn't have permissions to see the files outside the root folder, and so an error would be given.

If you think about it, for most situations, it makes more sense to make relative paths relative to the file rather than the root directory, since a folder and a and its subfolders can be moved, or a superfolder can be renamed, and the scripts are unaffected. If the paths were always relative to the root directory, then these paths would need to be renamed all the time if the scripts moved.

My point in my original post about going up to the root directory was basically, is there a quicker way than either using the domain name or working out how many '/'s there are in the path, and adding that number of '../'s to the relative path? This second method would require additional processing, and may have the problem of going to the wrong folder (described above), and I would rather avoid that if possible, so I think the best thing is just to use the full domain.

I think that the way that I will probably do it is to use the full path, and not worry about the additional processing time that will be needed to get the IP of the computer that contains the file.

Cheers.
 
hmm..
I'm sure that if I do:
/file/2/file.php

then it will be from docroot!

if I do:
file/2/file.php

then it will be from current folder

I know how ../ works, but I dont use this, unless it's in the folder outside the current.

I dont have "time" to test it now, as I have to test a cargame :p

Olav Alexander Mjelde
Admin & Webmaster
 
I think I haven't been totally clear, sorry.

The file that contains the relative link could be anywhere, and the file that contains the file that is linked to could be anywhere, also. This file could be in a sub-folder of the current folder, or not. If it is not, then I would need to use ../ as necessary to go up the necessary number of folders in order to come back down again. The easiest way to do this generally would be to go up to the site root (not the document root), and then back down the folders again.

Relative links are from the document root, as you say, which means the current document. This is only the same as the site root, if the document itself is directly in the site root. If it is not, then you need to use ../ to go up levels to reach the site root.

This means that if you have two folders:

/file/1/ and /file/2/

Then to go up to the site root from either of these, you could need to append ../../ to the relative paths, before adding the path from the site root.

If you have a file in one of these folders, and it includes a file in the other one, then you could either do a relative link as:

../1/ or ../2/ (depending on which folder you are going from and to)

or

../../file/1/ or ../../file/2/

In order to generalise this over a site with many folders and sub-folders, to be able to go from one arbitrary folder to another, you would need to add ../ enough times to go up to the site root, and then append the path from the site root.

As I have mentioned previously, I think that the safest way of including files that might be on other folders is to include the full URL, and leave the locating up to DNS servers and traffic managers.

Cheers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top