Welcome to WebmasterWorld Guest from 54.167.76.176

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

stripping out the name of a subdomain from local paths .

     

dmmh

11:24 am on Apr 13, 2006 (gmt 0)

10+ Year Member



i have several functions which use $_SERVER['DOCUMENT_ROOT'] as the basedir, and paths are set relative to the path returned by that variable.
However, using the url not like http://www.example.com/subdomain/ but like [subdomain.example.com...] alters the $_SERVER['DOCUMENT_ROOT'] variable and includes the subdomain name at the end of the variable.
I need a regular expression to strip out any subdomain that may or may not be included

I will never understand regular expressions :/

any help?

coopster

11:38 am on Apr 13, 2006 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I'm slightly confused by the question. You are referring to the document root for locating your subdomain. If you are looking for the host portion of the name, why not use $_SERVER['SERVER_NAME']?

dmmh

12:09 pm on Apr 13, 2006 (gmt 0)

10+ Year Member



you are aware that $_SERVER['DOCUMENT_ROOT'] returns a local path to the www root (normally) of the website right? What good is $_SERVER['SERVER_NAME'] gonna do me then? :)

Including files and opening directories work best when you set path like $dir = $_SERVER['DOCUMENT_ROOT']./folder/ (imo)

but this only works when you dont use subdomains in the url, only normal www.example.com/subdomain naming style URLs
in this case $dir could be: home/user/public_html/folder

if one uses subdomain.example.com instead, the $dir will be
home/user/public_html/subdomain/folder, as the $_SERVER['DOCUMENT_ROOT'] is altered by using the different URL

coopster

3:24 pm on Apr 13, 2006 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member




you are aware that $_SERVER['DOCUMENT_ROOT'] returns a local path to the www root (normally) of the website right? What good is $_SERVER['SERVER_NAME'] gonna do me then?

Because it is you that has a misunderstanding, my friend :)
Are you aware, that this ...


However, using the url not like http://www.example.com/subdomain/ but like [subdomain.example.com...] alters the $_SERVER['DOCUMENT_ROOT'] variable and includes the subdomain name at the end of the variable.

... is not a true statement? Example:

<VirtualHost 192.168.1.1:80> 
ServerName subdomain.example.com
DocumentRoot "/www/example/subdomain"
</VirtualHost>
<VirtualHost 192.168.1.1:80>
ServerName www.example.com
ServerAlias example.com *.example.com
DocumentRoot "/www/example/root"
</VirtualHost>

A request to
http://subdomain.example.com

returns
$_SERVER['DOCUMENT_ROOT']
of
/www/example/subdomain

... but ...

A request to any of the following:

http://example.com

http://www.example.com

http://someothersubdomain.example.com

returns
$_SERVER['DOCUMENT_ROOT']
of
/www/example/root

Thereby the reasonable request for clarification.

Yes, using $_SERVER['DOCUMENT_ROOT'] is ideal for setting paths for includes relative to the DocumentRoot and I use it exclusively. However, how are you going to know that you are in a subdomain versus your 'root' domain?

Possible answer: have a look at the $_SERVER['SERVER_NAME']

I'm guessing your situation is that you have some documents within your 'root' domain folder that you want to include [php.net] when you are active in a subdomain. Is this a true statement?

dmmh

7:59 am on Apr 14, 2006 (gmt 0)

10+ Year Member



I see what you mean.

I need this basically for includes and functions which need access to certain folders on the site.
For example I have a php file which draws a table of smilies one can use. The smilies can be used in the forums, for which Ive set up a subdomain, but also for comments on a couple of other subdomains on my site.

so I really need to simply strip out the subdomain from the local path :)

also:

... is not a true statement? Example:

A request to [subdomain.example.com...]
returns $_SERVER['DOCUMENT_ROOT'] of /www/example/subdomain

isnt that what I said and what prevents this from working right?

... but ...

A request to any of the following:
http://example.com
http://www.example.com
[someothersubdomain.example.com...]
returns $_SERVER['DOCUMENT_ROOT'] of /www/example/root

well, a request to [someothersubdomain.example.com...] will return $_SERVER['DOCUMENT_ROOT'] of /www/example/someothersubdomain, thats for sure

I could hardcode the local web root for the site, but I really dont want this for portability, Im forseeing moving hosts a couple of times.

I just need something that will always return the real web root and since it does not exist, I need a regexp to strip out possible subdomains in that path :)

dmmh

8:28 am on Apr 14, 2006 (gmt 0)

10+ Year Member



I decided to take the easy route and hardcode it into one file, which holds all sorts of stuff which is needed throughout the entire site anyway :D

thx for the help man

coopster

12:29 pm on Apr 14, 2006 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



No problem, but we aren't done yet ;)


I could hardcode the local web root for the site, but I really dont want this for portability, Im forseeing moving hosts a couple of times.

That's where I figured you headed here. I believe what you are really describing then is a common includes directory for use in all your sites. Create yourself a

common
includes directory. Then, in your include_path all you have to do for the sites that use the common includes is append that path. For example, lets say we have three sites setup, first is the 'root' site, and then two subdomains.

www.example.com 
subdomain.example.com
anothersubdomain.example.com

We need includes paths for all these. Some will be specific to each domain, but one will contain all the functions and included files that we want to share across all the domains. So we create a directory structure for our includes:

/www/example/includes/root 
/www/example/includes/subdomain
/www/example/includes/anothersubdomain
/www/example/includes/common

Store all your commonly shared functions (non site-specific) in the common includes directory. Now, in all your sites that require these includes you append the path. To do so in your Apache

httpd.conf
it will look something like this:
<VirtualHost 192.168.1.1:80>  
ServerName subdomain.example.com
DocumentRoot "/www/example/subdomain"
php_value include_path ".:/path/to/PEAR:/www/example/includes/subdomain:/www/example/includes/common"
</VirtualHost>
<VirtualHost 192.168.1.1:80>
ServerName anothersubdomain.example.com
DocumentRoot "/www/example/anothersubdomain"
php_value include_path ".:/path/to/PEAR:/www/example/includes/anothersubdomain:/www/example/includes/common"
</VirtualHost>
<VirtualHost 192.168.1.1:80>
ServerName www.example.com
ServerAlias example.com *.example.com
DocumentRoot "/www/example/root"
php_value include_path ".:/path/to/PEAR:/www/example/includes/root:/www/example/includes/common"
</VirtualHost>

If you needed to do this at runtime using ini_set() [php.net] there is an example in message #17 of Good PHP solutions to small problems [webmasterworld.com].
 

Featured Threads

Hot Threads This Week

Hot Threads This Month