PHP获取域名、IP地址的方法
1 |
<?php |
2 |
echo gethostbyname("www.jbxue.com"); |
3 |
?> |
以上会输出域名所对应的的IP。
对于做了负载与cdn的域名来讲,可能返回的结果会有不同,这点注意下。
下面来说说获取域名的方法,例如有一段网址:http://www.jbxue.com/all-the-resources-of-this-blog.html
方法1,
echo $_SERVER[“HTTP_HOST”];
//则会输出www.jbxue.com
本地测试则会输出localhost。
方法2,使用parse_url函数;
1 |
<?php |
2 |
$url ="http://www.jbxue.com/index.php?referer=jbxue.com"; |
3 |
$arr=parse_url($url); |
4 |
echo "<pre>"; |
5 |
print_r($arr); |
6 |
echo "</pre>"; |
7 |
?> |
输出为数组,结果为:
(
[scheme] => http
[host] => www.jbxue.com
[path] => /index.php
[query] => referer=jbxue.com
)
说明:
scheme对应着协议,host则对应着域名,path对应着执行文件的路径,query则对应着相关的参数;
方法3,采用自定义函数。
01 |
<?php |
02 |
$url ="http://www.jbxue.com/index.php?referer=jbxue.com"; |
03 |
get_host($url); |
04 |
function get_host($url){ |
05 |
//首先替换掉http:// |
06 |
$url=Str_replace("http://","",$url); |
07 |
//获得去掉http://url的/最先出现的位置 |
08 |
$position=strpos($url,"/"); |
09 |
//如果没有斜杠则表明url里面没有参数,直接返回url, |
10 |
//否则截取字符串 |
11 |
if($position==false){ |
12 |
echo $url; |
13 |
}else{ |
14 |
echo substr($url,0,$position); |
15 |
} |
16 |
} |
17 |
?> |
方法4,使用php正则表达式。
1 |
<?php |
2 |
header("Content-type:text/html;charset=utf-8"); |
3 |
$url ="http://www.jbxue.com/index.php?referer=jbxue.com"; |
4 |
$pattern="/(http:\/\/)?(.*)\//"; |
5 |
if(preg_match($pattern,$url,$arr)){ |
6 |
echo "匹配成功!"; |
7 |
echo "匹配结果:".$arr[2]; |
8 |
} |
9 |
?> |
PHP获取域名、IP地址的方法的更多相关文章
- Flask框架获取用户IP地址的方法
本文实例讲述了python使用Flask框架获取用户IP地址的方法.分享给大家供大家参考.具体如下: 下面的代码包含了html页面和python代码,非常详细,如果你正使用Flask,也可以学习一下最 ...
- C#获取真实IP地址实现方法
通常来说,大家获取用户IP地址常用的方法是: string IpAddress = ""; if((HttpContext.Current.Request.ServerVariab ...
- .net core获取本地Ip地址的方法
笔记: /// <summary> /// 获取本地Ip地址 /// </summary> /// <returns></returns> public ...
- 获取用户Ip地址通用方法常见安全隐患(HTTP_X_FORWARDED_FOR)
分析过程 这个来自一些项目中,获取用户Ip,进行用户操作行为的记录,是常见并且经常使用的. 一般朋友,都会看到如下通用获取IP地址方法. function getIP() { if (isset($_ ...
- python获取本地ip地址的方法
#_*_coding:utf8_*_ #以下两种方法可以在ubuntu下或者windows下获得本地的IP地址 import socket # 方法一 localIP = socket.gethost ...
- 集群环境下JSP中获取客户端IP地址的方法
String ip = request.getHeader("X-Forwarded-For");if (ip == null || ip.length() == 0 || &qu ...
- php获取客户端IP地址的方法
参考:https://www.cnblogs.com/rendd/p/6183094.html <?php function getip() { //strcasecmp 比较两个字符,不区分大 ...
- 获取用户Ip地址通用方法
1 public static function getIp() 2 { 3 if ($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"]) 4 { ...
- 获取树莓派ip地址的方法
1.有显示器 (1)鼠标停留在屏幕右上角网络图标上,2-3秒会显示网络连接信息(2) 图形界面下打开终端运行ifconfig命令 2.登录路由器查看名叫raspberry的设备 3.电脑上运行命 ...
- 【转】Asp.Net Core2.0获取客户IP地址,及解决发布到Ubuntu服务器获取不到正确IP解决办法
1.获取客户端IP地址实现方法(扩展类) using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.ModelBinding; u ...
随机推荐
- altera tcl
例子:https://www.altera.com/support/support-resources/design-examples/intellectual-property/embedded/n ...
- 机器学习经典算法具体解释及Python实现--线性回归(Linear Regression)算法
(一)认识回归 回归是统计学中最有力的工具之中的一个. 机器学习监督学习算法分为分类算法和回归算法两种,事实上就是依据类别标签分布类型为离散型.连续性而定义的. 顾名思义.分类算法用于离散型分布预測, ...
- 红茶一杯话Binder (传输机制篇_中)
红茶一杯话Binder (传输机制篇_中) 侯 亮 1 谈谈底层IPC机制吧 在上一篇文章的最后,我们说到BpBinder将数据发到了Binder驱动.然而在驱动层,这部分数据又是如何传递到BBind ...
- OpenCv中基本数据类型--Point,Size,Rect,Scalar,Vec3b类类型的详细解释
头文件路径:opencv-2.4.9/modules/core/include/opencv2/core/core.hpp 一.Point类 在这些数据类型中,最简单的就是Point点类,Point类 ...
- python c example2:pylame2
#include <Python.h> #include <lame.h> //pyton object variables typedef struct{ PyObject_ ...
- library not found for -lPods-AFNetworking解决放案
出现library not found for -lPods-AFNetworking这个报错, 来自于我从git上面把我项目直接Download下来的,我的项目里面用了CocoaPods的,如今pr ...
- Windows Azure 系列-- Azure Redis Cache的配置和使用
假设还没有配置Azure Power shell 能够參照这里进行配置:http://blog.csdn.net/lan_liang/article/details/46850221 打开Azure ...
- nc在centos7上的安装和简单使用
下载 http://vault.centos.org/6.6/os/x86_64/Packages/nc-1.84-22.el6.x86_64.rpm rpm -iUv nc-1.84-22. ...
- python中SQL的使用
# 常用的关系型数据库有 mysql postgresql sqlite 等(具体区别上课再说) # # 传统数据库以表的形式存储数据 # 一张表可以有很多个字段 # 以用户表为例, 存储 4 个数据 ...
- A Survey of Shape Feature Extraction Techniques中文翻译
Yang, Mingqiang, Kidiyo Kpalma, and Joseph Ronsin. "A survey of shape feature extraction techni ...