Home  >  Article  >  Backend Development  >  PHP gets user IP address

PHP gets user IP address

PHP中文网
PHP中文网Original
2016-08-08 09:18:571123browse

PHP obtains user IP address

<?php

function getClientIp() {
	$uip = &#39;&#39;;
	if(!empty($_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;]) && strcasecmp($_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;], &#39;unknow&#39;)) {
		$uip = $_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;];
		strpos($uip, &#39;,&#39;) && list($uip) = explode(&#39;,&#39;, $uip);
	} elseif(!empty($_SERVER[&#39;HTTP_CLIENT_IP&#39;]) && strcasecmp($_SERVER[&#39;HTTP_CLIENT_IP&#39;], &#39;unknow&#39;)) {
		$uip = $_SERVER[&#39;HTTP_CLIENT_IP&#39;];
	} elseif(!empty($_SERVER[&#39;REMOTE_ADDR&#39;]) && strcasecmp($_SERVER[&#39;REMOTE_ADDR&#39;], &#39;unknow&#39;)) {
		$uip = $_SERVER[&#39;REMOTE_ADDR&#39;];
	}
	return $uip;
}
echo getClientIp();

The above introduces PHP to obtain user IP address, including aspects. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


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