I've read everywhere that in order for sessions to work properly, session_start
must be called before outputting any html.
For example, this question: "Cannot send session cache limiter - headers already sent"
However, this example does not do that and it works:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Counting with the SESSION array</title>
</head>
<body>
<FORM action="counter-session.php" method="GET">
<INPUT type="submit" name="Count" value="Count">
<?php
session_start();
if (! isset($_SESSION['counter']))
$count = 0;
else
$count = $_SESSION['counter'];
$count = $count + 1;
$_SESSION['counter'] = $count;
echo "count is $count";
?>
</FORM>
</body>
</html>
That is, the counter increments as expected. How come?
Addition question: is it true that once session_destroy
is called, any further data stored into $_SESSION
will not be saved (ie. further changes to $_SESSION
will not be reflected the next time we use the $_SESSION
variable from another page)?