I am creating a user login.
When submitting username and password I receive the message:
Warning: Cannot modify header information - headers already sent by (output started at /home/ekawee81/public_html/authenticate.php:3) in /home/ekawee81/public_html/authenticate.php on line 40
Which corressponds to this line (line 40):
#if there is a match the log-in is authenticated
if( $num != 0 )
{
$msg = "<h3>Welcome $username - your log-in succeeded!</h3>";
$msg = "<p align=\"center\"><a href=\"index.html\">Continue</a></p>";
}
else
{
###line 40###
header( "Location:$referer" ); exit();
}
Here is the entire login script:
<?php
$username = $POST['username'];
$password = $POST['password'];
$self = $SERVER['PHP_SELF'];
$referer = $SERVER['HTTP_REFERER'];
if either form field is empty return to the log-in page
if( ( !$username ) or ( !$password ) )
{ header( "Location:$referer" ); exit(); }
#connect to MySQL
$conn=@mysql_connect( "localhost", "username", "pw" )
or die( "Could not connect" );
#select the specified database
$rs = @mysql_select_db( "my_database", $conn )
or die( "Could not select database" );
#create the query
$sql = "select * from users where username=\"$username\" and password = password( \"$password\" )";
#execute the query
$rs = mysql_query( $sql, $conn )
or die( "Could not execute query" );
#get number of rows that match username and password
$num = mysql_numrows( $rs );
#if there is a match the log-in is authenticated
if( $num != 0 )
{
$msg = "<h3>Welcome $username - your log-in succeeded!</h3>";
$msg = "<p align=\"center\"><a href=\"index.html\">Continue</a></p>";
}
else
{
header( "Location:$referer" ); exit();
}
?>
<html>
<head>
<title>Log-In Authenticated</title>
</head>
<body>
<?php echo( $msg ); ?>
</body>
</html>
Can anyone help? Can anyone recommend a better way for logging in?
Also how do how to have the successful login automatically go back to the index page?
th