php - Simple slim session manager not read session in a different function -
i using following slim session manager(https://github.com/bryanjhv/slim-session). have separate functions login
, logout
, user_data
.
$app->post("/login", function() use ($app) { $input = $app->request()->getbody(); $input = json_decode($input); try { if ($input->username && $input->password) { $user = model::factory('users')->where("username",$input->username)->where("password",md5($input->password))->find_one(); $session = new \slimsession\helper; //set session $session->set('userid', $user->id); $status = 'success'; $message = 'logged in successfully.'; } else { $status = 'danger'; $message = 'could not log in. please try again.'; } } catch (exception $e) { $status = 'danger'; $message = $e->getmessage(); } $response = array( 'status' => $status, 'message' => $message, ); $app->response()->header("content-type", "application/json"); echo json_encode($response); }); $app->post("/logout",function() use ($app) { try { $session = new \slimsession\helper; $session::destroy(); $status = 'success'; $message = 'you have been logged out successfully'; } catch (exception $e) { $status = 'danger'; $message = $e->getmessage(); } $response = array( 'status' => $status, 'message' => $message ); $app->response()->header("content-type", "application/json"); echo json_encode($response); }); $app->get("/user_data", function() use ($app) { try { $session = new \slimsession\helper; //get session $userid = $session->get('userid'); $_session['userid'] = $userid; if ($_session['userid']) { $users = model::factory('users')->where('id',$_session['userid'])->find_one(); $response = array( 'id'=>$users->id, 'username'=>$users->username, 'email'=>$users->email, 'phone_number'=>$users->phone_number, 'password'=>$users->password, 'type'=>$users->type, 'credits'=>$users->credits, 'profile_picture'=>$users->profile_picture, ); } else { $status = "danger"; $message = 'you need logged in that.'; $response = array( 'status' => $status, 'message' => $message ); } } catch (exception $e) { $status = "danger"; $message = $e->getmessage(); $response = array( 'status' => $status, 'message' => $message ); } $app->response()->header("content-type", "application/json"); echo json_encode($response); });
the problem having when user logs in set session
variable in /login function. when session
variable set in login
function isn't being retrieved in /user_data function.
anyone knows whats going on?
have started session session_start() ?
correct logic check session follows:
if (isset($_session['userid'])) { // session exists // further work }
Comments
Post a Comment