-you call session start
- php checks to see if the browser has sent a cookie to the server w/ a unique identifier in it(the session id)
- if so, it then checks its session_save directory for a file w/ the name of the session id
- if it finds this file, it parses the file and sets the variables into the $_SESSION array
if the browser did NOT send a cookie w/ a sess_id in it, then php sends a cookie back to the client w/ a session id, which the browser will then send back to the server on subsequent requests.
- at the end of the scripts execution, php will look at all the values you have put/removed/changed from $_SESSION, and update this users session file accordingly.
you cant check if the browser accepts cookies or not using your current method. this is because as soon as you set a session variable, its set within php. that variable will ALWAYS exist at LEAST until the next page refresh. the only way the variable will dissapear, is if on the next page request, the browser fails to send a cookie w/ the session id in it, which causes php to be unable to load thier session file w/ the data in it. in this case php would start a new session, and if the browser doesnt accept cookies, it will keep starting a new session on EVERY page request.
to check if the browser accepts cookies or not, you need to refresh/redirect to a page which will check this. there is no way possible to do this without refreshing the page, or loading a second page, because http is a stateless protocol. well, i shouldnt say impossible, but it would be pretty involved to make something reliable.
<?php
session_start();
if (!isset($_SESSION['cookie_flag']) && isset($_GET['cookie_test'])) {
// this browser does not accept cookies
} elseif (!isset($_SESSION['cookie_flag'])) { // we dont know if they accept cookies or not, so lets test it
$_SESSION['cookie_flag'] = true; // lets set a session var to see if it persists to the next page request
header('Location: http://example.org/session_test.php?cookie_test=true'); // redirect to this same script, but set a _GET variable to let us know we tried to set a session var
exit;
} else {
// $_SESSION['cookie_flag'] was set so we are sure the browser accepts cookies
}
?>