Skip to content
Advertisement

Can’t execute script if session not set

Good morning, I am trying to develop a link shortener system with statistics in php (5.6). One of these stats is the number of sessions. I am trying to use this logic:

JavaScript

With my first approach i tried to put session_start in the middle of the code and it didn’t work. I realized I can’t do it and I moved the statement in the first line of the code, making a big if condition.

JavaScript

When I debug this the session field is always updated, but it should increase only the first time. What am I missing?

Side note: this is the redirect page, i don’t know if it is a useful info.

Advertisement

Answer

session_status shows only status of current session, not if user has ever started a session. That’s why your session_status() === PHP_SESSION_NONE are always true on first requests execution of that if statement.

Maybe you want to do something like storing some flag in session to indicate if it’s user’s first visit or not

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement