tags:

views:

163

answers:

3

I am learning PHP PEAR and testing the following code. However when I login, I get the following error.

Could anyone tell me what is wrong with the code?

++++++++++++++++

Warning: session_regenerate_id() [function.session-regenerate-id]: Cannot regenerate session id - headers already sent in C:\xampp\php\PEAR\Auth.php on line 830

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\php5adv\Ch04\login.php:50) in C:\xampp\php\PEAR\Auth.php on line 858

++++++++++++++++

<?php # Script 4.3 - login.php

/*  This page uses PEAR Auth to control access.
 *  This assumes a database called "auth",
 *  accessible to a MySQL user of "username@localhost" 
 *  with a password of "password".
 *  Table definition:

    CREATE TABLE auth (
    username VARCHAR(50) default '' NOT NULL,
    password VARCHAR(32) default '' NOT NULL,
    PRIMARY KEY (username),
    KEY (password)
    )
 *  MD5() is used to encrypt the passwords.
 */

// Need the PEAR class:
require_once "Auth.php";

// Function for showing a login form:
function show_login_form() {

    echo '<form method="post" action="login.php">
<p>Username <input type="text" name="username" /></p>
<p>Password <input type="password" name="password" /></p>
<input type="submit" value="Login" />
</form><br />
';

} // End of show_login_form() function.

// Connect to the database: 
$options = array('dsn' => 'mysql://username:password@localhost/auth');

// Create the Auth object:
$auth = new Auth('DB', $options, 'show_login_form');

// Add a new user:
$auth->addUser('me', 'mypass');

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <title>Restricted Page</title>
</head>
<body>
<?php

// Start the authorization:
$auth->start();

// Confirm authorization:
if ($auth->checkAuth()) {

    echo '<p>You are logged in and can read this. How cool is that?</p>';

} else { // Unauthorized.

    echo '<p>You must be logged in to access this page.</p>';

}

?>
<a href="logout.php>Logout</a>
</body>
</html>
+2  A: 

Hi,

The "headers already sent" errors means that you are trying to send headers (cookies are HTTP headers, and sessions in PHP use cookies for the session identifier) while there has already been some output sent.

When there is some ouput (even one white space is enough !) sent, PHP automatically sends the headers ; and you cannont set another header when those have already been sent.

So : you have to remove any output that is sent before the call to session_regenerate_id

First of all, check that there is no white-space before the <?php tag, or ofter the ?> -- this often solves the problem...


Here, considering the "output started at C:\xampp\htdocs\php5adv\Ch04\login.php:50" message, I'd check in you login.php, arround line 50 ;-)

You are sending some HTML output (html, head, ... tags), and, only after, are doing

// Start the authorization:
$auth->start();

I'm guessing this start method is the one that starts the session, and, hence, tryies to send cookies... As there has already been some HTML output, headers have already been sent, and you cannot send new ones.

This means you'll probably have to move the call to $auth->start to the top of your file, before doing any output ; in the PHP code-block that creates the instance of Auth, for instance -- so headers have not already been sent.

Pascal MARTIN
Thanks Pascal MARTIN. I moved $auth->start(); just before ?> and it is fixed. If I move to at the top it gives an error, but before ?>, it works fine.Thanks again.
shin
@shin : OK ; yeah, by "at the top", I meant "before any output, in the block of PHP code that creates the instance of the Auth class". Have fun !
Pascal MARTIN
A: 

Your script should begin with

<?php

and you have that after the comment block and some new lines.

Everything that is before that is printed and if PHP prints anything it's impossible to regenerate session id because session ids are stored in cookies which must be sent before page content.

You should always make sure not to have anything before <?php in your scripts, not even a white characters.

RaYell
A: 

It looks like you're outputting some text before the start of your PHP.

Even if it's just whitespace, this will break things.

therefromhere