Hi everyone,
I'm running into a few issues creating a username/login script. This has been driving me nuts for the last week. The following scripts work well in IE except for when I logout of a session. I get the following error:
"Warning: session_destroy() [function.session-destroy]: Trying to destroy uninitialized session in /home/mikepar8/public_html/login/logout.php on line 2
Warning: Cannot modify header information - headers already sent by (output started at /home/mikepar8/public_html/login/logout.php:2) in /home/mikepar8/public_html/login/logout.php on line 3"
If I use Firefox, when I login using the proper login credentials the index.php page just reloads itself. However, if I enter the wrong username or password, it does notify me that wrong information was submitted.
There are 4 different files. I wanted to try to keep this post as short as I could so I omitted the code for the username and password form.
I apologize for the length of this post. Thanks for your help!
Here is the code for login.php, which is the form action from the username and password form :
<?php
require_once( "DB.php" );
$dsn = '';
$db =& DB::Connect( $dsn, array() );
if (PEAR::isError($db)) { die($db->getMessage()); }
$res = $db->query( "SELECT id FROM users WHERE name=? AND
password=MD5(?)",
array( $_POST['user'], $_POST['password'] ) );
$row = array( null );
if ( $res != null )
$res->fetchInto( $row );
if ( $row[0] != null )
{
session_start();
$_SESSION['user'] = $row[0];
header( "Location: welcome.php" );
}
else
{
header( "Location: index.php?bad=1" );
}
?>
Here is the welcome screen that a user comes to upon entering a correct
username and password:
<?php
session_start();
if ( $_SESSION['user'] == null || $_SESSION['user'] < 1 )
{
header( "Location: index.php" );
exit;
}
require_once( "DB.php" );
$dsn = '';
$db =& DB::Connect( $dsn, array() );
if (PEAR::isError($db)) { die($db->getMessage()); }
$res = $db->query( "SELECT name FROM users WHERE id=?",
array( $_SESSION['user'] ) );
$res->fetchInto( $row );
?>
<html>
<head><title>Welcome</title></head>
<body>
Welcome <?php echo( $row[0] ); ?><br/><br/>
<a href="logout.php">Logout</a>
</body>
</html>
Here is the logout.php script:
<?php
session_destroy();
header( "Location: index.php" );
?>