Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if PHP session exists

Tags:

php

session

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.

like image 525
Pablo Avatar asked Aug 21 '10 18:08

Pablo


People also ask

How do you check if there is a session in PHP?

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.

How check session is empty in PHP?

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).

What is session status?

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.

How check session is destroy or not in PHP?

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.


2 Answers

I use a combined version:

if(session_id() == '' || !isset($_SESSION) || session_status() === PHP_SESSION_NONE) {     // session isn't started     session_start(); } 
like image 106
Reign.85 Avatar answered Oct 09 '22 03:10

Reign.85


According to the PHP.net manual:

If $_SESSION (or $HTTP_SESSION_VARS for PHP 4.0.6 or less) is used, use isset() to check a variable is registered in $_SESSION.

like image 28
Sebastián Grignoli Avatar answered Oct 09 '22 04:10

Sebastián Grignoli