The removing of the spaces didn't work. I think ill just start again from scratch, I'm getting really annoyed with errors. I get this error when I sign in:
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\authenticate.php:3) in C:\xampp\htdocs\authenticate.php on line 39
Here is all of the code:
<?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", "password" )
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 user_name=\"$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>";
}
else
{
header( "Location:$referer" ); exit();
}
?>
<html>
<head>
<title>Log-In Authenticated</title>
</head>
<body>
<?php echo( $msg ); ?>
</body>
</html>
and the create user code is:
<html><head><title>Adding a User</title></head>
<body>
<?php
$self = $_SERVER['PHP_SELF'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$username = $_POST['username'];
$password = $_POST['password'];
if( (!$firstname) or (!$lastname) or (!$username) or (!$password) )
{
$form ="Please enter all new user details...";
$form.="<form action=\"$self\"";
$form.=" method=\"post\">First Name: ";
$form.="<input type=\"text\" name=\"firstname\"";
$form.=" value=\"$firstname\"><br>Last Name: ";
$form.="<input type=\"text\" name=\"lastname\"";
$form.=" value=\"$lastname\"><br>User Name: ";
$form.="<input type=\"text\" name=\"username\"";
$form.=" value=\"$username\"><br>Password: ";
$form.="<input type=\"text\" name=\"password\"";
$form.=" value=\"$password\"><br>";
$form.="<input type=\"submit\" value=\"Submit\">";
$form.="</form>";
echo($form);
}
else
{
#connect to MySQL
$conn = @mysql_connect("localhost","username","password")
or die("Could not connect to MySQL");
#select a database
$db = @mysql_select_db("my_database",$conn)
or die("Could not select database");
#create the SQL query
$sql = "insert into users (first_name,last_name,user_name,password)
values (\"$firstname\",\"$lastname\",\"$username\",password(\"$password\"))";
#execute the query
$result = @mysql_query($sql,$conn)
or die("Could not execute query");
if($result)
{ echo("New user $username added"); }
}
?>
</body></html>