×ðÁú¿­Ê±¹ÙÍøµÇ¼

ÔõÑùʹÓÃ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µØµãµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ