在php中获取IP地址的方法有很多种,下面我介绍利用linux命令获取IP地址然后转换成数组输出,下面也介绍了一些常用关于php ip地址处理实例.
PHP获取服务器IP地址,用PHP执行ifconfig获得Linux服务器IP并输出为数组,下面是代码:
<?php function getServerIp() { //用ifconfig读取服务器IP并输出为数组 $ss = exec('/sbin/ifconfig | sed -n 's / ^ * . *addr: ([0 - 9.] { 7, }) . *$ / 1 / p'', $arr); return $arr; } $ips = getServerIp(); foreach ($ips as $k => $v) { //过滤IP if (substr($v, 0, 3) == '127' || substr($v, 0, 3) == '10.' || substr($v, 0, 7) == '192.168' || substr($v, 0, 6) == '172.16') { unset($ips[$k]); } } shuffle($ips); //重新排序 print_r($ips); ?>
一些php获取ip地址例,代码如下:
<?php function GetIP() { if (!emptyempty($_SERVER["HTTP_CLIENT_IP"])) { $cip = $_SERVER["HTTP_CLIENT_IP"]; } elseif (!emptyempty($_SERVER["HTTP_X_FORWARDED_FOR"])) { $cip = $_SERVER["HTTP_X_FORWARDED_FOR"]; } elseif (!emptyempty($_SERVER["REMOTE_ADDR"])) { $cip = $_SERVER["REMOTE_ADDR"]; } else { $cip = "无法获取!"; } return $cip; } echo GetIP(); ?>
我这里有一个比较公用的方法来获取用户ip地址,代码如下:
<?php function get_user_ip() { if (isset($_SERVER['HTTP_CLIENT_IP']) && $_SERVER['HTTP_CLIENT_IP'] != 'unknown') { $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] != 'unknown') { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip = $_SERVER['REMOTE_ADDR']; } return $ip; } ?>
PHP获取IP所在地区(调用新浪API获取IP所在地),代码如下:
<?php function get_location($ip) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=" . $ip); $location = curl_exec($curl); $location = json_decode($location); if ($location === FALSE) return ""; return emptyempty($location->desc) ? $location->province . $location->city . $location->district . $location->isp : $location->desc; } ?>
永久链接:
转载随意!带上文章地址吧。
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article
How to fix KB5055612 fails to install in Windows 10?
4 weeks agoByDDD
Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Roblox: Grow A Garden - Complete Mutation Guide
3 weeks agoByDDD
Nordhold: Fusion System, Explained
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web development tools
