<?php
session_start();
if (isset($_POST['submit'])) {
// Connect to the database or die.
$connection = mysql_connect("DB-ADDRESS", "USERNAME", "PASSWORD");
if (!$connection) { die("The Database connection failed. " . "<br />" . mysql_error()); }
// Select the table within the database or die.
$database_select = mysql_select_db("TABLENAME", $connection);
if (!$database_select) { die("The Database selection failed. " . "<br />" . mysql_error()); }
// Get the POST variables.
$myusername = $_POST['myusername'];
$mypassword = $_POST['mypassword'];
// To protect MySQL injection, we stripslashes and use mysql_real_escape_string.
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
// Run a query to bring back the users' record from the database.
$result = mysql_query("SELECT * FROM TABLENAME WHERE username = '$myusername' AND password = '$mypassword' LIMIT 1", $connection);
// If the query is not set meaning it failed, then die.
if (!$result) { die("The Database query failed. " . "<br />" . mysql_error()); }
// If it successfully returned one row...
if (mysql_num_rows($result) == 1) {
// Bind the returned results to an array.
$row = mysql_fetch_array($result);
// Register $_SESSION variables and redirect to "success.php".
$_SESSION["myusername"] = $myusername;
$_SESSION["firstname"] = $firstname;
// Redirect the person to the new page.
header("location:success.php");
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP: Login</title>
</head>
<body>
<form name="loginForm" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="myusername">Username</label> <br />
<input name="myusername" id="myusername" type="text" /> <br /> <br />
<label for="mypassword">Password</label> <br />
<input name="mypassword" id="mypassword" type="text" /> <br /> <br />
<input name="submit" id="submit" type="submit" value="Login" />
</form>
</body>
</html>