Here's the code:
<?php
//Start session
session_start();
//Array to store validation errors
$errmsg_arr = array();
//Validation error flag
$errflag = false;
//connect to the database
$con = mysql_connect("localhost", "root");
if(!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("faculty_wars", $con);
//Input Validations
if($_POST['username'] == '') {
$errmsg_arr[] = 'Username missing';
$errflag = true;
}
if($_POST['password'] == '') {
$errmsg_arr[] = 'Password missing';
$errflag = true;
}
//If there are input validations, redirect back to the login form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: login.php");
}
//Create query
$sql="SELECT * FROM users WHERE username='$_POST[username]' && password='".md5($_POST['password'])."'";
$result=mysql_query($sql);
// Check username and password match
if (mysql_num_rows($result) == 1) {
// Set username session variable
$_SESSION['username'] = $_POST['username'];
// Jump to secured page
header('Location: home.php');
}
else {
// Jump to login page
header('Location: login-failed.php');
}
?>

New Topic/Question
Reply




MultiQuote







|