Forum Moderators: coopster
My idea of determining if there is an active session is to call session_start() and check to see if an expected value (user_id, for example) is set. But the act of calling session_start() will begin a new session if one did not exist, correct? This is what I'd like to avoid.
What I'm looking for is something like
if (session_exists()) {
session_start();
$user_id = $_SESSION['user_id'];
}
If you are storing session information in cookies then you could always look for $_COOKIE['session_name'] to see if it is there. Then decide to start or not. If you are using url id's then look in the $_GET array for PHPSESSID or whatever your session_name is set to.
You may be able to use session_id() [uk.php.net] for what you are looking for.
If you are starting sessions based on user supplied info like cookies/url's then be very very careful about people fixing/poisoning/mucking your your sessions. As with everything that users can supply treat it as evil until it have been cleaned, validated and checked...and even then it may well just be safer to call session_regenerate_id() [uk.php.net]or just start a new session.