在主题文件function.php下添加以下代码即可解决:' T$ I* T, c8 y% P, Z6 G. T8 D6 @' R
- /**4 [# K/ P; e- G8 u
- * 获取用户真实IP
: E1 X0 Z, X1 x7 @ - */
( p" O& N+ O1 v: Q - function getIP()
2 V! s# F; ^* e9 Z6 R5 ] - {1 U. F6 `# Z4 k# J
- static $realIP;# g6 W2 H3 h2 c b" a C
- if (isset($_SERVER)){6 p K \8 S% q) z
- if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){4 K. h# p0 m( G& I& ]/ V% j
- $realIP = explode(',', $_SERVER["HTTP_X_FORWARDED_FOR"]);
" l) g; i5 L t* ]+ p - $realIP = $realIP[0];
1 W- \& ]3 W- l# m4 m - } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {- ?# g" U" q' f6 b( Y! K+ Q
- $realIP = $_SERVER["HTTP_CLIENT_IP"];
0 J) I J5 t' V {% k' \& [ - } else {
) p7 M4 Y0 U9 n# T' O; l. Y8 D - $realIP = $_SERVER["REMOTE_ADDR"];
8 S; H, N7 |8 z, h+ z - }
" D- {! J# j/ q* `+ q - } else {5 ?6 l4 k0 l! ?# O& D$ ~
- if (getenv("HTTP_X_FORWARDED_FOR")){
* D" y! `" }9 `1 H$ G# ~# r4 y: V" A - $realIP = getenv("HTTP_X_FORWARDED_FOR");1 S! D% G0 x. t: m& M( q
- } else if (getenv("HTTP_CLIENT_IP")) {
8 J% A5 V" p8 n3 m( A% P( t - $realIP = getenv("HTTP_CLIENT_IP");
& h& P/ e# N6 E: p - } else {
: X$ T9 b/ J4 B' t. F; _! ~- K9 W - $realIP = getenv("REMOTE_ADDR");
( j- E' q+ C" |" m% |8 n4 k4 a1 g - }: n& X3 {- B. J4 U8 M
- }2 J2 c; @! X. w5 s2 D$ M6 z1 S
- $_SERVER['REMOTE_ADDR'] = $realIP;% ^# W& l3 t# `9 A
- //return $realIP;3 W3 ]$ j6 {7 E) D4 F* c4 K9 n4 E) Y
- }8 j4 @/ T1 o8 o4 S' F8 Z
- add_action( 'init', 'getIP' );
复制代码
. a- } s1 _, I4 I6 s, J, q |