【问题标题】:laravel auth and session not persistinglaravel 身份验证和会话不持久
【发布时间】:2014-10-03 06:50:43
【问题描述】:

我使用的 laravel 会话和身份验证在服务器中存在一些问题,但在 localhost 中工作得很好。我会展示的。

路线

Route::get('/signin', 'PageController@signin');
Route::get('/signup', 'PageController@signup');
Route::get('/terms', 'PageController@terms');
Route::resource('/', 'PageController');
Route::controller('user', 'UserController');

页面控制器

public function index() {
         if (Auth::check()) {
            return View::make('user.index');
        } else {
            return View::make('landing');
        }       
    }

用户控制器

public function postLogin() {           
        $data = array();            
        $secured = ['user_email' => $_POST['email'], 'password' => $_POST['password']];
        if (Auth::attempt($secured, isset($_POST['remember']))) {
            if (Auth::user()->user_status == 1 ) {
                return Redirect::to('/');
            } else {
                $data['success'] = false;
            }
        } else {
            $data['success'] = false;
        }
        return $data;
    }

Auth::check() 即使在登录成功后也会在 pagecontoller 中失败。但是如果我将代码更改为

用户控制器

    public function postLogin() {           
        $data = array();            
        $secured = ['user_email' => $_POST['email'], 'password' => $_POST['password']];
        if (Auth::attempt($secured, isset($_POST['remember']))) {
            if (Auth::user()->user_status == 1 ) {
                return Return View::make(user.index);
            } else {
                $data['success'] = false;
            }
        } else {
            $data['success'] = false;
        }
        return $data;
    }

我得到了索引页面,如果我点击主页的链接,我得到的是登陆页面而不是索引页面。

我想我澄清了我的问题,我已经经历了可能的解决方案,之前以同样的方式回答了问题,但没有任何效果。

我认为不是服务器问题,因为另一个 laravel 应用程序在同一台服务器上运行良好。

请帮忙。

【问题讨论】:

    标签: session authentication laravel


    【解决方案1】:

    您的查询似乎不完整,据我了解,您只需通过一次身份验证检查即可获取索引页面,即使用此方法:

    public function postLogin() {
        $data = array();
        $secured = ['user_email' => $_POST['email'], 'password' => $_POST['password']]; 
       if (Auth::attempt($secured, isset($_POST['remember']))) {
       if (Auth::user()->user_status == 1 ) { 
       return Return View::make(user.index); 
       } 
       else {
       $data['success'] = false; 
       } 
      } 
      else { 
       $data['success'] = false; 
       }
      return $data; 
      }
    

    尝试使用不同的浏览器以确保客户端没有 cookie 存储限制,并检查 app/config/session.php 文件,看看您是否已根据需要配置 HTTPS Only Cookies。

    另外说明这一行“return Return View::make(user.index);”看起来很模糊。

    【讨论】:

    • 您好 Arjun,感谢您的回复,return array( 'driver' => 'file', 'lifetime' => 0, 'expire_on_close' => true, 'files' => storage_path().'/sessions', 'connection' => null, 'table' => 'sessions', 'lottery' => array(2, 100), 'cookie' => 'laravel_session', 'path' => '/', 'domain' => null, 'secure' => false, ); 这是 session.php 文件。正如您所说,如果我重定向到 '/' , pageController 中的索引乐趣会确定索引或登录页面。我检查了其他浏览器,但没有运气。
    • @it'sme 改变 'lifetime' => 0
    • 您可以在此处指定您希望会话在过期前保持空闲的分钟数。
    • 会话已设置为 0。这是我的 auth.php 文件return array( 'driver' => 'eloquent', 'model' => 'User', 'table' => 'mcs_users', 'reminder' => array( 'email' => 'emails.auth.reminder', 'table' => 'password_reminders', 'expire' => 60, ), ); 在 localhost 中一切正常,但在服务器中没有。
    • @it'sme,不应该是0,这个值代表时间,单位是分钟。您可以将 15 指定为 15 分钟,依此类推。
    猜你喜欢
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 2014-06-23
    • 2011-06-11
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 2018-06-06
    相关资源
    最近更新 更多