get domain name in php – How to get current page URL in PHP?

get domain name in php – How to get current page URL in PHP?

get domain name in php from a URL, you can use the parse_url() method and host parameter. All PHP Code are group together in getDomain() function.

Example:-

function getDomain($url){
    $Uri = parse_url($url);
    $domain = isset($Uri['host']) ? $Uri['host'] : '';
    if(preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)){
        return $regs['domain'];
    }
    return FALSE;
}

echo getDomain("http://infinityknow.com"); // outputs 'infinityknow.com'
echo getDomain("http://www.infinityknow.com"); // outputs 'infinityknow.com'
echo getDomain("http://mail.infinityknow.co.uk"); // outputs 'infinityknow.co.uk'
</domain>

How to Get Domain Name from URL in PHP?

<!--?php  
    if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on')   
         $url = "https://";   
    else  
         $url = "http://";     
    $url.= $_SERVER['HTTP_HOST'];   
      
    $url.= $_SERVER['REQUEST_URI'];    
      
    echo $url;  
  ?-->   
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments