0

in my login form i would like to set check session timeout for filling form, after fill form, i want to check long time, if session expired form must be recreate to fill by user, i'm using this method but i get error:

My code:

public function checkAccount(CheckAuthenticationRequest $request)
{

    if ((time() - Session::activity()) > (Config::get('session.lifetime') * 60))
    {
        return redirect()->back()
            ->withErrors('Login form no longer fill, you must be fill again')
            ->withInput();
    }

    ...
}

Error: for use above code

call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Session\Store' does not have a method 'activity'

Error: if my form no longer fill by user and try to send form data

TokenMismatchException
  • Are you sure that you're importing the right class at the top? – Joel Hinz Apr 13 '16 at 11:21
  • 1
    Laravel's session store has no `activity` method, also you can't reliably determine when a session has expired because Laravel could of had already replaced the old session with a new one. So a practical way of handling this would be to add a session key, let's call it `is_active`, and then checking if that key exists `if (Session::has('is_active')) { ... }`. If the `is_active` key is still there it means the session has not expired, if the key is missing then it means the session expired and removed that key (and you'd need to put it back). – Bogdan Apr 13 '16 at 11:23
  • @JoelHinz yes sir. i wrote `use Session;` top of my class –  Apr 13 '16 at 11:23
  • @Bogdan unfortunately if my form no longer fill by user. i get `TokenMismatchException` error, and i must be handle it. –  Apr 13 '16 at 11:25
  • You can use Laravel's [Exception Handler](https://laravel.com/docs/5.2/errors#the-exception-handler) if you want to manually handle some exceptions at a global level. So you can this can apply this to `TokenMismatchException`, and if the exception is thrown then in `render` method of the handler class just `return redirect()->back()->withInput();` and you're done. – Bogdan Apr 13 '16 at 11:31
  • I think I found the page you got that code from: http://stackoverflow.com/questions/14688853/check-for-session-timeout-in-laravel - if you read the comments, it appears the `activity()` method was removed in Laravel 4. – Joel Hinz Apr 13 '16 at 11:31
  • 2
    @JoelHinz is right, `activity` is way back from Laravel 3 :). I've tagged that question properly so this doesn't happen to other people. – Bogdan Apr 13 '16 at 11:35
  • @Bogdan and how to check that in my custom function? –  Apr 13 '16 at 11:50
  • Just use this condition `if ($e instanceof TokenMismatchException) { return redirect()->back()->withInput(); }}`. – Bogdan Apr 13 '16 at 12:39

2 Answers2

0

The error states that Session doesn't have the method you are calling. Try with Session::get('lastActivityTime')

Implement lastActivityTime by setting the session like so: Session::set('lastActivityTime', time())

Norgul
  • 4,613
  • 13
  • 61
  • 144
  • You might want to explain where that `lastActivityTime` session key is coming from, because Laravel has no such thing. – Bogdan Apr 13 '16 at 11:19
  • You should implement it on your own after each request: `Session::set('lastActivityTime', time())` – Norgul Apr 13 '16 at 11:48
  • Then please include that in the answer so people know what you're referring to, instead of having to guess. – Bogdan Apr 13 '16 at 12:35
0

You can do something like that.

class formController extends Controller{

    /**FUNCTION THAT RETURNS THE FORM VIEW**/
    public function showForm(Request $request) {
        $request->session()->put("opendate", Carbon::now()); //put current timestamp in the session

        return view("myform");
    }

    /**FUNCTION THAT HANDLES THE FORM DATA**/
    public function public function checkAccount(Request $request){
        if(!$request->session()->has("opendate")) return abort(400);

        $open_date = $request->session()->pull("opendate");

        //check the difference between data in the session and now
        if($opendate->diffInMinutes(Carbon::now()) > 10) //<-- expire minutes here) 
            return redirect()->back()
                            ->withErrors("Login form no longer fill, you must be fill again")
                            ->withInput();

        //success code here
    }

}
Claudio King
  • 1,606
  • 1
  • 10
  • 12