AK Productions Studios  
Home Forum Downloads Submit
Tutorials
Welcome Guest!
Learn how to create a ‘fake’ subdomain using some basic PHP code.
Author: Ashley Stevens
Difficulty: 3/10
 
PHP Subdomains
   

Today we are going to learn how to create a ‘fake’ subdomain using some basic PHP code. We have the option of either masking the URL (uniform resource locator; the address of a website) that the subdomain goes to, or not (masking is not possible if you are making subdomains’ for certain websites, Gmail, for example). Primarily I will be covering the method for masking the URL.

Note: This tutorial assumes you already have a website with an ‘index.php’ page setup. Don’t worry if you don’t, open up Notepad, or your favorite code editor, and save the file with the name ‘index.php’ (make sure type of file is set to ‘any’).

With the help of this tutorial, you will have setup as many subdomains’ as you want within a matter of minutes. Although some PHP knowledge will help, everything is explained throughout and you will pick up some skills along the way. Let’s go!

The Limitations:
This is only a ‘fake’ subdomain system and has a number of limitations. If a visitor views the source code of one of your subdomains’, instead of seeing the source of the page it goes to they will see something like the following:

Followed by the rest of your website’s code…

Also, these subdomains’ do not support email; for example, you would not be able to create the email address you@some.stuff.com with this script there is no way around this. These subdomains’ unfortunately aren’t search engine friendly either; so you won’t be seeing your subdomain listed in Google.

The Requirements:
Not all web hosting setups will work with this code, because the system relies on your web host automatically redirecting subdomains’ that don’t exist to your index page. To find out whether this will work for you, navigate to a subdomain of your domain that doesn’t exist (some.stuff.com) in your web browser. If you are redirected to your index page then this system will work for you, if not then it won’t (contact your hosting provider for details of how to correct this). Your host must also have PHP support.

How to do it:
At the very top of your ‘index.php’ or default page (the page a browser is automatically taken too when they navigate to your websites’ root, usually called index) (nb. must have .php extension, and no other index files must be located in your root directory), before any html, insert the following php code:

The above code assigns the address a user types into their URL bar to the variable ‘$sub’, so that it can be easily used later in the script. Variables in PHP start with a ‘$’ sign and allow us to easily use a string or piece of data later in our script. This means that wherever we type the variable ‘$sub’ into our script, the URL the visitor tried to go to will be inserted.

We can now use very basic code to check whether ‘$sub’ (the URL the visitor tried to navigate to) matches a pre-defined URL, and if so create an HTML frame which grabs the specified page and puts it over the top of ours.

Remember, if it doesn’t then the contents of the rest of your page will be shown.

Below the code we just inserted at the top of our page, put this code:

We have basically just made a big window over our website, which displays another website if a certain address is used to get to our site.
Let’s grind the code down and see what it does. Firstly we insert the ‘$sub’ variable, which we assigned earlier, within an ‘if’ statement which tells the script that if it is the same as a predefined subdomain (in this case, ‘http://sub.yourdomain.com/’) to send the HTML frame to the browser. Then on the last line we close the ‘if condition’ and finish the script.

One may ask the following question...
Why are all the different parts of the script in separate PHP opening and closing tags (<?php and ?>)?

This is so we can send the HTML frame code to the browser without the server thinking that it is PHP and trying to execute it. PHP code is executed on the server where it is hosted, and then the HTML output is sent to the browser; therefore it is very secure because a visitor cannot get access to the PHP code.

But what if I don’t want to mask the URL?
The code is very simply varied so that if $sub matches a predefined subdomain, the code to redirect is executed instead of the code to frame.

As you can see there is some variation in the way the script is laid out, but the basic principal remains the same. This time the script is all in one PHP open and close tag because the redirect code is a PHP command and not HTML. If you read the rest of the tutorial thoroughly then you should have no problem understanding this code :P .

And now you’re finished!
Check the subdomains’ that you setup work by navigating to it in your web browser. If it doesn't’t, carefully follow the instructions over again and make sure that your hosting package complies with the requirements stated at the beginning of this tutorial.

Comment
Tell A Friend
 
       
       
Home | Tutorials | Forum | Submit | Articles | Downloads | Affiliates | Topsites | Contact | Site Map
Copyright © 2004 - 2006 AK Productions Studios. All Rights Reserved. hit tracker