homepage Welcome to WebmasterWorld Guest from 54.161.133.166
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
stripping out the name of a subdomain from local paths .
dmmh




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

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




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

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




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

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




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


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




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

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




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

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




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

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].

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved