Despite you not asking a distinct question and rather just asking for a code review (much better to do this on codereview) here is my opinion.
You has a couple of obvious issues,
- Firstly like napster3world said you really need to start your session or nothing of much use will come of your error reporting.
- Secondly you are wide open for all sort of malicious attacks this kinda undermining having a secure login.
While by no means exhaustive the following contains a couple of suggestions to help make it a little securer.
- Detach your variables from the POST global - making it more obvious in the input is tainted.
- Using a PDO database connection as this is much more secure method of connecting to your database than mysql_connect especially when using bound parameters to protect against sql injection (See the discussion here)
Code
The following is a quick ready that hopefully will point you in the right direction and improve some of you security.
// Start the session for storing your errors
session_start();
// Check that the button was clicked on the form
if (isset($post)) {
// Array for storing any errors
$err = array();
// Extract details from POST global
$_username = $_POST['username'];
$_password = $_POST['password'];
/*
You may want to consider some filtering here
*/
// Did the user fill in the username field?
if (empty($_username)) {
$err['username'] = "User name not provided";
}
// Did the user fill in the password field?
if (empty($_password)) {
$err['password'] = "Password not provided";
} else {
// Yes so hash it for the database check
$hashedPassword = md5($_password);
}
if (empty($err)) {
// Establish database connection
try {
$dsn = "mysql:host{$host};port={$port};dbname={$database}";
$connection = new PDO($dsn, $dbUsername, $dbPassword, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
} catch (PDOException $e) {
throw new \Exception("Unable to connect to the Database");
}
// Build SQL query and run on PDO connection
$sql = "SELECT id, usr FROM tz_members WHERE usr = :username AND pass = :hashedPassword";
try {
$stmt = $connection->prepare($sql);
// Bid your parameters to prevent sql injection
$stmt->bindParam(':username', $_username);
$stmt->bindParam(':hashedPassword', $hashedPassword);
$stmt->execute();
} catch (PDOException $e) {
throw new Exception("Error with executing query: {$e->getMessage()}");
}
// Fetch your results
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if (!empty($row)) {
// Fill the session up with users details
$_SESSION['id'] = $row['id'];
$_SESSION['usr'] = $row['usr'];
// Head back to the login page - surely you wan to head to your protected page?
header("Location: login.php/");
return;
}
// Login failed
$err['login'] = "Wrong username and/or password!";
}
// Head back to the login page
$_SESSION['errors'] = $err;
header("Location: login.php/");
return;
}
Further reading
The following are a couple of links to tuts that might help a little.
- This is a tut by the excellent Jeffrey Way on using the PDO Api
- And this tut looks at more detailed ways to secure your forms.
I hope this is of help, and if anything makes you ask more questions.