2016-12-24 22 views
1

我是新的laravelLaravel,會議只爲1個在線用戶

我編寫了一個腳本,許多使用者

,但問題是我有工作,工作是這樣的:

時一個喜歡「Helen」的用戶登錄她可以看到她的個人資料 ,但是如果接下來另一個用戶喜歡「Maria」登錄,Marias面板將顯示他們兩人

我認爲這意味着只有一個會話可以在同一時間和會話w的價值不適合最新的用戶 而舊用戶會話不會過期只是會話中的值將被更改,因此她將其標識爲另一個用戶,並且可以看到該用戶配置文件以及用戶註銷時由於關閉的會話中,所有用戶都將被註銷。 這裏是我的簡單的代碼:

public function Login(){ 
     $this->Token(); 
     $pack=Input::all(); 
     try { 
      $result=DB::table('user')->where('Email','=',$pack['email'])->get(); 
      if (Hash::check($pack['password'], $result[0]->Password)){ 
       session(['there' => $result['0']->Email]); 
       return redirect('dashboard'); 
      } 
      return redirect('dashboard')->with('does','wrong password'); 
     }catch(Exception $e){ 
      return redirect('dashboard')->with('does',.$e); 
     } 
} 

public function UserType() { 
     if(!session('there')) 
      return "Not Logged"; 
     else { 
      $result = DB::table('user')->where('Email', '=', session('there'))->get(); 

     if($result!=null) 
      return "User"; 
} 

public function ShowDashboard(){ 
     if($this->UserType()=="Not Logged") 
     else 
      return view('pages/dashboard'); 
} 
+0

東西似乎關閉;你爲什麼完全避開Laravel的內置認證?我在您的方法中發現了一些可能返回未知值的邏輯漏洞。 –

+0

返回值是真的...我試着dd(會話)和返回的值是真的 – Pedramch

回答

1

我不知道你爲什麼session()管理用戶登錄...此外,他們依賴了很多,用戶從同一臺計算機上,相同的瀏覽器登錄的情況.. 。cookies ...等等等等......也許這就是爲什麼你可能同時得到2個不同的會話值...

無論如何..請嘗試使用Laravel的預定義函數Auth來處理您的登錄/註銷程序。

public function Login() 
{ 
    // What does this do? Check for a CSRF token? If yes, then 
    // please understand then Laravel automatically checks 
    // for the CSRF token on POST/PUT requests and therefore 
    // there is no special need to use the below function... 
    $this->Token(); 

    $pack = request()->only(['email', 'password']); 

    // I don't really feel try catch is required here... but completely your choice... 
    try { 
    if(auth()->attempt($pack)) { 
     return redirect('dashboard') 
    } 
    return redirect->back()->with('does', 'wrong password'); 
    } catch(Exception $e) { 
    return redirect->back()->with('does', $e); 
    } 
} 


public function ShowDashboard() 
{ 
    // You can remove this if/else by adding the 'auth' middleware 
    // to this route 
    if(!auth()->check()) 
    return view('pages.dashboard'); 
    else 
    return redirect(route('login')); 
} 

我發現了很多在上面的代碼中的問題...

  1. 請使用駝峯命名功能...(我還沒有在我的代碼改變了命名,因爲以上我真的不知道你在你的工作場所遵循什麼規則或idk ...)
  2. 不返回字符串爲一個簡單的真/假的情況。
  3. 請儘量使用Models。原始的DB命令是非常複雜和廣泛的查詢所必需的
+0

非常感謝有用的提示 – Pedramch