Jump to content

$_SERVER['SERVER_NAME'] without the www.


Simonvetterli

Recommended Posts

I have a simmilar same problem, hope somebody can help me:

 

Depend of the tld I have I choose the language on the site:

 

so I have the follow code:

 

$domain = $_SERVER['SERVER_NAME'];

list(subdom,$secdom,$tld) = explode(".",$domain);

 

If I have in the browser www.turtlesdive.com no problem, ich I have www.turtlesdive.ch no problem.

 

But if I have only turtlesdive.com or turtlesdive.ch - I not get any more com or ch as $tld.

 

How can I do?

 

Kindly Simon

Link to comment
https://forums.phpfreaks.com/topic/275077-_serverserver_name-without-the-www/
Share on other sites

What you can do is either to check the count of elements, and if 2 then you know there is no sub-domain. Or you can use a regular expression, to match as required. In this case, I'd probably go for the RegExp, seeing as domains aren't quite as straight forward as they would seem.

$domainRegExp =
'(?P<sub>(?:[\\w\\pL][\\w\\pL-]*(?<!\\-)\\.)+)(?<!\\.|-)\\.'.
'(?P<domain>[\\w\\pL][\\w\\pL-]+)(?<!\\.|-)\\.'.
'(?P<tld>(??:priv|com?|me|org)\\.)?[a-z\\pL]{2,6})/ui';
This will give you the three parts of a domain address in three named groups, "sub", "domain" and "tld". Which will give you named indices in the matches array, if used with preg_match.

 

PS: Moved this to RegExp help, as this is very much regular expressions territory. ;)

 

PPS: Untested, so there might be bugs with this. If so, just let me know and I'll fix 'em.

Hi

 

And how I do with my "problem":

 

Here is my code:

 

$lang = $_GET['lang'];
	$currency = '';
	list($subdom,$secdom,$tld) = explode(".",$domain); 

	if (($lang == '') AND ($tld == 'ch')){ 
    	$lang = 'ge';
        $currency = 'CHF';		
		} 
	elseif (($lang == 'ge') AND ($tld == 'ch')){ 
   		$lang = 'ge';
        $currency = 'CHF';		
		}
	elseif (($lang == 'en') AND ($tld == 'ch')){ 
   		$lang = 'en';
		$currency = 'CHF';
		}
	elseif (($lang == '') AND ($tld == 'com')){ 
   		$lang = 'en';
		$currency = 'THB';
		}
	elseif (($lang == 'ge') AND ($tld == 'com')){ 
   		$lang = 'ge';
		$currency = 'THB';
		}	

	$_GET['lang'] 	= $lang;
	$url1 			= $_SERVER['PHP_SELF'];
	$url 			= basename($url1);

How I "include" Your code?

 

Kindly Simon

Did you check the manual page for preg_match, which I linked you to? It is explained in great detail there how to use it to match a string against a regular expression.

 

Also, after having a second look of the code it seems I forgot an important detail: Making the sub domain(s) optional. Here's the updated version:

$domainRegExp =
'(??P<sub>(?:[\\w\\pL][\\w\\pL-]*(?<!\\-)\\.)+)(?<!\\.|-)\\.)?'.
'(?P<domain>[\\w\\pL][\\w\\pL-]*)(?<!\\.|-)\\.'.
'(?P<tld>(??:priv|com?|me|org)\\.)?[a-z\\pL]{2,6})/ui';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.