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 " > <head> <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="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css"> </head> <body> <h1 style=" color : rgb(113, 221, 113) ;"> TALKERSCODE </h1> <h2> php get domain from URL </h2> </body> </html> <?php $url = 'http://talkerscode.com/php/php.php' ; $url = parse_url($url) ; echo 'Host: '.$url['host']."\n" ; ?>
- First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- 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.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
- <h1> tag used to add heading here and also adding the inline CSS here.
- Close the HTML tag with </html>
- At first <?php tag to write the php.
- Created a variable for $url and add some url into it
- Parse_url() used here to parse the url and return components of URL.
- Using echo to display the domain name in the HTML page
- Close the php code with ?> tag
Example 2
PHP code:
<?php 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] ; } } ?>
- At first <?php tag to write the php.
- Declare a function name GetRootDomain ().
- Using strlower() with the super global variable $_SERVER.
- Ereg_replace() to remove the www\.
- Parse_url() used here to parse the url and return components of URL
- Creating an if-else statement for if the url is not empty then return the host.
- Close the php code with ?> tag
<?php include ("get_root_domain_function.php") ; $root_domain = GetRootDomain () ; print $root_domain ; ?>
- At first <?php tag to write the php.
- Now include the php file above
- Call the function GetRootDomain()
- Using print_r to display the $root_domain.
- 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.