golgotha
12-10-2006, 01:05 AM
Hi. I have a login form written in php that uses itself as the action (<form action="<?php echo $_SERVER['../PHP_SELF']?>" method="post">).
When the user enters their username and passwd, the page reloads. If submit was clicked, then the input is checked against the database. If it matches, the user authenticates and is logged in. Their home page should then load.
The problem is that I am getting this error:
Warning: Cannot modify header information - headers already sent by (output started at .../test/04/include/header.php:6) in ...test/04/include/body_login.php on line 58
I know am getting that error because I have already included several files and outputted HTML.
What I want is the user's home page to load after they click the submit button, but I don't know another way to do it besides the header() function.
Any suggestions?
the login page is here: http://www.arationalmind.com/test/04/home.php
this is the login code:
<td width = "80%" valign = "top">
<?php
// file: login.php
// database connect script.
// credit to http://www.free2code.net for the code concept
require 'include/db_connect.php';
if($logged_in == 1) {
die('You are already logged in, '.$_SESSION['user_name'].'.');
}
if (isset($_POST['submit'])) { // if form has been submitted
/* check they filled in both fields and then authenticate */
if(!$_POST['uname'] | !$_POST['passwd']) {
die('You did not fill in a required field.');
}
// authenticate.
if (!get_magic_quotes_gpc()) {
$_POST['uname'] = addslashes($_POST['uname']);
}
$check = $db_object->query("SELECT user_name, password FROM user_login WHERE user_name = '".$_POST['uname']."'");
if (DB::isError($check) || $check->numRows() == 0) {
die('That user_name does not exist in our database.');
}
$info = $check->fetchRow();
// check passwords match
$_POST['passwd'] = stripslashes($_POST['passwd']);
$info['password'] = stripslashes($info['password']);
$_POST['passwd'] = md5($_POST['passwd']);
if ($_POST['passwd'] != $info['password']) {
die('Incorrect password, please try again.');
}
// if we get here user_name and password are correct,
//register session variables and set last login time.
$date = date('m d, Y');
$update_login = $db_object->query("UPDATE user_login SET last_login = '$date' WHERE user_name = '".$_POST['uname']."'");
$_POST['uname'] = stripslashes($_POST['uname']);
$_SESSION['user_name'] = $_POST['uname'];
$_SESSION['password'] = $_POST['passwd'];
$db_object->disconnect();
header("Location: http://www.arationalmind.com/test/04/home.php");
exit;
} else { // if form hasn't been submitted
?>
<form action="<?php echo $_SERVER['../PHP_SELF']?>" method="post">
<table align="center" border="1" cellspacing="0" cellpadding="3">
<tr><td colspan="2"> Login</td></tr>
<tr><td>Username:</td><td>
<input type="text" name="uname" maxlength="40">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="passwd" maxlength="50">
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="submit" value="Login">
</td></tr>
</table>
</form>
<center><a href="http://www.arationalmind.com/test/04/register.php">Register an Account</a></center>
<?php
}
?>
</b></p>
</td>
When the user enters their username and passwd, the page reloads. If submit was clicked, then the input is checked against the database. If it matches, the user authenticates and is logged in. Their home page should then load.
The problem is that I am getting this error:
Warning: Cannot modify header information - headers already sent by (output started at .../test/04/include/header.php:6) in ...test/04/include/body_login.php on line 58
I know am getting that error because I have already included several files and outputted HTML.
What I want is the user's home page to load after they click the submit button, but I don't know another way to do it besides the header() function.
Any suggestions?
the login page is here: http://www.arationalmind.com/test/04/home.php
this is the login code:
<td width = "80%" valign = "top">
<?php
// file: login.php
// database connect script.
// credit to http://www.free2code.net for the code concept
require 'include/db_connect.php';
if($logged_in == 1) {
die('You are already logged in, '.$_SESSION['user_name'].'.');
}
if (isset($_POST['submit'])) { // if form has been submitted
/* check they filled in both fields and then authenticate */
if(!$_POST['uname'] | !$_POST['passwd']) {
die('You did not fill in a required field.');
}
// authenticate.
if (!get_magic_quotes_gpc()) {
$_POST['uname'] = addslashes($_POST['uname']);
}
$check = $db_object->query("SELECT user_name, password FROM user_login WHERE user_name = '".$_POST['uname']."'");
if (DB::isError($check) || $check->numRows() == 0) {
die('That user_name does not exist in our database.');
}
$info = $check->fetchRow();
// check passwords match
$_POST['passwd'] = stripslashes($_POST['passwd']);
$info['password'] = stripslashes($info['password']);
$_POST['passwd'] = md5($_POST['passwd']);
if ($_POST['passwd'] != $info['password']) {
die('Incorrect password, please try again.');
}
// if we get here user_name and password are correct,
//register session variables and set last login time.
$date = date('m d, Y');
$update_login = $db_object->query("UPDATE user_login SET last_login = '$date' WHERE user_name = '".$_POST['uname']."'");
$_POST['uname'] = stripslashes($_POST['uname']);
$_SESSION['user_name'] = $_POST['uname'];
$_SESSION['password'] = $_POST['passwd'];
$db_object->disconnect();
header("Location: http://www.arationalmind.com/test/04/home.php");
exit;
} else { // if form hasn't been submitted
?>
<form action="<?php echo $_SERVER['../PHP_SELF']?>" method="post">
<table align="center" border="1" cellspacing="0" cellpadding="3">
<tr><td colspan="2"> Login</td></tr>
<tr><td>Username:</td><td>
<input type="text" name="uname" maxlength="40">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="passwd" maxlength="50">
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="submit" value="Login">
</td></tr>
</table>
</form>
<center><a href="http://www.arationalmind.com/test/04/register.php">Register an Account</a></center>
<?php
}
?>
</b></p>
</td>