Wildcard DNS and PHP

I’ve always been wondering how it works, and I’ve finally found out how it does. I will give a few simple examples how it works and how you can use it.

Step one : Make sure your server/domain allows wildcard DNS and that it is enabled on your server/domain.

Step two : Lets create a file called index.php

now before I continue with step 3, it is nice to know that the wildcard subdomain forwards to the index.php on the main domain so all the following domain pont to the same file.

  • foo.domain.com
  • bar.domain.com
  • foobar.domain.com

like said before all these calls will point to domain.com/index.php. We now can use PHP to understand what we typed in,and what we can do with this data.

step three : Now we can enter the code in the index.php that will identify the subdomain we have requested for further processing.

<?php
$serverhost = explode('.',$_SERVER["HTTP_HOST"]);$subdomain = $serverhost[0];
//check if it really is a subdomain

if (isset($serverhost[2]) and ($serverhost[2] != "") ? $sub = $subdomain : $sub = "" );
// now we can use $sub whereever we want.
echo $sub;
?>

Now the variable “$sub” contains the subdomain we have requested, if we directly called the main url the variable will be empty.
i hope this short and simple tutorial was usefull for you!

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • Furl
  • Live
  • Ma.gnolia
  • NewsVine
  • StumbleUpon
  • Technorati
  • TwitThis
  • Chawlk
  • Propeller
  • Ycombinator
Tags: , , ,

4 Responses to “Wildcard DNS and PHP”

  1. Harris %9 Says:

    Thank you for your help!

  2. SplitFive Says:

    Thanks for the process

  3. Mac Einck Says:

    I don’t usually reply to posts but I will in this case. WoW

  4. evileye Says:

    Thanks for the poem. great work!

Leave a Reply