How do I check if a session ($_SESSION) exists or is active in PHP?

Asked

Viewed 22,145 times

3

there is some native function of the PHP that you can use to verify that a particular user session is open?

For example, I have a $_SESSION['usuario'] = "400379d4eaa6ec24826dd44cd5cf65a3" and I want to check if she’s active on the server PHP that is with the active session.

This, through another page, external, for example, if someone calls: http://meuenderecom.com.br/getSession.php?idUsuario=400379d4eaa6ec24826dd44cd5cf65a3 I want it to return’S' if session is active, or 'N' if session is closed.

3 answers

8


From what I understand, you want to know if a another session, other than the user in question, does exist, right? If yes:

PHP stores session data in a temporary folder on the server, which you can find out what it is, or even define using session_save_path().

For each session, PHP creates a file sess_{SESSION_ID} in that folder. You can get the list of files (sessions) using:

<?php
print_r(scandir(session_save_path()));
?>

If you want to know if a specific session exists, if you have SESION_ID, you can do the following:

<?php
session_start();
// $SESSION_ID = id da sessão que você quer saber se existe
echo (file_exists(session_save_path().'/sess_'.$SESSION_ID) ? "Existe!" : "Não existe!");
?>

As it seems that you want to search for the 'user', you would have to do the parse of those files and check for the information you need there. If you do not want to do the parse, you can take the ID of all sessions, which is in the file name and make a loop by initiating via session_start($SESSION_ID) and searching for the data you need to check, in this case the user.

Remember that in no way this is a recommendation. There is still an option, which would be to manipulate the sessions yourself by setting a new one Handler. That way you could store the data in a database or any other way you prefer, but you need to make sure you know what you’re doing!

Finally, it seems that you just want to know if a user is online. There are other much safer and more efficient techniques to do this, but it is for a next question (or even should already have one about it).

4

to do this just use the command isset(), in your case would look like this, using an if ternary:

$valor = isset($_SESSION['usuario']) ? 'S' : 'N';
  • But where will I inform the id from the session I want to check? I assigned $_SESSION['user'] the code "400379d4eaa6ec24826dd44cd5cf65a3".

  • your question is not very clear, please edit it so I can help better

  • What don’t you understand? See: I have a session, where I assigned her several encrypted identifiers, and I want through one of these identifiers to know if the session is active or not, by the ID and not by the session itself!

-1

With this question actually ends up expressing maybe other doubts, ideal to study a little. If you want to take the id session can use the session_id() but there are other variables about this, for example: - The session of usuario(session['usuario'], but the session_id() may still be the same if no regenerate... I recommend you do a little research on how sessions work so we can all help you better.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.