在主题文件function.php下添加以下代码即可解决:
( x7 V0 e; h( e. c- /**3 H. k( P4 ]! z5 b/ e6 m8 T. ^
- * 获取用户真实IP8 `4 o( V2 h! @& {. V6 M
- */
/ N) j0 M. V, g# r - function getIP()# c2 A/ A' Z. ]$ F' a; R+ W
- {9 n) k8 d7 W2 G u& ~# m$ G) \
- static $realIP;
6 L) ]* R7 R& S! j; {6 } - if (isset($_SERVER)){
7 g3 \( M" P9 ^) ~ - if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
$ |3 P3 n" L6 c' e6 y9 `# A8 J - $realIP = explode(',', $_SERVER["HTTP_X_FORWARDED_FOR"]);4 ~: f+ ?! u: M- F
- $realIP = $realIP[0];- A' Q+ |- o$ S5 b9 Q
- } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
. i N& a" ?- g - $realIP = $_SERVER["HTTP_CLIENT_IP"];$ z& o$ N) w/ Y# Z; O
- } else {& L& {! a) z- R% d$ r
- $realIP = $_SERVER["REMOTE_ADDR"];
4 {( l* T' L1 ` - }) ^$ B! L0 r( ^5 d2 a U$ z, `
- } else {
, h# x+ o2 Z* Y - if (getenv("HTTP_X_FORWARDED_FOR")){; d0 a. \' {8 r* V& p
- $realIP = getenv("HTTP_X_FORWARDED_FOR");
3 ~! r( x2 D" ?1 b& ~9 I1 c- P - } else if (getenv("HTTP_CLIENT_IP")) {
% F$ T' s. X0 j$ O5 O5 y) z - $realIP = getenv("HTTP_CLIENT_IP");
. X# [$ k2 Y& N P4 f, j* W - } else {
7 e9 C. p0 U F( s7 u - $realIP = getenv("REMOTE_ADDR");
% s' V3 l0 V# Q2 v7 L% w- G - }
0 o! R# i; P/ c# O: `* U - }
7 ], Q7 q5 `4 d! v$ a6 _5 q8 J - $_SERVER['REMOTE_ADDR'] = $realIP;3 A. w J% }; \4 r4 T8 W
- //return $realIP;
6 ^1 X4 I. n. S/ Y6 @' |: R3 v - }+ ~+ \2 u9 N, A. v' }
- add_action( 'init', 'getIP' );
复制代码 4 `3 f) I9 |( c
|