Jump to content

How to get the value of subdomain


etrader

Recommended Posts

is this any use to you?

<?php 
$urlParts = explode('.', $_SERVER['HTTP_HOST']);
echo($urlParts[1]); //or 0 if no www
?>

 

EDIT: to actually answer your question

 

<?php 
$urlParts = explode('.', $_SERVER['HTTP_HOST']);
$variable = $urlParts[1]; //or 0 if no www
?>

Link to comment
Share on other sites

This is not an easy task to do, but I sat here and figured out a way because it interests me.

 

I first parse the url, then find the main host,  compared the values, I insert a www. to the front of the main host so there is always a position zero in the array. Then exploded the array and use position zero as the subdomain.

 

Basically if there is no subdomain it will always be www. , because www. is actually a subdomain.

 

<?php
$url = mysql_real_escape_string($_GET['url']);

if (substr($url, 0, 5) != "http:") {
$url = "http://$url";
}

function getsubdomain($new_parse_url) {

                $parsedUrl = parse_url(strtolower($new_parse_url));
                $parse_url = trim($parsedUrl[host] ? $parsedUrl[host] : array_shift(explode('/', $parsedUrl[path], 2)));
                
                $slds =
                        '\.co\.uk|\.me\.uk|\.net\.uk|\.org\.uk|\.sch\.uk|
    \.ac\.uk|\.gov\.uk|\.nhs\.uk|\.police\.uk|
    \.mod\.uk|\.asn\.au|\.com\.au|\.net\.au|\.id\.au|
    \.org\.au|\.edu\.au|\.gov\.au|\.csiro\.au';

                preg_match (
                        "/^(http:\/\/|https:\/\/|)[a-zA-Z-]([^\/]+)/i",
                        $parse_url, $thematches
                );

                $host = $thematches[2];

                if (preg_match("/$slds$/", $host, $thematches)) {
                    preg_match (
                            "/[^\.\/]+\.[^\.\/]+\.[^\.\/]+$/",
                            $host, $thematches
                    );
                }
                else {
                    preg_match (
                            "/[^\.\/]+\.[^\.\/]+$/",
                            $host, $thematches
                    );
                }
                
                if($parse_url == $thematches[0]){
                $urlParts = "www.$thematches[0]";
                } else {
                if (substr($parse_url, 0, 4) != "www.") {
                $parse_url = "www.$parse_url";
                }
                $urlParts = $parse_url;
                }
                
                $subParts = explode('.', $urlParts);
                $subdomain = $subParts[0];
                
                return $subdomain;
            }


$subdomain = getsubdomain($url);
echo $subdomain;
             
?> 

Link to comment
Share on other sites

i dont know if that over complicates it...

 

would this not do the trick?

<?php
$url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . $_SERVER['QUERYSTRING'];
$urlParts = explode('.', $_SERVER['HTTP_HOST']);
if(strpos($url, 'www')){
$subdomain_value = $urlParts[1];
} else { 
$subdomain_value = $urlParts[0]; 
};
echo $subdomain_value;
?>

 

and even then you can whittle that code down loads...

 

Link to comment
Share on other sites

I believe etrader wants to read from different urls and find the subdomains only.

 

The problem arises when there is no www, and also no subdomain value, it would read the subdomain as being the first part of a domain name.

 

I don't believe what I did over complicates it at all, because it's kind of a difficult task to accomplish. It can't just be simple explodes because the amounts and the values of the positions may change.

 

I believe is just one more step needed in the code I made, to check for a match if the first part domain name comes up as the subdomain, then just call it www as the subdomain instead.

 

I'll fiddle with in in a little bit and get it working.

 

 

Link to comment
Share on other sites

OK, I finally got some time to get this working.

 

It will find the subdomain of a url, if no subdomain is present the subdomain will be www.

 

I have the common levels for domain patterns for the preg_match, if I missed any or need more, add them in.

 

<?php
//getting url from address bar
//http://mysite.com/this-script-name.php?url=subdomain.domain.co.uk
$url = mysql_real_escape_string($_GET['url']);

function getsubdomain($new_parse_url) {
//if empty value display www and exit
if($new_parse_url == ""){
return "www";
EXIT;
}
//add http:// if is not present
if (substr($new_parse_url, 0, 5) != "http:") {
$new_parse_url = "http://$new_parse_url";
}
//parse and clean the url
                $parsedUrl = parse_url(strtolower(trim($new_parse_url)));
                $parse_url = $parsedUrl[host] ? $parsedUrl[host] : array_shift(explode('/', $parsedUrl[path], 2));
                //patterns for preg_match of second level domains 
                $slds =
                        '\.co\.uk|\.me\.uk|\.net\.uk|\.org\.uk|\.sch\.uk|
    \.ac\.uk|\.gov\.uk|\.nhs\.uk|\.police\.uk|
    \.mod\.uk|\.asn\.au|\.com\.au|\.net\.au|\.id\.au|
    \.org\.au|\.edu\.au|\.gov\.au|\.csiro\.au';
    //sorting to just get domain
                preg_match (
                        "/^(http:\/\/|https:\/\/|)[a-zA-Z-]([^\/]+)/i",
                        $parse_url, $thematches
                );
                //host position
                $host = $thematches[2];
                //explode the parsed url into parts
                $explode_parse = explode(".",$parse_url);
                //subdomain is position zero
                $subdomain = $explode_parse[0];
                //count the parts
                $count_positions = count($explode_parse);
                //check if second level domain exists
                if (preg_match("/$slds$/", $host, $thematches)) {
                    preg_match (
                            "/[^\.\/]+\.[^\.\/]+\.[^\.\/]+$/",
                            $host, $thematches
                    );                    
                    //counting positions so host is not used as subdomain
                    if($count_positions <= 3){
                $subdomain = "www";
                }
                } else {
                //if second level domain does not exist
                    preg_match (
                            "/[^\.\/]+\.[^\.\/]+$/",
                            $host, $thematches
                    );                    
                    //counting positions so host is not used as subdomain
                    if($count_positions <= 2){
                $subdomain = "www";
                }

                }
                //return the subdomain
                return $subdomain;
            }

//usage
$subdomain = getsubdomain($url);
echo $subdomain;
             
?> 

Link to comment
Share on other sites

QuickOldCar you scare me!

 

return "www"; EXIT;

You do realise that EXIT; will never be executed, do you?

 

$url = mysql_real_escape_string($_GET['url']);

This will actually put FALSE into $url when it can't establish a MySQL connection (when mysql_connect() with zero parameters fails). You also don't need to call it since you don't actually store the value in MySQL.

 

$parsedUrl[host] ? $parsedUrl[host] : array_shift(explode('/', $parsedUrl[path], 2));

No quotes around string indexes?

 

Always place your functions at the bottom of your scripts so that it remains readable and understandable.

Link to comment
Share on other sites

All I can say is it works.

 

When is no value it doesn't output anything but www, if the exit is extra so be it.

The mysql real escape is a habit because I usually do input into mysql, but it still does work exactly the way I have it. It's was just a demo anyway. The function is what the person needs.

 

As for the parsing of url functions, it doesn't need any quotes and when you do it breaks.

You can even look here and see not one of them are quoted when multiple parses are used.

http://php.net/manual/en/function.parse-url.php

 

I will forever place my functions up top so they can be accessed multiple times if needed without having to be defined first. If I was to include the function, that's where it would be.

 

Feel free to make changes and then post your "fixed" version.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.