0

I have this situation:

if ( 
    session_status() === PHP_SESSION_NONE
) {
    echo "session not exist";
    session_start();
} else {
    echo "session exist";
}

It return always "session not exist" too when page is reloaded the second time. How i can solve?

Marcello Impastato
  • 2,263
  • 5
  • 30
  • 52
  • Do you have any `session_start();` before the line with `session_status()`? – Progman Apr 24 '21 at 16:31
  • 1
    The session has to be started for the code to access it... Every time the code runs. – Louys Patrice Bessette Apr 24 '21 at 16:31
  • Of course no! I have a php file cleaned with only it. @LouysPatriceBessette: The real situation is little different. I just want make a session_id but need generate it ONLY when session not is started. – Marcello Impastato Apr 24 '21 at 16:36
  • Maybe you are looking for [session_name](https://www.php.net/manual/en/function.session-name.php)? – Louys Patrice Bessette Apr 24 '21 at 16:38
  • @LouysPatriceBessette : no, i use too it. But i want generate manually a session_id and set it. The problem is which is executed always first block too when session is started and so i have always different session id when reload page. Just i want solve this problem (see post) for solve too this second problem. – Marcello Impastato Apr 24 '21 at 16:40
  • 2
    It sounds like you have [an X/Y Problem](https://meta.stackexchange.com/q/66377/237313): you thought checking session_status() would help you with your real task, but it doesn't. So go back to what you were _actually_ trying to do, and look for other solutions. – IMSoP Apr 24 '21 at 16:42
  • @IMSoP : I have tried to set different condition on the if but not solved. I tried with !isset( $_SESSION ) for example but nothing. Just i want to be sure which a block is executed ONLY a time when session not is yet started. It need for set a custom session_id. In this moment, with this condition the first block is always executed and session_id is always set. It need only a time. – Marcello Impastato Apr 24 '21 at 16:49
  • 1
    A simple method of checking if the session has been started is https://stackoverflow.com/a/64335687/1213708 – Nigel Ren Apr 24 '21 at 16:57
  • @NigelRen : i tried so, as you suggested in the link but so i can't change session_id () with a custom value. For this i wanted check before of launch session_start if it was started or not. In this mode, if not started yet i can set a custom session id. Understand? – Marcello Impastato Apr 24 '21 at 17:07
  • You would need to try and start the session using whatever ID you have and then see if it is a valid session. – Nigel Ren Apr 25 '21 at 08:47
  • @NigelRen Yes. I want do it. But using code as in post not work. If i put out session_start work but i can't set session id after which started. How i can solve it? – Marcello Impastato Apr 25 '21 at 09:46
  • You call the initial start session with the id, then check if it's a valid session. – Nigel Ren Apr 25 '21 at 13:46
  • @NigelRen session start correctly. My problem is which i want set a custom id ( generated as random ). The problem is which i need use session_id ( value ) before to call session_start(). For this reason i thinked a control, like: " if session not exist (or not created) then set custom session id and create session". Of if session exist (or created) the set custom id not need. As i can set correctly the if control for it? I thinked: if ( /* something */ ) { $id = random(); session_id( $id ); session_start(); } . As i can write that "something" ? – Marcello Impastato Apr 25 '21 at 15:43
  • @NigelRen i tried too with session_id() === "" as condition but not work. – Marcello Impastato Apr 25 '21 at 15:44
  • @MarcelloImpastato You can use `session_start(); $isNew = count($_SESSION) == 0; session_write_close();` to check if a new session would/will be created or an existing session exists (which have data in the session). – Progman Apr 25 '21 at 16:09

1 Answers1

3

I think your confusion is with the meaning of session_start(). Your code would work if it meant "start a session for this user", but what it actually means is more like "start the session machinery". Or, as the PHP manual page for it says:

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.

The status returned by session_status() is basically just saying "you haven't called session_start() yet".

IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • Exactely! I want this: IF session NOT STARTED then set a custom value for session_id() and start session else NOT DO NOTHING. It need for preserve when i go to generate a session_id() before of session_start(). – Marcello Impastato Apr 24 '21 at 16:42
  • I need to execute session_id() only when session not is started. But in this case the condition of IF is always true. To reload of page it should be false. – Marcello Impastato Apr 24 '21 at 16:45
  • @MarcelloImpastato Reloading a page still starts with a fresh PHP execution where the `session_start()` method hasn't been called yet (for that exact PHP execution). Unless you have [`session.auto_start`](https://www.php.net/manual/en/session.configuration.php#ini.session.auto-start) enabled, your PHP script will start with a session **not** started (yet). Check the documentation of `session_start();` how/when you should/must use it. – Progman Apr 24 '21 at 16:53
  • @Progman : of course. But if i start session after i have problem to change session_id. It need to be setted before to start a session. For this i want find a solution about some correct condition for to do about i want. – Marcello Impastato Apr 24 '21 at 16:55
  • @MarcelloImpastato You already did. When `session_status()` returned `NONE` then you know you haven't called `session_start()` yet, so you can call `session_id()` to set the session id. – Progman Apr 24 '21 at 16:58
  • @Progman yes, but when i reload the page it again join in first block and not in second block of the if. Probably i mistake to use the condition with session_status but if i try to put out session_start from if block and i try to do inside the if as condition a: isset ( $_SESSION ["something"] ) it work but so i can't set a custon session_id becouse session is started! This is my problem. – Marcello Impastato Apr 24 '21 at 17:05
  • @MarcelloImpastato Depending on how your system is setup you can use the approach mentioned on https://stackoverflow.com/questions/13114185/how-can-you-check-if-a-php-session-exists/35254202#35254202. Or doing some weird stuff like `session_start()`, check if there are any session variables, run `session_write_close()` and then do your check and actions you want to perform. However, this still sounds like an XY problem and you should edit your question to include a *detailed* description what you *actually* trying to do with changing `session_id()`. – Progman Apr 24 '21 at 17:15