在主题文件function.php下添加以下代码即可解决:
# T/ H! g) F% \+ G; d- /**
4 \# J. e; _2 Q: @ - * 获取用户真实IP
3 u( ]% M* N3 g9 H+ V - */8 {& G& a3 W$ g2 w: `
- function getIP()& B& ?) Z6 L9 I4 `2 b5 s0 R
- {- A6 S2 j+ E3 u8 X% q9 H2 |
- static $realIP;. h- p; P# J* r% E
- if (isset($_SERVER)){
9 x. v; m. A0 c# S( n - if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){# g& o0 v$ i& \
- $realIP = explode(',', $_SERVER["HTTP_X_FORWARDED_FOR"]);" r6 l O J) ]9 g
- $realIP = $realIP[0];- ]0 [( M+ x# }: }7 z* o" s2 H
- } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {9 F% i/ y: r2 J3 V: v$ }* g4 I
- $realIP = $_SERVER["HTTP_CLIENT_IP"];' a0 l' x, t0 r( ^1 N4 p' K
- } else {4 e, w' o$ b; k% k! g
- $realIP = $_SERVER["REMOTE_ADDR"];% Q q6 a" b+ t; }* P
- }
% G4 j* c# ~, Y - } else {4 i8 w, o" x. M! p# P
- if (getenv("HTTP_X_FORWARDED_FOR")){5 a2 U2 @; e5 d9 r6 v7 v d. u
- $realIP = getenv("HTTP_X_FORWARDED_FOR");( d& C6 ?; e7 v; g; l I7 P
- } else if (getenv("HTTP_CLIENT_IP")) { r( ?. k3 z4 l
- $realIP = getenv("HTTP_CLIENT_IP");
& N1 l; g: @! u; z0 R7 g - } else {
, L4 a% D1 R) O' p9 }6 D3 \ - $realIP = getenv("REMOTE_ADDR");7 U1 ?& {' A; X: _) I# H, b% ?
- }
5 P' U( \/ U9 I9 t$ u4 V/ e - }
! t8 _: d! Y8 N( d- A2 O- o - $_SERVER['REMOTE_ADDR'] = $realIP;- W# q% \' M0 S5 V. O" W/ z( H
- //return $realIP;
8 O- j0 j2 L D/ T5 o7 P - }
8 ~, B1 @2 `6 Q) ]& D. ` - add_action( 'init', 'getIP' );
复制代码
6 \' k! O# V) M* B, b0 v0 ?: Z* E0 h |