PHP get domain name and domain name IP method
Recently in a project to use to obtain the domain name and IP functions, roughly the following methods.
The method of acquiring the domain name IP can be obtained using the built-in function gethostbyname, for example:
Echo gethostbyname ("www.jbxue.com");
The above will directly output the IP address of the domain name, of course, the results of various local tests are not the same, because Baidu's server is not only one, my side of the IP is 115.239.210.27;
Here's how to get a domain name, such as a URL: http://www.jbxue.com/all-the-resources-of-this-blog.html
The www.jbxue.com//global array will be output echo $_server["Http_host"];
I will output localhost when I test locally
The second method of acquiring a domain name is using: Parse_url function;
$url = "http://www.jbxue.com/index.php?referer=kakata.com"; $arr =parse_url ($url); echo ""; Print_r ($arr); echo "";
This piece of code gets an array, and when we print this array, you will be at a glance;
Array
(
[Scheme] = http
[Host] = www.jbxue.com
[Path] =/index.php
[Query] = referer=jbxue.com
)
Scheme corresponds to the protocol, the host corresponds to the domain name, path corresponds to the path of the execution file, and query corresponds to the relevant parameters;
PHP Third Way to get domain name information:
This is also the domain name we use the third method to obtain the domain name, we take the custom function to obtain.
$url = "http://www.jbxue.com/index.php?referer=jbxue.com"; Get_host ($url); function Get_host ($url) {//replace http://$ First Url=str_replace ("http://", "", $url);//get rid of http://url/first occurrence position $position=strpos ($url, "/");//If there is no slash, there is no parameter in the URL. Directly returns url,//otherwise intercepts the string if ($position ==false) {echo $url;} Else{echo substr ($url, 0, $position);}}? >
The last method is to use the regular, this step is more complex, to achieve complex domain names to get more complex regular, I just simple to provide ideas, we try more.
Header ("Content-type:text/html;charset=utf-8"); $url = "http://www.jbxue.com/index.php?referer=jbxue.com"; $ Pattern= "/(http:\/\/)? (. *) \//", if (Preg_match ($pattern, $url, $arr)) {echo" matches successfully ", echo" matches ". $arr [2];}? >