dongpo9071 2014-12-29 07:31
浏览 42
已采纳

重定向循环错误laravel auth

hi im trying to make a good user auth form but i ve have had some problems , so first i tried this :

Route::get('login', function()
{
    /* Get the login form data using the 'Input' class */
    $userdata = array(
        'username' => Input::get('username'),
        'password' => Input::get('password')
    );

    /* Try to authenticate the credentials */
    if(Auth::attempt($userdata))
    {
        // we are now logged in, go to admin
        return Redirect::to('home');
    }
    else
    {
        return Redirect::to('login');
    }
});

and this is the form blade page :

     {{ Form::open(array('url' => 'login', 'class' => 'form-horizontal')) }}

<div class="control-group">
  <label class="control-label" for="username"></label>
  <div class="controls">
    <input id="username" name="username" type="text" placeholder="" class="input-xlarge" required="">

  </div>
</div>

<!-- Password input-->
<div class="control-group">
  <label class="control-label" for="password"></label>
  <div class="controls">
    <input id="password" name="password" type="password" placeholder="" class="input-xlarge" required="">

  </div>
</div>

<!-- Button -->
<div class="control-group">
  <label class="control-label" for="submit"></label>
  <div class="controls">
    <button id="submit" name="submit" class="btn btn-inverse"></button>
  </div>
</div>

</fieldset>

and it gave me the redirect loop error

and then i tried this :

Route::get('login', function()
{
    /* Get the login form data using the 'Input' class */
    $user = new User;
    $log = array(
    $user -> username =  Input::get('username'),
    $user -> password =  Input::get('password')
    );



    /* Try to authenticate the credentials */
    if(Auth::attempt($log))
    {
        // we are now logged in, go to admin
        return Redirect::to('home');
    }
    else
    {
        return Redirect::to('login');
    }
});

and it gives me :

SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where clause' (SQL: select * from `users` where `0` is null and `1` is null limit 1)

does anyone knows whats the problem? first code should have worked perfectly but why redirect error?

  • 写回答

2条回答 默认 最新

  • dongliushui2001 2014-12-29 07:43
    关注

    Firstfully, change your route names, to: Route::get('login', function(){}); And Route::get('sign-in', function(){});

    Get auth page:

    Route::get('login', function()
    {
       return View::make('your-auth-view');
    }
    

    And your sign-in handler should looks like:

    Route::get('sign-in', function(){
        $userdata = array(
            'username' => Input::get('username'),
            'password' => Input::get('password')
        );
    
         if(Auth::attempt($userdata))
        {
            // we are now logged in, go to admin
            return Redirect::to('home');
        }
        else
        {
            return Redirect::to('login');
        }
    });
    

    And form will change to:

    {{ Form::open(array('url' => 'sign-in', 'class' => 'form-horizontal')) }}
    

    P.S. You have a redirect loop, because, you have a two the same routes, and when you submit the form you redirect to login page again and again

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探