在主题文件function.php下添加以下代码即可解决:
7 F) W1 N7 C* o9 m- /** @2 X1 ]; w* e5 ?4 ]5 _' C
- * 获取用户真实IP7 T* g) o* f+ k8 z4 e
- */' d0 w8 ~+ ]2 K' i0 O4 {
- function getIP()
* ~% d+ ?$ `; v" a4 ~3 A - {- e" u& ~0 d6 Y9 m u& U, H- n5 P
- static $realIP; o7 o1 T, m5 ]+ {: i9 j' s" d
- if (isset($_SERVER)){
. l: o2 c7 n6 I2 R) h/ L - if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
' n3 q; k& j' S - $realIP = explode(',', $_SERVER["HTTP_X_FORWARDED_FOR"]);$ K6 M! W) T/ P- k: Q) z% N
- $realIP = $realIP[0];
! Z, f( v& u8 q. T3 \6 k - } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
; o. ~" ^3 T0 c3 d - $realIP = $_SERVER["HTTP_CLIENT_IP"];4 V8 ~# y E: S" S) n4 F( D
- } else {
; z6 w, G* }# y5 ^ - $realIP = $_SERVER["REMOTE_ADDR"];
$ P& o9 u" M' X6 e, o. b- S - }7 L) n/ r7 a6 N9 t0 U0 p
- } else { j& ]% e" `7 E
- if (getenv("HTTP_X_FORWARDED_FOR")){5 d$ W, b1 j( O: i+ U
- $realIP = getenv("HTTP_X_FORWARDED_FOR");
1 u$ P8 E7 W) F# I0 {" r9 @ - } else if (getenv("HTTP_CLIENT_IP")) {
/ D# W! `: C/ b* T, s - $realIP = getenv("HTTP_CLIENT_IP");! a3 w; V# S# _* l
- } else {+ Z5 j8 { \& s# k" [- i8 V& n1 c
- $realIP = getenv("REMOTE_ADDR");& x6 j, u, ~5 J3 Y- h% i5 E
- }
3 A4 O1 T( W9 F, X! Q3 D/ z6 M - }
0 l5 }9 _. `* w6 U$ k1 D7 { - $_SERVER['REMOTE_ADDR'] = $realIP;
% o% Q8 \0 ?& u! S/ X - //return $realIP;* Q3 k- m! J; p
- }
' K; p2 p: O) M5 N- { - add_action( 'init', 'getIP' );
复制代码
3 R- p+ K2 @4 g9 U I* c" B |