PDA

View Full Version : Little fun script...


Jacob Stetser
05-14-1999, 09:14 PM
<?
$headers = getallheaders();
while (list($header, $value) = each($headers)) {
[nbsp][nbsp] if($header=='Host' &amp;&amp; $value=='www.yourdomain.com') {
[nbsp][nbsp][nbsp][nbsp]$filename=&quot;/path/to/index.html&quot;;
[nbsp][nbsp][nbsp][nbsp]$fp=fopen($filename,&quot;r&quot;);
[nbsp][nbsp][nbsp][nbsp]$contents = fread( $fp, filesize( $filename ) );[nbsp][nbsp]
[nbsp][nbsp][nbsp][nbsp]echo[nbsp][nbsp]$contents;
[nbsp][nbsp] } elseif($header=='Host' &amp;&amp; $value=='subdomain.yourdomain.com') {
[nbsp][nbsp][nbsp][nbsp]$filename=&quot;path/to/your/subdomain/index.html&quot;
[nbsp][nbsp][nbsp][nbsp]$fp=fopen($filename,&quot;r&quot;);
[nbsp][nbsp][nbsp][nbsp]$contents = fread( $fp, filesize( $filename ) );[nbsp][nbsp]
[nbsp][nbsp][nbsp][nbsp]echo[nbsp][nbsp]$contents;
[nbsp][nbsp] }
}

?>


What this does is look at the HTTP Request Header Host: and see what domain name people used to access your site. Make this your index page, etc and set everything up correctly, and you'll be able to serve different home pages to people depending on which subdomain they access!

Okay, so maybe it's not as cool as I hoped, and there's no error checking in this snippet.. but if you have www.yourdomain.com and sub.yourdomain.com pointing at the same page, now you can have viewers see different pages without any extra work on Terra's part :)

Any questions, comments, suggestions? This is just a piece of code I threw together as I try to learn more PHP..

If you want to see it at work, try
http://sandbox.icongarden.com/ vs. http://www.icongarden.com/sandbox/. Never mind the 404 page- that's simply the page I offered up for people trying to access the sandbox through the subdirectory method, see?

------------------
http://icongarden.com/?fq[nbsp][nbsp]
icongarden: making good ideas grow.
<!-- NO_AUTO_LINK -->
[This message has been edited by Jacob Stetser (edited 05-14-99)]

Terra
05-14-1999, 09:47 PM
This won't work on our servers as I do not use 'wildcard' addresses in either our Apache or DNS configurations...

TAZ is plagued with Apache wildcards (but *not* DNS) (legacy issues)
SIX uses no wildcards (I saw the light)

There are 2 issues with wildcards:
1) security (DNS side)
2) performance (Apache side)

I solve #1 by just not doing it... ;)
I solve #2 by adding the new 'DomainStack' to the ServerAlias list, and head off the request via a modified IRM...[nbsp][nbsp]Hence the new 'subdomain' identifier DS:IRM...

My DS:IRM is highly optimized and incurs little overhead as it is designed into the core, and not reliant on forking overhead by the above CGI script per each request...

--
Terra
--Works hours and hours and hours just to shave off 1% overhead, cause in the end - it really does add up--
FutureQuest

Terra
05-14-1999, 09:53 PM
The reason the above will work, is I have already configured 'sandbox' as a DS:IRM within your configuration...

DS == DomainStack == DNS Side
IRM == Internal Redirection/Mapping == Apache Side

For others not DS enabled, it will not work as the browser will never find the 'subdom.domain.com' IP address from our DNS server which must be installed manually...

--Terra

Jacob Stetser
05-14-1999, 10:09 PM
Hold on there, pardner...

So if I just got something that was, say, bettyboop.icongarden.com, that did the same thing as www.icongarden.com (http://www.icongarden.com), namely serve the same index.html in the same directory as www.icongarden.com (http://www.icongarden.com), instead of my futzin around with the DS/IRM setup as you mentioned (i.e., serving requests for the DNS host sandbox.icongarden.com from the directory www.icongarden.com/sandbox) (http://www.icongarden.com/sandbox)), that script wouldn't work? (am I divulging too much about my setup? ;) )

In other words, if I simply got, an internal subdomain as opposed to what I did get- an internal subdomain that was also an IRM mapped to a subdirectory, it wouldn't work?

I would think that as long as the browser thinks the host's name is bettyboop.icongarden.com, it will pass that in the Host: header to the server, and thereby be picked up by my script.

---- Oh, duh. I see what you're saying now...

Terra is saying: this script alone will not do anything for you. I'm saying: combined with a subdomain PURCHASED from FQ, it will work :)

You can't just use it to create subdomains, and that's what I think Terra thinks I thought. But I didn't think that, and so I think what he thought is a little confusing.

Oy.


------------------
icongarden.com/?fq (http://icongarden.com/?fq)
icongarden: making good ideas grow.

Jacob Stetser
05-14-1999, 10:15 PM
BTW, Terra is right about overhead, although I think I heard somewhere that PHP is nowhere near as bad at overhead as that PERL language! :)

If you're going to buy a subdomain and want to serve different things from the subdomain, let Terra take care of you :) My code was just a 'will this work?' exercise and although I might find some use for it elsewhere, it's not intended as a replacement for his amazing coding abilities- which I'd trust more than mine anyday!

Jake
------------------
icongarden.com/?fq (http://icongarden.com/?fq)
icongarden: making good ideas grow.

dean
05-15-1999, 01:01 AM
Nice script, I played with it it is also useful for picking which domain name was typed if multiple domains point to the same site. In my case a *.net or *.com. Nice thanks for sharing the info


dean


------------------
Beta News ? http://www.betazine.com

pdstein
05-17-1999, 11:24 AM
I believe you could also accomodate multiple domain names, &quot;www&quot; and non &quot;www&quot; URLs, and subdomains all pointing to the same place using code like this:


$domain = strtolower($HTTP_HOST);
switch ($domain) {
[nbsp][nbsp]case “www.domain1.com”:
[nbsp][nbsp]case “domain1.com”:
[nbsp][nbsp][nbsp][nbsp]include “$homedir/domain1/home.html”;
[nbsp][nbsp][nbsp][nbsp]break;
[nbsp][nbsp]case “www.domain2.com”:
[nbsp][nbsp]case “domain2.com”:
[nbsp][nbsp][nbsp][nbsp]include “$homedir/domain2/home.html”;
[nbsp][nbsp][nbsp][nbsp]break;
[nbsp][nbsp]case “subdomain.domain2.com”:
[nbsp][nbsp][nbsp][nbsp]include “$homedir/domain2/subdomain/home.html”;
[nbsp][nbsp][nbsp][nbsp]break;
}


- Paul