<?php
session_start();
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$bool = true;
mysql_connect("localhost", "root", "") or die (mysql_error()); //Connect to server
mysql_select_db("first_db") or die ("Cannot connect to database"); //Connect to database
$query = mysql_query("Select * from users WHERE username='$username'"); // Query the users table
$exists = mysql_num_rows($query); //Checks if username exists
$table_users = "":
$table_password = "";
if($exists > 0) //IF there are no returning rows or no existing username
{
while($row = mysql_fetch_assoc($query)) // display all rows from query
{
$table_users = $row['username']; // the first username row is passed on to $table_users, and so on until the query is finished
$table_password = $row['password']; // the first password row is passed on to $table_password, and so on until the query is finished
}
if(($username == $table_users) && ($password == $table_password))// checks if there are any matching fields
{
if($password == $table_password)
{
$_SESSION['user'] = $username; //set the username in a session. This serves as a global variable
header("location: home.php"); // redirects the user to the authenticated home page
}
}
else
{
Print '<script>alert("Incorrect Password!");</script>'; // Prompts the user
Print '<script>window.location.assign("login.php");</script>'; // redirects to login.php
}
}
else
{
Print '<script>alert("Incorrect username!");</script>'; // Prompts the user
Print '<script>window.location.assign("login.php");</script>'; // redirects to login.php
}
?>
<head>
<title>My first PHP website</title>
</head>
<body>
<h2>Login Page</h2>
<a href="index.php">Click here to go back</a><br/><br/>
<form action="checklogin.php" method="post">
Enter Username: <input type="text" name="username" required="required"/> <br/>
Enter Password: <input type="password" name="password" required="required" /> <br/>
<input type="submit" value="Login"/>
</form>
</body>
this is the code I have which should check the login and direct you to the index.php page but it doesn't do anything. all my user names and passwords are stored in my db The last bit of code is what it used to create the login page , I'm not sure what is stopping the login taking place? all the users are stored in the db properly but when I log in with them the page just refreshers quickly and does nothing