Facebook now offer subscriptions to users so you can get realtime updates on changes. If my app receives an update, I plan to store it in the database. I would also like to detect if their session exists. If it does then I could update the data in there too.
My session IDs are MD5(fb_id + secret) so I could easily edit their session. The question is how can I detect if the session exists.
You can check whether a variable has been set in a user's session using the function isset(), as you would a normal variable. Because the $_SESSION superglobal is only initialised once session_start() has been called, you need to call session_start() before using isset() on a session variable.
If you want to check whether sessions are available, you probably want to use the session_id() function: session_id() returns the session id for the current session or the empty string ("") if there is no current session (no current session id exists).
Sessions or session handling is a way to make the data available across various pages of a web application. The session_status() function returns the status of the current session.
Well, you can use a var like $_SERVER['gameHasStarted'] which defaults to false unless the user performs some action and that is reset when the user ends the game. (when you destroy the session). Yeah that's what I've ended up doing, seems to be the best way to go.
I use a combined version:
if(session_id() == '' || !isset($_SESSION) || session_status() === PHP_SESSION_NONE) { // session isn't started session_start(); }
According to the PHP.net manual:
If
$_SESSION
(or$HTTP_SESSION_VARS
for PHP 4.0.6 or less) is used, useisset()
to check a variable is registered in$_SESSION
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With