PHP获取域名及域名IP的步骤
PHP获取域名及域名IP的方法
最近在做的一个项目用到获取域名和IP的功能,大致有以下几种方法。
获取域名IP的方法可以使用内置的函数gethostbyname获取,例如:
echo gethostbyname("www.jbxue.com");
以上则会直接输出域名所对应的的IP,当然各个地方测试的结果是不一样的,因为百度的服务器也不只有一台,我这边的ip是115.239.210.27;
下面是获取域名的方法,例如有一段网址是这样的:http://www.jbxue.com/all-the-resources-of-this-blog.html
//则会输出www.jbxue.com //全局数组 echo $_SERVER[“HTTP_HOST”];
我在本地测试则会输出localhost
第二种获得域名的方法则是使用:parse_url函数;
$url ="http://www.jbxue.com/index.php?referer=kakata.com"; $arr=parse_url($url); echo " "; print_r($arr); echo " “;
这段代码获得一个数组,当我们打印这个数组,你会一目了然;
Array
(
[scheme] => http
[host] => www.jbxue.com
[path] => /index.php
[query] => referer=jbxue.com
)
scheme对应着协议,host则对应着域名,path对应着执行文件的路径,query则对应着相关的参数;
php 获取域名信息的第三种方法:
同样是这一段域名我们使用第三种方法获取域名,我们采用自定义函数获得。
<!--?php <br ?--> $url ="http://www.jbxue.com/index.php?referer=jbxue.com"; get_host($url); function get_host($url){ //首先替换掉http:// $url=str_replace("http://","",$url); //获得去掉http://url的/最先出现的位置 $position=strpos($url,"/"); //如果没有斜杠则表明url里面没有参数,直接返回url, //否则截取字符串 if($position==false){ echo $url; }else{ echo substr($url,0,$position); } } ?>
最后一种方法则是使用正则了,这一步比较复杂,要实现复杂的域名匹配就得更复杂的正则,我只是简单的提供一下思路,大家多尝试。
<!--?php <br ?--> 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 "匹配成功"; echo "匹配到了".$arr[2]; } ?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



HTTP status code 520 means that the server encountered an unknown error while processing the request and cannot provide more specific information. Used to indicate that an unknown error occurred when the server was processing the request, which may be caused by server configuration problems, network problems, or other unknown reasons. This is usually caused by server configuration issues, network issues, server overload, or coding errors. If you encounter a status code 520 error, it is best to contact the website administrator or technical support team for more information and assistance.

To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

The reason for the error is NameResolutionError(self.host,self,e)frome, which is an exception type in the urllib3 library. The reason for this error is that DNS resolution failed, that is, the host name or IP address attempted to be resolved cannot be found. This may be caused by the entered URL address being incorrect or the DNS server being temporarily unavailable. How to solve this error There may be several ways to solve this error: Check whether the entered URL address is correct and make sure it is accessible Make sure the DNS server is available, you can try using the "ping" command on the command line to test whether the DNS server is available Try accessing the website using the IP address instead of the hostname if behind a proxy

Understand the meaning of HTTP 301 status code: common application scenarios of web page redirection. With the rapid development of the Internet, people's requirements for web page interaction are becoming higher and higher. In the field of web design, web page redirection is a common and important technology, implemented through the HTTP 301 status code. This article will explore the meaning of HTTP 301 status code and common application scenarios in web page redirection. HTTP301 status code refers to permanent redirect (PermanentRedirect). When the server receives the client's

Differences: 1. Different definitions, url is a uniform resource locator, and html is a hypertext markup language; 2. There can be many urls in an html, but only one html page can exist in a url; 3. html refers to is a web page, and url refers to the website address.

How to use NginxProxyManager to implement automatic jump from HTTP to HTTPS. With the development of the Internet, more and more websites are beginning to use the HTTPS protocol to encrypt data transmission to improve data security and user privacy protection. Since the HTTPS protocol requires the support of an SSL certificate, certain technical support is required when deploying the HTTPS protocol. Nginx is a powerful and commonly used HTTP server and reverse proxy server, and NginxProxy

HTTP status code 403 means that the server rejected the client's request. The solution to http status code 403 is: 1. Check the authentication credentials. If the server requires authentication, ensure that the correct credentials are provided; 2. Check the IP address restrictions. If the server has restricted the IP address, ensure that the client's IP address is restricted. Whitelisted or not blacklisted; 3. Check the file permission settings. If the 403 status code is related to the permission settings of the file or directory, ensure that the client has sufficient permissions to access these files or directories, etc.

In today's era of rapid technological development, programming languages are springing up like mushrooms after a rain. One of the languages that has attracted much attention is the Go language, which is loved by many developers for its simplicity, efficiency, concurrency safety and other features. The Go language is known for its strong ecosystem with many excellent open source projects. This article will introduce five selected Go language open source projects and lead readers to explore the world of Go language open source projects. KubernetesKubernetes is an open source container orchestration engine for automated
