Scriptplayground Network

Scriptplayground Preview
Fireworks CS4 Learning Center

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area. This book explains the process of working with PHP in Flash, while creating real world examples that you can actually learn something from.

The Flash and PHP Bible has a dedicated forum for support and comments.

Scriptplayground » tutorials » php » Grab Subdomain From Url

Grab Subdomain From Url

Use this block of code to grab the subdomain from the URL. You can then use it in any part of your code.

This requires your host to have: #ServerAlias *.domain.com in the .htaccess file of in the config of Apache otherwise Apache just redirects to a domain name.

<?php

// Whether or not to output debug code
//DEFINE("DEBUG", 1);
DEFINE("DEBUG", 0);

// An array of actual subdomains on the server
$real_subdomains = array('subdomain1','subdomain2');

// The domain info for this site
$domain_name = 'domain';
$domain_tld = '.com';

// Set a variable to hold the host info
$host_info = '';

// Check to see if 'register_globals' is on
if(ini_set('register_globals' == 1))
	{
	$host_info = $HTTP_HOST;
	}
else
	{
	$host_info = $_SERVER['HTTP_HOST'];
	}

// Break up the pieces of the host info
$parts = explode(".", $host_info);

// Pull the 'subdomain' for the url
$subdomain = $parts[0];

(DEBUG) ? print "Host Info: " . $host_info : '';

// Check that the subdomain isn't 'www' or the domain name
if(($subdomain != "www") && ($subdomain != $domain_name))
	{
	// Is the subdomain a real domain?
	if(in_array($subdomain, $real_subdomains))
	{
	// Redirect and exit
	header("Location: http://www." . $domain_name . $domain_tld . "/" . $subdomain . "/");
	exit();
	}
	else
	{
	header("Location: http://www." . $domain_name . $domain_tld . "/");
	}
	}

?>

The above code has been commented for easy use. This will work if "register_globals" is on or off.

| Print It |  Follow Scriptplayground on Twitter (@scriptplay)

Comments: Grab Subdomain From Url

 Jhecht  Sun Jun 10, 2007 5:54 pm  
I did this function a bit backwards. I would have a subdomain, but i wanted to get the base domain from the subdomain. I came up with this simple function :

function find_base_domain($url=false){
if($url== false){
$url = (@is_array($_SERVER)) ? $_SERVER['SERVER_NAME'] : ((@is_array($HTTP_SERVER_VARS)) ? $HTTP_SERVER_VARS['SERVER_NAME'] : $SERVER_NAME );
//If $_SERVER is an array, give the server name, if $HTTP_SERVER_VARS is an array, give the server name from that, if not, then just use the old old $SERVER_NAME variable;
}
//Continue with the rest of the function;
$url = str_replace("http://","",$url);
$domainExt = substr($url,strrpos($url,"."));

$domain = substr($url,0,strrpos($url,"."));

if(strstr($domain,".")){
$base_domain = substr($domain,strrpos($domain,".")+1);
}else{
$base_domain = $domain;
}
return $base_domain.$domainExt;


}

I plan on making it so it takes another argument, being $level, which says how many levels back you want to go(since some sites have subdomain1.subdomain2.domain.com and so on(like yahoo) ).
 mkeefe  Mon Jun 11, 2007 3:01 am  
That is a very useful function, do you mind if I post it in the forum resources with full credit given?
Add a comment
Name:
Website:
Comment:
Please note: Offensive comments, flaming and spamming is not permitted on this site and your comment will be deleted immediately.

HTML is not allowed.

Please provide all comments in English so that others can help you. A common helper in this is to use an online translator.

As a security measure your ip will be recorded.
 
Anti-Robot Check:

Enter the key you see above.

What is this?: This extra test has been added due to the recent explosion of spam.
 
Google