0

Apologize if this has already been asked and answered; did a quick search, but, not exactly sure how to word it/what exactly to search for.

I have to web addresses pointing to one site/file(s). One ends in .net [domain.net] and the other ends in .org [domian.org].

Using PHP; I want to put at least the ".net" and/or ".org" part of the URL into a variable to determine what text is displayed.

Something to the effect of:

$domainExt = 'net';

For domain ending with .net; as an example.

Thanks for any help, tips, etc.

6 Answers6

1

First get the extension

$host = filter_input(INPUT_SERVER, 'HTTP_HOST');
$domainExt = pathinfo($host, PATHINFO_EXTENSION);

Then switch through the extension

switch($domainExt){
    case "net":
        $var = "yada";
        break;

    case "org":
        $var = "yada yada";
        break;
}

You can apply this example to fit your needs

Himanshu
  • 251
  • 4
  • 18
Ken Sawyerr
  • 236
  • 1
  • 7
0

You can use environment information.

$_SERVER['HTTP_HOST']

or

$_SERVER['SERVER_NAME']
Tuğca Eker
  • 1,493
  • 13
  • 20
0

You can use below code

<?php
$host =  filter_input(INPUT_SERVER, 'HTTP_HOST');
$domainExt = substr(strrchr($host, "."), 1);
?>
Himanshu
  • 251
  • 4
  • 18
0

Just explode the current HTTP_HOST at the period.

$ext = explode( '.', $_SERVER['HTTP_HOST'] );

if( $ext == 'org' ){
    echo 'This is .org';    
} else {
    echo 'This is .net';    
}
Xhynk
  • 13,513
  • 8
  • 32
  • 69
0

Use parse_url() function to get the domain extention.

Example below:

 $url = 'http://www.example.com/site';
    echo end(explode(".", parse_url($url, PHP_URL_HOST))); 
// output "com"

Dynamically get the domain extension like below way.

$url =  'http://' . $_SERVER['SERVER_NAME']; 
echo end(explode(".", parse_url($url, PHP_URL_HOST))); 
// output "com"
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
-1

Try to separate the website on different folder for .net and .org and use Virtual Host to route the domain names on their respective folders.

sorry for my English but i hope you get the point.

If you are using apache then google search can help you

Kevin Loquencio
  • 391
  • 1
  • 4
  • 16