PHP下得到客戶端IP的方法

2021-09-30 03:38:42 字數 1082 閱讀 8073

php下得到客戶端ip的方法

2007-04-19 10:53

但是當web伺服器api是asapi (iis)的時候,getenv函式是不起作用的。這種情況下你如果用getenv來取得使用者客戶端ip的話,得到的將是錯誤的ip位址。

getenv -- gets the value of an environment variable

stringgetenv( string varname )

returns the value of the environment variable varname, orfalseon an error.

<?php

// example use of getenv()

$ip

= getenv

('remote_addr'

);// or simply use a superglobal ($_server or $_env)

$ip

= $_server

['remote_addr'

];?>

這是在php官方的manual提供的方法。

但是當web伺服器api是asapi (iis)的時候,getenv函式是不起作用的。這種情況下你如果用getenv來取得使用者客戶端ip的話,得到的將是錯誤的ip位址。

因此更為安全和準確的方法是盡量避免使用getenv函式。比如可以用以下的函式來獲取ip資訊:

//get the real client ip ("bullet-proof")

function

getip()

apache伺服器下可以用下面這個:

PHP下得到客戶端ip的方法

getenv gets the value of an environment variable stringgetenv string varname returns the value of the environment variable varname,or false on an erro...

php獲得客戶端ip

在php 中使用 server remote addr 來取得客戶端的 ip 位址,但如果客戶端是使用 伺服器來訪問,那取到的就是 伺服器的 ip 位址,而不是真正的客戶端 ip 位址。要想透過 伺服器取得客戶端的真實 ip 位址,就要使用 server http x forwarded for 來...

PHP獲取客戶端IP

在php獲取客戶端ip中常使用 server remote addr 但如果客戶端是使用 伺服器來訪問,那取到的是 伺服器的 ip 位址,而不是真正的客戶端 ip 位址。要想透過 伺服器取得客戶端的真實 ip 位址,就要使用 server http x forwarded for 來讀取。但只有客戶...