Hello,
I'm trying to do a very simple login against a mysql 'user' table that will redirect upon successful authentication. I'm stuck trying to troubleshoot why this will not take me to the correct page when authenticated.
Problem is when I successfully authenticate I'm still directed to the login page (index.php).
I've made sure I can do sessions with a simple test script echoing back the session ID. I've also checked my variable names.
Please advise if possible.
login.php
<?php
require_once( "DB.php" );
$dsn = 'mysql://root:password@localhost/database';
$db =& DB::Connect( $dsn, array() );
if (PEAR::isError($db)) { die($db->getMessage()); }
$res = $db->query( "SELECT user_name FROM user WHERE user_name=? AND passwd=MD5(?)",
array( $_POST['user_name'], $_POST['passwd'] ) );
$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.php
<?php
session_start();
if ( $_SESSION['user'] == null || $_SESSION['user'] < 1 )
{
header( "Location: index.php" );
exit;
}
require_once( "DB.php" );
$dsn = 'mysql://root:password@localhost/database';
$db =& DB::Connect( $dsn, array() );
if (PEAR::isError($db)) { die($db->getMessage()); }
$res = $db->query( "SELECT user_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>
Thank you for any help provided.
Tim