I am a newbie to PHP development. I tried following code sample from PHP5 Power Programming book:
<?php
require_once 'DB.php';
require_once 'PEAR.php';
require_once 'Auth.php';
$auth_options = array(
'dsn' => 'mysql://username:password@localhost/database',
'table' => 'users',
'usernamecol' => 'username',
'passwordcol' => 'password',
'db_fields' => '*',
);
PEAR::setErrorHandling(PEAR_ERROR_DIE);
$auth = new Auth('DB', $auth_options);
$auth->start();
if (!$auth->getAuth()) {
exit;
}
if (!empty($_REQUEST['logout'])) {
$auth->logout();
print "<h1>Logged out</h1>
";
print "<a href=\"$_SERVER[PHP_SELF]\">Log in again</a>
";
exit;
}
print "<h1>Logged in!</h1>
";
if (!empty($_REQUEST['dump'])) {
print "<pre>_authsession = ";
print_r($_SESSION['_authsession']);
print "</pre>
";
} else {
print "<a href=\"$_SERVER[PHP_SELF]?dump=1\">Dump session</
?a><br>
";
}
print "<a href=\"$_SERVER[PHP_SELF]?logout=1\">Log Out</a>
";
?>
there are several problems I am having with this code:
Why am I getting these?
Could some please explain these to me? Thanks.
Sessions are maintained by a cookie passed between the server and the browser in each request containing a session identifier PHP generates. Since cookies are part of HTTP headers, creation of a session by sending a cookie has to happen before your script outputs any content.
That includes whitespace, which is the likely culprit for your error. There can be no whitespaces, including blank lines, spaces, BOMs at the top of the file before the opening <?php
tag, including in the included files.
The second error is a side effect of the first error, so once you fix that, it'll go away as well.