会话有价值,但无法识别 [英] Session has value but is not recognised
问题描述
我想知道为什么Session
似乎并没有保留它的价值,但实际上却打印出了它的价值?
我打印出Session
的值,但是当我在其他选项卡上访问同一页面时,它无法识别出它的值.
另外,当页面刷新时,Session
变为null ...?
另外我不确定Log Out
按钮中的操作是否正确
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
这就是我的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');
}
}
这里是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
表示uservalid.blade.php
<html>
<header>
<title></title>
</header>
<body>
@if(Session::has('username'))
@yield('content')
@else
nothing to do here
@endif
</body>
</html>
谢谢
推荐答案
我添加了
<?php
$username = Session::get('username');
?>
和
{{ 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 ?
这篇关于会话有价值,但无法识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!