Check if the session is established or not, and if it is not created?

I want to check if a session is currently established, and if so, that the page works as usual (do nothing), if it does not create a session.

I looked at another SO question in which the following code was posted:

if ( empty( $_SESSION['login'] )) { } else { } 

The easiest way to do this is to set something like $_SESSION['a'] for each session, and then run if(empty($_SESSION['a'])) to check if the session exists?

Again, you can use a session variable without calling session_start () in the first place, which makes it obsolete (I tried it yesterday as an echo, but not an if statement, to check if the variable was transferred without realizing that session_start () needs to be called, before I can repeat the variable).

This is probably an easy way that is often used, I just can't find it.

Any help would be greatly appreciated!

+7
source share
1 answer

session_id() returns a string identifying the current session. If the session has not been initialized, it will return an empty string.

  if(session_id()) { // session has been started } else { // session has NOT been started session_start(); } 
+22
source

All Articles