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

5/5 - (1 vote)

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.

get domain name in php – Get Domain Name from URL in PHP

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'

Free Live Chat for Any Issue

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;  
  ?>   

How to Get Domain Name From URL in PHP?

get domain name in php

$a = "https://infinityknow.com/get-domain-name-in-php/";

Get Domain Name

$a = "https://infinityknow.com/get-domain-name-in-php/";

$Uri = parse_url($a);

echo "The domain is ".$Uri['host'];
// The domain is infinityknow.tempurl.host

Download

I hope you get an idea about get domain name in php.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.