当我通过ajax-login方法名称(signinuser)创建会话时,我正在使用laravel 5.1创建会话。我可以放置并获得Session。 另一方面当我尝试从同一控制器的anther方法(userprofile)获取会话数据时,会话数据消失
在我的控制器下面
<?php
namespace App\Http\Controllers;
use DB;
use Auth;
Use Session;
Use View;
Use Crypt;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class HomeController extends Controller {
/*
|--------------------------------------------------------------------------
| Home Controller
|--------------------------------------------------------------------------
|
| This controller renders your application's "dashboard" for users that
| are authenticated. Of course, you are free to change or remove the
| controller as you wish. It is just here to get your app started!
|
*/
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
//$this->middleware('auth');
}
/**
* Show the application dashboard to the user.
*
* @return Response
*/
public function index()
{
return view('index');
}
public function signupuser(){
$data = json_decode(file_get_contents("php://input"));
$usrname = ($data->uname);
$upswd = ($data->pswd);
$uemail = ($data->email);
$validUser = true;
$userEmail = DB::table('users')->where('email', $uemail)->first();
if(empty($userEmail)){
$userName = DB::table('users')->where('username', $usrname)->first();
if(!empty($userName)){
$arr = array('msg' => "", 'error' => 'User Already exists with same username','action'=>'exist');
$validUser =false;
}
}else{
$arr = array('msg' => "", 'error' => 'User Already exists with same email','action'=>'exist');
$validUser =false;
}
if ($validUser) {
$encrypted = md5($upswd);
$id = DB::table('users')->insertGetId(
[
'username' => $usrname,
'email' => $uemail,
'password' => $encrypted,
'last_login' => date('Y-m-d H:i:s'),
'created_at' => date('Y-m-d H:i:s')
]
);
if ($id) {
$sql ="select * from users where id=$id";
$users = DB::select($sql);
Session::put('userinfo',$users[0]);
$arr = array('msg' => "User Created Successfully!!!", 'error' => '','action'=>'insert');
} else {
$arr = array('msg' => "", 'error' => 'Error In inserting record','action'=>'error in inserting');
}
}
$jsn = json_encode($arr);
print_r($jsn);
exit;
}
public function signinuser(){
$data = json_decode(file_get_contents("php://input"));
$usrname = ($data->uname);
$upswd = ($data->pswd);
$pwd = md5($upswd);
$sql ="select * from users where password='$pwd' AND (email='$usrname' OR username='$usrname')";
$users = DB::select($sql);
if(empty($users)){
$arr = array('msg' => "", 'error' => 'Please enter valid username or email.','action'=>'error');
}else{
Session::put('userinfo',$users[0]);
DB::table('users')
->where('id', $users[0]->id)
->update(['last_login' => date('Y-m-d H:i:s')]);
$arr = array('msg' => "Login successfully.", 'error' => '','action'=>'success');
}
$sessiondata = Session::all();
echo '<pre>==='; print_r($sessiondata); exit;
$jsn = json_encode($arr);
print_r($jsn);
exit;
}
public function userprofile(){
Session::put('key', 'pankaj');
$value = Session::get('key');
$sessiondata = Session::all();
echo '<pre>==='.$value; print_r($sessiondata); exit;
return View::make('profile', $data);
}
}
?>
signinuser方法(ajax调用)响应
Array
(
[_token] => cOCcWvyy8Z93W36rpaNNjU4p2cmC2l5t75WT0LaH
[_previous] => Array
(
[url] => http://laravel.example.com
)
[flash] => Array
(
[old] => Array
(
)
[new] => Array
(
)
)
[userinfo] => stdClass Object
(
[id] => 8
[email] => pankaj@mail.com
[username] => pancaz
[avatar] =>
[password] => e10adc3949ba59abbe56e057f20f883e
[permissions] =>
[last_login] => 2015-11-04 06:26:28
[first_name] =>
[last_name] =>
[created_at] => 0000-00-00 00:00:00
[updated_at] => 0000-00-00 00:00:00
)
)
其他方法点击网址(http://localhost/project/home/profile)
Array
(
[_token] => cOCcWvyy8Z93W36rpaNNjU4p2cmC2l5t75WT0LaH
[_previous] => Array
(
[url] => http://laravel.example.com
)
[flash] => Array
(
[old] => Array
(
)
[new] => Array
(
)
)
[key] => pankaj
)
routes.php file path app/Http/routes.php contains
Route::post('home/signupuser', 'HomeController@signupuser');
Route::post('home/signinuser', 'HomeController@signinuser');
Route::get('home/profile', 'HomeController@userprofile');