Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mrestufp20's avatar

Session Problem in Laravel 4

I have a problem when I login but not go to the main page, but instead to the login page again? How to fix it?

this my LoginController

/**

  • Login Controller For Admin

  • Darana S.V (http://www.webstatis.com) */ class LoginController extends BaseController { function __construct() { $this->getCurl = new getCurl; }

    function index() { return View::make('login'); }

    function dologin() { $username = Input::get('username'); $password = Input::get('password');

      $data_string = array(
          'username' => $username,
          'password' => $password
      );
    
      $getencode = json_encode($data_string); 
    
      $url = Config::get('app.api_url').'public/auth';
      $data = $this->getCurl->post($url,$getencode);
      $rest = json_decode($data);
      
      if ($rest->status==true && $rest != null) {
          Session::put('logged',true);
          Session::put('user_id',$rest->data->user->id);
          Session::put('token',$rest->data->login->token);
          Session::put('role_id',$rest->data->user->role_id);
          Session::put('username',$rest->data->user->username);
          Session::put('role_name',$rest->data->user->role_name);
          Session::put('fullname',$rest->data->user->fullname);                                               
          return Redirect::to('/');
      } else {
          return Redirect::to('/login?error');
      }
    

    }

    function logout() { Session::flush(); return Redirect::to('/login'); }

// END }

and this my filters

/* |-------------------------------------------------------------------------- | Application & Route Filters |-------------------------------------------------------------------------- | | Below you will find the "before" and "after" events for the application | which may be used to do any work before or after a request into your | application. Here you may also register your custom route filters. | */

App::before(function($request) { // });

App::after(function($request, $response) { // });

/* |-------------------------------------------------------------------------- | Authentication Filters |-------------------------------------------------------------------------- | | The following filters are used to verify that the user of the current | session is logged into this application. The "basic" filter easily | integrates HTTP Basic authentication for quick, simple checking. | */

Route::filter('auth', function() { if (Auth::guest()) { if (Request::ajax()) { return Response::make('Unauthorized', 401); } else { return Redirect::guest('login'); } } });

Route::filter('auth.basic', function() { return Auth::basic(); });

/* |-------------------------------------------------------------------------- | Guest Filter |-------------------------------------------------------------------------- | | The "guest" filter is the counterpart of the authentication filters as | it simply checks that the current user is not logged in. A redirect | response will be issued if they are, which you may freely change. | */

Route::filter('guest', function() { if (Auth::check()) return Redirect::to('/'); });

/* |-------------------------------------------------------------------------- | CSRF Protection Filter |-------------------------------------------------------------------------- | | The CSRF filter is responsible for protecting your application against | cross-site request forgery attacks. If this special token in a user | session does not match the one given in this request, we'll bail. | */

Route::filter('csrf', function() { if (Session::token() != Input::get('_token')) { throw new Illuminate\Session\TokenMismatchException; } });

Route::filter('userAuth', function() { if (Session::has('logged')==false) { return Redirect::to('/login'); } });

0 likes
0 replies

Please or to participate in this conversation.