2017-10-03 11 views
0

함수는 $ request-> ip()를 한 번 호출하고 필요할 때마다 함수를 호출하지 않도록 ip를 상수에 넣으 려합니다. 따라서 리소스 사용은 페이지에서 함수를 10 번 호출하지 않기 때문에 감소합니다. 단, 해당 함수에서 한 번 가져온 정의 된 상수 만 반향시킬 수는 있습니다.laravel - 함수를 호출하지 않기 위해 ip에 상수를 정의하십시오.

Laravel에서도 가능합니까? 그렇다면 어떻게?

답변

0

당신은 당신의 LoginControllerlogin() 방법을 덮어 쓰기 Sessions

사용하고 다음과 같이 뭔가를 추가 할 수 있습니다

if(!\Session::has('user_ip')) 
      \Session::put('user_ip',$request->ip()); 

전체 방법 :

/** 
* Handle a login request to the application. 
* 
* @param \Illuminate\Http\Request $request 
* @return \Illuminate\Http\Response 
*/ 
public function login(Request $request) 
{ 
    $this->validateLogin($request); 

    // If the class is using the ThrottlesLogins trait, we can automatically throttle 
    // the login attempts for this application. We'll key this by the username and 
    // the IP address of the client making these requests into this application. 
    if ($this->hasTooManyLoginAttempts($request)) { 
     $this->fireLockoutEvent($request); 

     return $this->sendLockoutResponse($request); 
    } 

    if ($this->attemptLogin($request)) { 
     if(!\Session::has('user_ip')) 
      \Session::put('user_ip',$request->ip()); 
     return $this->sendLoginResponse($request); 
    } 

    // If the login attempt was unsuccessful we will increment the number of attempts 
    // to login and redirect the user back to the login form. Of course, when this 
    // user surpasses their maximum number of attempts they will get locked out. 
    $this->incrementLoginAttempts($request); 

    return $this->sendFailedLoginResponse($request); 
} 

하는 \를 분명히를 \ 가져 기억의 HTTP \ 요청 :

24,이제 사방

+0

이 방법으로 사용자는 vpn/pro * xy로 로그인 할 수 있으며 이후에 그는 자신의 ip를 변경할 수 있습니다. 세션이 ip인지 아닌지 만 확인 했으므로 true를 반환하지만 이전에 설정된 ip가 있습니다. 가짜입니다! 페이지를로드 할 때마다 상수 값을 설정하고 해당 페이지에서 사용할 때마다 확인하고 싶습니다 !! – codepro

+0

그런 다음 공급자에서 수행하고 항상 업데이트하여 논리를 적용하십시오. 당신이 우리에게 완전한 논리를 제공하지 못한다면 우리는 완전한 대답을 얻을 수 없다. – aaron0207

0

내가 $request->ip() 세션 또는 상수를 호출하는 것보다 더 많은 자원을 사용하는 호출 생각하지 않는다 Session::get('user_ip')를 사용할 수 있습니다.

IP는 언제든지 $_SERVER 수퍼 글로벌을 사용하는 모든 PHP 요청에서 액세스 할 수 있으며 $request->ip()은이를위한 래퍼 일뿐입니다.

+0

$ _SESSION 역시 역시 superglobal이다. – aaron0207

+0

각 페이지에서 나는 하나의 호출을 가지고 있고 그것을 설정하면 여러개의 섹션을 가지고있다. 상수는 반드시 함수를 여러 번 호출하는 것보다 적은 리소스를 사용합니다! – codepro

+0

내가 말했던 것처럼 함수를 백만 번 호출하지 않으면 성능 문제가되어서는 안됩니다. 당신이 있다면, 리팩토링 코드 :) –