All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

PHP Get Domain From URL

Last Updated : Mar 11, 2024

PHP Get Domain From URL

In this article we will show you the solution of PHP get domain from URL, here we will see two different examples to get the domain from the URL.

We used a super global variable named $_SERVER here.

$_SERVER: this super global variable is used to hold information about the header, path, and script location.

We also used the parse_url() function here. This function is used to parse a URL and return its components.

Step By Step Guide On PHP Get Domain From URL :-

Example 1

PHP Code:

<!DOCTYPE html>
<html lang = " en " >
    <meta charset = " UTF - 8" >
    <meta http-equiv = " X-UA-Compatible " content = " IE=edge " >
    <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " >
    <title> php get domain from URL </title>
    <link rel="stylesheet" href="">
    <h1 style=" color : rgb(113, 221, 113) ;"> TALKERSCODE </h1>
    <h2> php get domain from URL </h2>
    $url = '' ;
    $url = parse_url($url) ;
    echo 'Host: '.$url['host']."\n" ;
  1. First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As mentioned above, the <head> tag contains information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1> tag used to add heading here and also adding the inline CSS here.
  7. Close the HTML tag with </html>
  8. At first <?php tag to write the php.
  9. Created a variable for $url and add some url into it
  10. Parse_url() used here to parse the url and return components of URL.
  11. Using echo to display the domain name in the HTML page
  12. Close the php code with ?> tag

Example 2

PHP code:

function GetRootDomain () {
    $url = strlower($_SERVER ["HTTP_HOST"]) ;
    $url = ereg_replace('www\.'. '', $url) ;
    $url = parse_url($url) ;
    if (!empty ($url ["host"])) {
        return $url ["host"] ;
    } else {
        return $url [$path] ;
  1. At first <?php tag to write the php.
  2. Declare a function name GetRootDomain ().
  3. Using strlower() with the super global variable $_SERVER.
  4. Ereg_replace() to remove the www\.
  5. Parse_url() used here to parse the url and return components of URL
  6. Creating an if-else statement for if the url is not empty then return the host.
  7. Close the php code with ?> tag
include ("get_root_domain_function.php") ;
$root_domain = GetRootDomain () ;
print $root_domain ;
  1. At first <?php tag to write the php.
  2. Now include the php file above
  3. Call the function GetRootDomain()
  4. Using print_r to display the $root_domain.
  5. Close the php code with ?> tag

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know how to get domain from URL using PHP.

I hope this article on PHP get domain from URL helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪