2017-10-03 3 views
0

関数$ request-> ip()を一度呼び出して、ipを定数に入れて、必要なときにいつも関数を呼び出さないようにします。ページ内で関数を10回呼び出すのではなく、その関数から一度だけフェッチされた定義された定数をエコーすることができるため、リソース使用量は減少します。laravel - 関数を呼び出さないようにするためにipの定数を定義する

Laravelでも可能ですか?もしそうなら、どのように? \

/** 
* 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をインポートすることを忘れないでください:

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

詳しい方法:

答えて

0

あなたLoginControllerlogin()方法を上書きし、このような何かを追加Sessions

を使用することができますリクエスト:

use Illuminate\Http\Request; 

今、あなたはどこにでも

+0

この方法でユーザーはvpn/pro * xyでログインすることができます。その後、彼は自分のIPを変更することができます。セッションがIPを持っているかどうかだけチェックしているので、trueを返します。それは偽物です!私は、ページがロードされるたびに定数を値に設定し、そのページで使用するたびにチェックしたいと思います! – codepro

+0

それからプロバイダーでそれを行い、それを常に更新し、あなたのロジックを適用してください。あなたは私たちに完全なロジックを提供していない場合、完全な答えに達することはできません – aaron0207

0

私は$request->ip()セッション、あるいは定数を呼び出すよりも多くのリソースを使用して呼び出すとは思わないSession::get('user_ip')を使用することができます。

IPには、$_SERVERスーパーグローバルを使用するすべてのPHPリクエストで常にアクセスできます。$request->ip()は、そのためのラッパーに過ぎません。

+0

$ _SESSIONも超グローバルです – aaron0207

+0

各ページでは、私は1つの呼び出しを持って、定数は必ず関数を複数回呼び出すよりもリソースを少なくします! – codepro

+0

私が言ったように、何百万回も関数を呼び出さなければ、パフォーマンスの問題ではありません。あなたがいるなら、あなたのコードをリファクタリングしてください:) –

関連する問題