Web Programming with PHP

24 Users Validation against a Database

In the previous version, Users Validation against an Array, usernames and passwords were stored in an array in the same check-login web page.

Usernames and passwords are stored in a table in the database.

Action page of login.php:

login_action.php

<?php
include "db.php";

$username = $_POST["username"];
$password = $_POST["password"];
$sql = "SELECT email, password FROM users WHERE email = '$username' and password = md5('$password')";
$result = mysql_query($sql, $link);

if($result == false) {
  echo '<a href="login.php">Error: cannot execute query</a>';
  exit;
}

$num_rows = mysql_num_rows($result);
if($num_rows == 1)
 $redirect = "private.php";
else
 $redirect = "login.php";

mysql_free_result($result);
mysql_close($link);

header("Location: $redirect");
?>