问题
I was wondering why does the Session doesn't seem to remain it's value but it actually prints its value?
I print out the the value of the Session but when I access the same page on a different tab it doesn't recognize that it has a value.
Also when the page is refreshed Session becomes null...?
And another thing I'm not sure if what I did in the Log Out button is correct
This is what I have in my LogInController.php
<?php
class LogInController extends BaseController {
public function actionLogIn() {
$username = Input::get('username');
$password = Input::get('password');
if(($username == 'batman' and $password == '12345') or ($username == 'robin' and $password == '54321')){
Session::put('username', $username);
return Redirect::route('welcome')
->with('username', $username)
->with('title', 'Welcome!');
}else {
Session::flush();
return Redirect::to('login')
->with('asterisk', 'Invalid username or password')
->with('title', 'Login');
}
}
public function actionLogOut() {
Session::flush();
return View::make('login.formlogin')
->with('title', 'Login');
}
}
Here's routes.php
Route::get('/', function()
{ return View::make('hello'); });
Route::get('login', array('as' => 'login', function ()
{ return View::make('login.formlogin')
->with('title', 'Login');
}));
Route::post('login', array('uses'=>'LogInController@actionLogIn'));
Route::get('error', array('uses'=>'LogInController@actionLogOut'));
Route::get('welcome', array('as' => 'welcome', function ()
{ return View::make('login.uservalid'); }));
formlogin.blade.php
@extends('login.login')
@section('content')
<h3>Login</h3>
{{ Form::open(array('method'=>'post')) }}
<div id="asterisk">{{ Session::get('asterisk') }}</div>
{{ Form::label('username', 'Username:') }}
{{ Form::text('username') }}<br/>
{{ Form::label('password', 'Password:') }}
{{ Form::password('password') }}<br/>
{{ Form::submit('Log In') }}
{{ Form::close() }}
@endsection
uservalid.php
@extends('login.layouts')
@section('content')
{{ Form::open(array('method'=>'get')) }}
welcome
{{ Session::get('username') }}
<br/>
{{ Form::submit('Log Out') }}
{{ Form::close(0) }}
@endsection
layouts.blade.php for uservalid.blade.php
<html>
<header>
<title></title>
</header>
<body>
@if(Session::has('username'))
@yield('content')
@else
nothing to do here
@endif
</body>
</html>
thanks,
回答1:
I added
<?php
$username = Session::get('username');
?>
and
{{ Session::flash('username', $username) }}
in uservalid.php
so when page is refreshed it display the same page (which is correct) and also when page is open in new window, it doesn't log out or anything now unlike before
it kinda seem iffy to me though .. any suggestion/s ?
回答2:
Really, you should probably be using the Auth class for something like this, you'll find it handles all the session-related authentication stuff for you.
来源:https://stackoverflow.com/questions/18571443/session-has-value-but-is-not-recognised