关于PEAR :: Auth包的问题

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:

  1. First, when I log in I get following warnings:
    Warning: session_regenerate_id() [function.session-regenerate-id]: Cannot regenerate session id - headers already sent in /usr/local/php5/lib/php/Auth.php on line 830
    Warning: Cannot modify header information - headers already sent by (output started at /home/myaccount/www/www/test.php:2) in /usr/local/php5/lib/php/Auth.php on line 858

Why am I getting these?

  1. Second, when I click on the Dump session link, it asks me to log in again - i.e., does not recognize that I am already authenticated and logged in.

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.