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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?