php query user ip method: 1, get user ip by "$_SERVER["REMOTE_ADDR"]" way; 2, get user ip by "($user_IP) ? $user_IP : $_SERVER["REMOTE_ADDR"];" to get the user ip and so on.

php How to query user ip?

Five ways to get user ip in php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

// Get the user ip way 1

$ip = $_SERVER["REMOTE_ADDR"];

echo $ip;

// Get the user ip way 2

$user_IP = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];

$user_IP = ($user_IP) ? $user_IP : $_SERVER["REMOTE_ADDR"];

echo $user_IP;

// Get the user ip way 3

function getRealIp()

{

$ip=false;

if(!empty($_SERVER["HTTP_CLIENT_IP"])){

$ip = $_SERVER["HTTP_CLIENT_IP"];

}

if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {

$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);

if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }

for ($i = 0; $i < count($ips); $i++) {

if (!eregi ("^(10│172.16│192.168).", $ips[$i])) {

$ip = $ips[$i];

break;

}

}

}

return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);

}

echo getRealIp();

// Get the user ip way 4

if ($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"])

{

$ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];

}

elseif ($HTTP_SERVER_VARS["HTTP_CLIENT_IP"])

{

$ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];

}

elseif ($HTTP_SERVER_VARS["REMOTE_ADDR"])

{

$ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];

}

elseif (getenv("HTTP_X_FORWARDED_FOR"))

{

$ip = getenv("HTTP_X_FORWARDED_FOR");

}

elseif (getenv("HTTP_CLIENT_IP"))

{

$ip = getenv("HTTP_CLIENT_IP");

}

elseif (getenv("REMOTE_ADDR"))

{

$ip = getenv("REMOTE_ADDR");

}

else

{

$ip = "Unknown";

}

echo $ip ;

// Get the user ip way 5

if(getenv('HTTP_CLIENT_IP')) {

$onlineip = getenv('HTTP_CLIENT_IP');

} elseif(getenv('HTTP_X_FORWARDED_FOR')) {

$onlineip = getenv('HTTP_X_FORWARDED_FOR');

} elseif(getenv('REMOTE_ADDR')) {

$onlineip = getenv('REMOTE_ADDR');

} else {

$onlineip = $HTTP_SERVER_VARS['REMOTE_ADDR'];

}

echo $onlineip;

Related articles

How to invert a string by word in php

Inversion methods: 1. use "explode(' ',$str)" statement to convert string to array; 2. use "array_reverse($arr)" statement to invert the array and return the array in the reverse order of elements; 3. use "implode(' ',$arr)" statement to convert the inver

php get maximum value in several numbers

In php, you can use the max() function to get the maximum value among several numbers. The function works by calculating and returning the maximum value among several specified values, with the syntax "max(value1,value2,...) ;".

How to convert php image to base64

php image to base64 method: 1, get an image address; 2, through the "chunk_split(base64_encode($gambar));" method to convert the image to base64 string format can be.

How to merge arrays in PHP

How to merge arrays in PHP? To do this we need to use the array_merge function and the array_merge_recursive function. Here we will introduce these two functions separately.

How to compare strings in PHP?

String is an important data type in PHP, which how to compare strings is also very common in our development work, there are many kinds of comparison methods to compare strings, here we will introduce the more commonly used several comparison methods.

The difference between jumps and redirects in PHP

Difference: 1. Jump is the current URL request success, re-request a new URL; while redirect is the current URL is invalid, was redirected to a new URL. 2. In the jump, the browser will record the current URL and the new URL to the history; while redirect

How to merge elements of different arrays in php

php merge different array elements method: 1, create a PHP sample file; 2, by "array_keys(array_flip($a) + array_flip($b) + array_flip($c));" way to multiple arrays can be merged and de-weighted.

The php exec function does not work

The solution for the php exec function not taking effect: 1. Open the /etc/php.ini file, delete the exec, then save and restart php-fpm; 2. Change the value of "safe_mode" to off.

What is the difference between ado and php

The difference between ado and php: 1, ADO is a Microsoft technology, is a programming interface to access data in the database, while PHP is a general open source scripting language;