php怎么獲取真實(shí)IP
Jun 13, 2016 am 11:15 AM
php如何獲取真實(shí)IP
試了網(wǎng)上很多代碼都不行,但是ip138每次都能顯示我的真實(shí)ip。特求各位大俠支招。
比如我用一般的php獲得的我的ip地址是:60.55.8.229?-?浙江寧波
但是ip38.com獲取的我的IP地址是:[42.196.192.67]?來自:上海市?長(zhǎng)城寬帶
ip138才是正確的。
我在醫(yī)院里測(cè)試也是這樣,自己的php代碼獲取的IP是浙江杭州
但是ip138卻能獲得我的真實(shí)ip,上海交通大學(xué)
我現(xiàn)在用的PHP代碼如下:
function GetIP(){ <br />if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) <br />$ip = getenv("HTTP_CLIENT_IP"); <br />else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) <br />$ip = getenv("HTTP_X_FORWARDED_FOR"); <br />else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) <br />$ip = getenv("REMOTE_ADDR"); <br />else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) <br />$ip = $_SERVER['REMOTE_ADDR']; <br />else <br />$ip = "unknown"; <br />return($ip); <br />} <br />echo GetIP();
------解決方案--------------------
這是我的方法:
Function curIp(){<br /> $ip='';<br /> IF(Getenv('HTTP_CLIENT_IP') And StrCaseCmp(Getenv('HTTP_CLIENT_IP'),'unknown')){<br /> $ip=Getenv('HTTP_CLIENT_IP');<br /> }ElseIF(Getenv('HTTP_X_FORWARDED_FOR') And StrCaseCmp(Getenv('HTTP_X_FORWARDED_FOR'),'unknown')){<br /> $ip=Getenv('HTTP_X_FORWARDED_FOR');<br /> }ElseIF(Getenv('REMOTE_ADDR')And StrCaseCmp(Getenv('REMOTE_ADDR'),'unknown')){<br /> $ip=Getenv('REMOTE_ADDR');<br /> }ElseIF(isset($_SERVER['REMOTE_ADDR']) And $_SERVER['REMOTE_ADDR'] And StrCaseCmp($_SERVER['REMOTE_ADDR'],'unknown')){<br /> $ip=$_SERVER['REMOTE_ADDR'];<br /> }Else{<br /> $ip='127.0.0.1';<br /> }<br /> Return $ip;<br /> }
------解決方案--------------------
function?real_ip()
{
static?$realip?=?NULL;
if?($realip?!==?NULL)
{
return?$realip;
}
if?(isset($_SERVER))
{
if?(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$arr?=?explode(',',?$_SERVER['HTTP_X_FORWARDED_FOR']);
/*?取X-Forwarded-For中第一個(gè)非unknown的有效IP字符串?*/
foreach?($arr?AS?$ip)
{
$ip?=?trim($ip);
if?($ip?!=?'unknown')
{
$realip?=?$ip;
break;
}
}
}
elseif?(isset($_SERVER['HTTP_CLIENT_IP']))
{
$realip?=?$_SERVER['HTTP_CLIENT_IP'];
}
else
{
if?(isset($_SERVER['REMOTE_ADDR']))
{
$realip?=?$_SERVER['REMOTE_ADDR'];
}
else
{
$realip?=?'0.0.0.0';
}
}
}
else
{
if?(getenv('HTTP_X_FORWARDED_FOR'))
{
$realip?=?getenv('HTTP_X_FORWARDED_FOR');
}
elseif?(getenv('HTTP_CLIENT_IP'))
{
$realip?=?getenv('HTTP_CLIENT_IP');
}
else
{
$realip?=?getenv('REMOTE_ADDR');
}
}
$onlineip?=?null;
preg_match("/[\d\.]{7,15}/",?$realip,?$onlineip);
$realip?=?!empty($onlineip[0])???$onlineip[0]?:?'0.0.0.0';
return?$realip;
}
------解決方案--------------------

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

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

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

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

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.

In iOS 17, Apple introduced several new privacy and security features to its mobile operating system, one of which is the ability to require two-step authentication for private browsing tabs in Safari. Here's how it works and how to turn it off. On an iPhone or iPad running iOS 17 or iPadOS 17, Apple's browser now requires Face ID/Touch ID authentication or a passcode if you have any Private Browsing tab open in Safari and then exit the session or app to access them again. In other words, if someone gets their hands on your iPhone or iPad while it's unlocked, they still won't be able to view your privacy without knowing your passcode

The famous activation script MAS2.2 version supports digital activation again. The method originated from @asdcorp and the team. The MAS author calls it HWID2. Download gatherosstate.exe (not original, modified) from https://github.com/massgravel/Microsoft-Activation-Scripts, run it with parameters, and generate GenuineTicket.xml. First take a look at the original method: gatherosstate.exePfn=xxxxxxx;DownlevelGenuineState=1 and then compare with the latest method: gatheros

WindowsServerBackup is a function that comes with the WindowsServer operating system, designed to help users protect important data and system configurations, and provide complete backup and recovery solutions for small, medium and enterprise-level enterprises. Only users running Server2022 and higher can use this feature. In this article, we will explain how to install, uninstall or reset WindowsServerBackup. How to Reset Windows Server Backup If you are experiencing problems with your server backup, the backup is taking too long, or you are unable to access stored files, then you may consider resetting your Windows Server backup settings. To reset Windows

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.
