在主题文件function.php下添加以下代码即可解决:* b1 U% q# r; |. t3 q9 N* ~
- /**
, A* P/ i7 K2 z3 Y) w. U - * 获取用户真实IP/ A* d' ?- m4 o$ g! V/ f
- */
n' w. A. k C) v - function getIP()
$ R0 s/ L5 U: {/ d9 M - {
+ n- S8 m, f* M- s5 G - static $realIP;" d/ Y% a& B) _4 U5 d
- if (isset($_SERVER)){$ L' B) j/ E! `' d; ?- r
- if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
; ?8 |! N) |: G* u$ z - $realIP = explode(',', $_SERVER["HTTP_X_FORWARDED_FOR"]);* R: S9 e- q. [6 \# f% y1 r
- $realIP = $realIP[0];
. \( C5 K% h* E - } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
u+ P8 ~0 R3 w+ {. J9 Q. |) Z - $realIP = $_SERVER["HTTP_CLIENT_IP"];
% m1 ^2 l4 E% [ - } else {
/ c0 t' Y8 t( Q6 o - $realIP = $_SERVER["REMOTE_ADDR"];
* g/ i8 r5 S6 H& M! `# |! U: a - }
" D7 F& h. @. \. e& E - } else {
3 C: K# Y: ~* ?" j9 p - if (getenv("HTTP_X_FORWARDED_FOR")){
7 g" k$ w- ]4 _ - $realIP = getenv("HTTP_X_FORWARDED_FOR");
* ^" d; Q9 |4 s! p - } else if (getenv("HTTP_CLIENT_IP")) {) t, v# I$ \; f" W+ l5 V
- $realIP = getenv("HTTP_CLIENT_IP");
) _/ O/ d9 r2 L" V4 L X - } else {
9 ~+ {4 a7 z) ^$ f/ A- a. z. E - $realIP = getenv("REMOTE_ADDR");
# t* ^( D5 M8 Q1 d+ S/ | - }+ q3 F+ Q9 @! l1 ]
- }
3 _8 K# |- Y7 w3 F" [+ h, l, {: ~ - $_SERVER['REMOTE_ADDR'] = $realIP;) [/ F8 z% k0 f) @# M6 x V
- //return $realIP;( ?) g) L' f* t" H) i
- }
! c4 O% L" Z, ^5 f' ^. C - add_action( 'init', 'getIP' );
复制代码
2 D: d5 v! N1 P, g9 G8 L9 o( e |