Click to See Complete Forum and Search --> : Simple Sessions


crazycoder
09-30-2006, 04:22 PM
I put together this very simple code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Feed Editor</title>
</head>

<body>

<?
$pass = 'iwonttellyerthat';

ini_set("display_errors", "1");
error_reporting(E_ALL);
?>

<? if(!isset($_SESSION['loggedin']) && !isset($_POST['pass'])) { ?>
<form action="editFeed.php" method="post">
Password: <input type="password" name="pass" size="30" />
<input type="submit" value="log in" name="submit" />
</form>
<? }
else {
if(isset($_POST['pass']) && !isset($_SESSION['loggedin'])) {
if($_POST['pass'] == $pass) {
session_start();
$_SESSION['loogedin'] = true;
}
else {
echo 'bad password!';
}
}
else {
echo 'You logged in!';
}
}

?>

</body>
</html>


I get this very strange error:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/freehost/t35.com/s/a/samoo/feed/editFeed.php:10) in /home/freehost/t35.com/s/a/samoo/feed/editFeed.php on line 26

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/freehost/t35.com/s/a/samoo/feed/editFeed.php:10) in /home/freehost/t35.com/s/a/samoo/feed/editFeed.php on line 26

NogDog
09-30-2006, 07:16 PM
Your session_start() command must be executed before any output is sent to the browser. This includes text (even spaces) before the opening <?php tag, and of course any PHP code that outputs data such as echo or print statements.

pcthug
09-30-2006, 08:12 PM
<?php

$pass = 'iwonttellyerthat';

ini_set("display_errors", "1");
error_reporting(E_ALL);
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Feed Editor</title>
</head>

<body>

<? if(!isset($_SESSION['loggedin']) && !isset($_POST['pass'])) { ?>
<form action="editFeed.php" method="post">
Password: <input type="password" name="pass" size="30" />
<input type="submit" value="log in" name="submit" />
</form>
<? }
else {
if(isset($_POST['pass']) && !isset($_SESSION['loggedin'])) {
if($_POST['pass'] == $pass) {
$_SESSION['loogedin'] = true;
}
else {
echo 'bad password!';
}
}
else {
echo 'You logged in!';
}
}

?>

</body>
</html>