ÔõÑùʹÓÃLaravel¿ò¼Ü»ñÈ¡¿Í»§¶ËIPµØµã
laravelÊÇÒ»¿îÊ¢ÐеÄphp webÓ¦ÓóÌÐò¿ò¼Ü£¬Ëü»ùÓÚmvcģʽ£¬¾ßÓмòÆÓ¶øÓÅÑŵÄÓï·¨¡£ÔÚlaravelÖлñÈ¡¿Í»§¶ËipµØµãÊÇÒ»Ïî³£¼ûµÄʹÃü£¬ÓÉÓÚËü¿ÉÒÔ×ÊÖú¿ª·¢Ö°Ô±Ê¶±ðȪԴ²¢¾ÙÐÐÏà¹ØµÄÊý¾ÝÆÊÎö¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÏÈÈÝÔõÑùʹÓÃlaravel¿ò¼Ü»ñÈ¡¿Í»§¶ËipµØµã¡£
Ò»¡¢Ê¹ÓÃPHPÔÉúÒªÁì»ñÈ¡IPµØµã
ÔÚLaravel¿ò¼ÜÖÐʹÓÃPHPÔÉúÒªÁì»ñÈ¡¿Í»§¶ËIPµØµã£¬²¢²»ÐèÒªÒÀÀµÓÚLaravel×Ô¼ºµÄº¯Êý¡£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄPHPº¯ÊýÀ´»ñÈ¡¿Í»§¶ËIPµØµã£º
function getClientIp() { $ipaddress = ''; if (getenv('HTTP_CLIENT_IP')) $ipaddress = getenv('HTTP_CLIENT_IP'); else if(getenv('HTTP_X_FORWARDED_FOR')) $ipaddress = getenv('HTTP_X_FORWARDED_FOR'); else if(getenv('HTTP_X_FORWARDED')) $ipaddress = getenv('HTTP_X_FORWARDED'); else if(getenv('HTTP_FORWARDED_FOR')) $ipaddress = getenv('HTTP_FORWARDED_FOR'); else if(getenv('HTTP_FORWARDED')) $ipaddress = getenv('HTTP_FORWARDED'); else if(getenv('REMOTE_ADDR')) $ipaddress = getenv('REMOTE_ADDR'); else $ipaddress = 'UNKNOWN'; return $ipaddress; }
µÇ¼ºó¸´ÖÆ
Õâ¶Î´úÂëÖÐÊ×ÏȽç˵ÁËÒ»¸ö±äÁ¿$ipaddress£¬È»ºóʹÓÃPHPÄÚÖõĺ¯Êýgetenv()¼ì²éHTTP¿Í»§¶ËIPÊÇ·ñ±£´æ£¬ÈôÊDZ£´æÔò½«Æ丳¸ø±äÁ¿$ipaddress¡£ÈôÊDz»±£´æHTTP¿Í»§¶ËIP£¬Ôò¼ì²éHTTP_X_FORWARDED_FOR¡¢HTTP_X_FORWARDED¡¢HTTP_FORWARDED_FOR¡¢HTTP_FORWARDED¡¢REMOTE_ADDRµÈ±äÁ¿£¬Ö±µ½ÕÒµ½IPµØµã»òÕ߸³ÖµÎª’UNKNOWN’¡£
¶þ¡¢Ê¹ÓÃLaravelµÄRequestÀà»ñÈ¡IPµØµã
LaravelÌṩÁËÒ»¸öRequestÀ࣬¿ÉÒÔÔÚ¿ØÖÆÆ÷ÖÐÇáËɵػñÈ¡¿Í»§¶ËIPµØµã¡£¸ÃRequestÀàÌṩÁËÁ½ÖÖ»ñÈ¡IPµÄÒªÁ죺ip()ºÍgetClientIp()¡£ÏÂÃæÊÇÕâÁ½ÖÖ·½·¨µÄ˵Ã÷£º
ʹÓÃip()ÒªÁìÀ´»ñÈ¡¿Í»§¶ËIPµØµã£º
public function index(Request $request) { $ip = $request->ip(); return view('welcome')->with('ip', $ip); }
µÇ¼ºó¸´ÖÆ
ʹÓÃgetClientIp()ÒªÁìÀ´»ñÈ¡¿Í»§¶ËIPµØµã£º
public function index(Request $request) { $ip = $request->getClientIp(); return view('welcome')->with('ip', $ip); }
µÇ¼ºó¸´ÖÆ
ÕâÁ½ÖÖÒªÁ춼¿ÉÒÔºÜÊÇÇáËɵػñÈ¡¿Í»§¶ËIPµØµã¡£ÔÚÕâÁ½¸öÒªÁìÖУ¬$request²ÎÊý´ú±íHTTPÇëÇ󹤾ߣ¬ÔÚLaravel¿ò¼ÜÖкܳ£¼û¡£
Èý¡¢Ê¹ÓÃLaravelµÄ¸¨Öúº¯Êý»ñÈ¡IPµØµã
LaravelÌṩÁËһϵÁи¨Öúº¯Êý£¬ÕâЩº¯Êý¿ÉÒÔ¸üÀû±ãµØÓëLaravel¿ò¼Ü¼¯³É¡£LaravelµÄ¸¨Öúº¯ÊýÊÇÈ«¾Ö¿ÉÓõģ¬Òò´ËÄú¿ÉÒÔÖ±½ÓÔÚ¿ØÖÆÆ÷»òÊÓͼÖÐʹÓÃËüÃÇ¡£ÏÂÃæÊÇʹÓÃLaravel¸¨Öúº¯Êý»ñÈ¡¿Í»§¶ËIPµÄʾÀý£º
ʹÓÃrequest()->ip()ÒªÁì»ñÈ¡¿Í»§¶ËIPµØµã£º
public function index() { $ip = request()->ip(); return view('welcome')->with('ip', $ip); }
µÇ¼ºó¸´ÖÆ
ʹÓÃrequest()->getClientIp()ÒªÁì»ñÈ¡¿Í»§¶ËIPµØµã£º
public function index() { $ip = request()->getClientIp(); return view('welcome')->with('ip', $ip); }
µÇ¼ºó¸´ÖÆ
ÕâÁ½¸öº¯Êý¿´ÆðÀ´ºÜÊǼòÆÓ£¬µ«ËüÃÇ¿ÉÒÔÔÚLaravel¿ò¼ÜÖкÜÊÇÀû±ãµØ»ñÈ¡¿Í»§¶ËIPµØµã¡£
×ܽ᣺
ÔÚʹÓÃLaravel¿ò¼Ü¾ÙÐÐWebÓ¦ÓóÌÐò¿ª·¢Ê±£¬»ñÈ¡¿Í»§¶ËIPµØµãÊÇÒ»Ïî¼òÆÓµ«Ö÷ÒªµÄʹÃü¡£±¾ÎÄÏÈÈÝÁ˶àÖÖÒªÁìÓÃÓÚ»ñÈ¡¿Í»§¶ËIPµØµã£¬°üÀ¨PHPÔÉúÒªÁì¡¢LaravelµÄRequestÀàºÍLaravelµÄ¸¨Öúº¯ÊýµÈ¡£ÎÞÂÛÄúʹÓÃÄÄÖÖÒªÁ죬ȷ±£ÄúµÄ´úÂëÇå¾²²¢Ê¼ÖÕ¶Ô»ñÈ¡µÄIPµØµã¾ÙÐÐÑéÖ¤ºÍ¹ýÂË¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃLaravel¿ò¼Ü»ñÈ¡¿Í»§¶ËIPµØµãµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡