3

I login and then keep the username in a session, then I refresh this page, but the session not keep value.

This is my code:

class WelcomeCtrl extends Controller
{
     public function gotoWelcome()
     {   
         return view('welcome')->with('user',Session::get('user'));//nothing in session
     }

        public function dologin(){
                $username = $_POST['username'];
                $password = $_POST['password'];
                $result = "";
                if($username && $password){
                    $result = User::getUser($username, $password);
                    if($result){
                        Session::set('user',$result);
                        return response()->json([true, Session::get('user')]);//I get value expected, this is ok.
                    }
                }
                return response()->json([false, Session::get('user')]);     
            }
    }

4 Answers 4

3

Have you added the middleware group 'web' to your routes. In 5.1 , this was default for all the routes and was used for session management and csrf verification etc.

Sign up to request clarification or add additional context in comments.

4 Comments

Oh, I think I not added the middleware.
Keep in mind that recent versions of Laravel 5.2 now include the web middleware by default in the routing service provider, so no need to do this if you are running one of the new versions, such as 5.2.30.
I added the middleware like this: Route::group(['middleware' => ['web']], function () { Route::post('dologin','WelcomeCtrl@dologin'); Route::get('gotoWelcome','WelcomeCtrl@gotoWelcome'); } but when I login with $.post of jquery, it say: "TokenMismatchException in VerifyCsrfToken.php line 67: in VerifyCsrfToken.php line 67 at VerifyCsrfToken->handle(object(Request), object(Closure)) at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124 at Pipeline->Illuminate\Pipeline\{closure}... am I miss something?
I put all controller in app\Http\controllers\ [all my file controller here]. is it ok with middleware => web
2

If use want to use Session:: make sure you add session Facade by:

use Session;

Instead of this you can use session helper. So you can use below codes:

session()->user()->email;
session()->get('user');
session()->flash('message', 'Hi there.');

For more see helpers.

Comments

0
Session::put('key','value');//to put the session value
Session::get('key');//to get the session value

Session::has('key');//to check the session variable exist or not

Comments

0

Remember one thing. When you post a form using ajax in Laravel 5.2, there will be a CsrfTokenMismatch error. To avoid this, add your route name which is accepting form values with ajax in your App/Http/Middleware/VerifyCsrfToken.php in $except array. Then that route will accept form values with ajax request.

1 Comment

you can also send csrf token through ajax request @chipcoiga

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.