So I have a login page that I want to either take the user to the homepage or back the login page depending on whether they entered their details correctly.
<?php
session_start();
unset($_SESSION['logerror']);
print_r($_POST);
$host="localhost"; // Host name
$db_name="website_database.sqlite"; // Database name
$tbl_name="User"; // Table name
$con= sqlite_open("Website_Database.sqlite");
// username and password sent from form
$username=$_POST['Username'];
$password=$_POST['Password'];
$sql="SELECT * FROM ".$tbl_name." WHERE Username='".$username."' and Password='".$password."'";
$result= sqlite_query($con,$sql);
// Mysql_num_row is counting table row
$count= sqlite_num_rows($result);
// If result matched $username and $password, table row must be 1 row
if($count==1){
// Register username, $password and redirect to file "login_success.php"
session_register("Username");
session_register("Password");
echo "Login Successful";
}
else {
echo "wrong username and password";
};
?>
I want to have something like this :
<meta http-equiv="Refresh" content="5;url=login2.php">
To take the user back to the login page is unsuccessful. What would be the best way to place this in the if and elif ?
I would be better to use a Location header and redirect them that way rather than a meta refresh. If you want them to see the error just save error in a $_SESSION variable and check that on the new page.
else {
$_SESSION['login_error'] = 'Wrong username or password.';
session_write_close();
header('Location: login2.php');
}
Then in your login2.php just check for the error:
if (!empty($_SESSION['login_error'])) {
echo "some html with the error: " . $_SESSION['login_error'];
}
Of course you need to do it in the else, when the user is not logged correctly.
else {
header("Refresh: 5; URL = login2.php");
exit;
}