【问题标题】:Can't prevent users going back to the admin page after logging out - Laravel 4注销后无法阻止用户返回管理页面 - Laravel 4
【发布时间】:2014-01-18 10:27:15
【问题描述】:

在使用 Laravel 4 开发一个非常基本的应用程序时,我遇到了一个奇怪的问题。我根据 laravel 4 的用户身份验证文档的文档创建了该功能,用户可以在其中注册以提出不同的问题,并且只有注册用户才能访问到秘密/管理页面。到目前为止一切正常,除了我遇到了这个奇怪的问题,即使在注销后用户仍然可以访问宝贵的页面,或者换句话说,管理页面。我不确定它与 Laravel 有什么关系,但我仍然无法弄清楚问题出在哪里以及如何防止或强制浏览器重新加载或其他什么东西,这样即使他们点击也看不到管理页面浏览器中的后退箭头。 尽管它可能无关紧要,但我仍在附上我拥有的代码。在 routes.php 我有这个

<?php

Route::get('/', array('as'=>'home', 'uses'=>'QuestionController@getindex'));
Route::get('register', array('as'=>'register', 'uses'=>'UserController@getnew'));
Route::get('login', array('as'=>'login', 'uses'=>'UserController@getlogin'));
Route::get('logout', array('as'=>'logout', 'uses'=>'UserController@getlogout'));

Route::post('register', array('before'=>'csrf', 'uses'=>'UserController@postcreate'));
Route::post('login', array('before'=>'csrf', 'uses'=>'UserController@postlogin'));

在 userController 我有这个

<?php

class UserController extends BaseController {

    public function getNew() {

        return View::make('users.new')
            ->with('title', 'Snappy Q&A - Register');
    }

    public function postCreate() {

        $validator = Member::validate(Input::all());

        if ( $validator->passes() ) {
            $user = User::create( array (
                'username' => Input::get('username'),
                'password' => Hash::make(Input::get('password'))
            ));

            Auth::login($user);

            return Redirect::route('home')->with('message', 'Thanks for registering!');
        } else {

            return Redirect::route('register')->withErrors($validator)->withInput();
        }
    }

    public function getLogin() {

        return View::make('users.login')
            ->with('title', 'Snappy Q&A - Login');
    }

    public function postLogin() {

        $user_creds = array(
            'username' => Input::get('username'),
            'password' => Input::get('password')
        );

        if( Auth::attempt($user_creds) ) {

            return Redirect::route('home')
                ->with('message', 'Yep, you are now logged in');
        } else {

            return Redirect::route('login')
                ->with('message', 'Shit man! The creds are not authorised!')
                ->withInput();
        }
    }

    public function getLogout() {

        if( Auth::check() ) {

            Auth::logout();
            return Redirect::route('login')
                ->with('message', 'You are now logged out!');
        } else {

            return Redirect::route('home');
        }
    }

}

这个new.blade.php负责创建一个新用户

@extends('master.master')

@section('content')

    @if( $errors->has() )
        <p>The following erros has occured: </p>

        <ul class="form-errors">
            {{ $errors->first('username', '<li>:message</li>') }}
            {{ $errors->first('password', '<li>:message</li>') }}
            {{ $errors->first('password_confirmation', '<li>:message</li>') }}
        </ul>
    @endif

    {{ Form::open( array('route'=>'register', 'method'=>'post')) }}

        {{ Form::token() }}

        {{ Form::label('username', 'Username') }}
        {{ Form::text('username', Input::old('username')) }}

        {{ Form::label('password', 'Password') }}
        {{ Form::password('password') }}

        {{ Form::label('password_confirmation', 'Confirm Password') }}
        {{ Form::password('password_confirmation') }}

        {{ Form::submit('Register', array('class'=>'btn btn-success')) }}

    {{ Form::close() }}
@stop

这个是用来登录用户的:

@extends('master.master')

@section('content')

    {{ Form::open( array('route'=>'login', 'method'=>'post') ) }}

        {{ Form::token() }}

        {{ Form::label('username', 'Username') }}
        {{ Form::text('username', Input::old('username')) }}

        {{ Form::label('password', 'Password') }}
        {{ Form::password('password') }}

        {{ Form::submit('Login', array('class' => 'btn btn-success')) }}

    {{ Form::close() }}

@stop

为了清楚起见,除了使用 Auth::check(); 方法的导航条件之外,我对管理页面没有任何特别之处。确保只有登录用户才能看到注销导航。完成此问题后,我将稍后创建该功能。管理页面视图将位于名为 questions 的文件夹中。我希望这现在有意义。

我该如何处理?请询问您是否还需要我的代码的任何其他实例。而且我相信 Laravel 世界中的许多新手在开发这样的功能时或多或少都面临着同样的问题。我希望这是一个很好的问题,并且会帮助其他人和我。

【问题讨论】:

  • 请也添加您的秘密/管理路线。
  • 哪些路由需要认证?我只看到登录/注册页面的公共路线。还是主页是管理页面?
  • 你的管理员路由是哪一个?
  • 我已经修改了线程并包含了视图文件。请看一下。管理页面现在是空的,只有一个文本“管理页面”和导航中的轻微变化,其中有退出选项,请注意一切正常,除了即使使用退格按钮退出后用户仍然可以访问的问题页。请继续检查我更新的线程。
  • 试试这个:在某处添加这个代码 if (Auth::check()) die('you are logged in') 现在登录并转到这条路线,你需要看到'你已登录'。现在注销并返回同一页面。您看到“您已登录”吗?

标签: php authentication laravel laravel-4


【解决方案1】:

尝试在路由中使用过滤器

Route::get('/', array('before' => 'auth', function()
{

}));

更多: http://laravel.com/docs/routing#route-filters

【讨论】:

  • 我试过了。正如我已经提到的身份验证方法的工作,但只是当他们点击退格按钮时浏览器不会自动刷新。注意:虽然他们可以看到页面,但他们不能做任何事情,刷新后他们不再看到注销选项。我想我只需要找到一种方法,即使他们返回浏览器也会自动刷新或其他东西。我不确定如何处理这个问题:(
  • 我认为问题在于导航器的缓存...您可以将标头发送到无缓存。 codeaid.net/php/send-no-cache-headers
猜你喜欢
  • 1970-01-01
  • 2016-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-26
  • 2018-04-12
  • 2011-10-03
  • 2015-01-06
相关资源
最近更新 更多