I am new in web development so please understand me.
After I click the button to submit in PHP in HTML it only reloads my login.html
Can someone help me to redirect the login page into index page if the credentials of the user is correct.
Here is the code:
HTML:
<?php include('php/pLogin.php'); ?>
<form action="" method="POST">
<div class="login-form-head">
<h4>Sign In</h4>
<p>Hello there, Sign in and start managing your Admin
Template</p>
</div>
<div class="login-form-body">
<div class="form-gp">
<label for="txtUser">Email address</label>
<input type="text" id="txtUser" name="txtUser">
<i class="ti-email"></i>
</div>
<div class="form-gp">
<label for="txtPass">Password</label>
<input type="password" id="txtPass" name="txtPass">
<i class="ti-lock"></i>
</div>
<div class="row mb-4 rmber-area">
<div class="col-6">
<div class="custom-control custom-checkbox mr-sm-2">
<input type="checkbox" class="custom-control-input" id="customControlAutosizing">
<label class="custom-control-label" for="customControlAutosizing">Remember Me</label>
</div>
</div>
<div class="col-6 text-right">
<a href="#">Forgot Password?</a>
</div>
</div>
<div class="submit-btn-area">
<button id="btnLogin" type="submit" value="Submit">Login <i class="ti-arrow-right"></i></button>
</div>
<div class="form-footer text-center mt-5">
<p class="text-muted">Don't have an account? <a href="register.html">Sign up</a></p>
</div>
</div>
</form>
PHP: Login.php
<?php
include("pConfig.php");
session_start();
if($_POST['txtUser'] != '' && $_POST['txtPass'] !='') {
$error = '';
// username and password sent from form
echo $myusername = mysqli_real_escape_string($db,$_POST['txtUser']);
echo $mypassword = mysqli_real_escape_string($db,$_POST['txtPass']);
//$sql = "SELECT user_id FROM user WHERE username = '$myusername' and password = '$mypassword'";
$sql = "SELECT * FROM userinfo WHERE (UserName = '$myusername' OR Email = '$myusername' ) and Password = '$mypassword'";
$result = mysqli_query($db,$sql);
$rows = mysqli_fetch_array($result);
$count = mysqli_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count == 1) {
//prevent session fixation attack
session_regenerate_id();
$_SESSION['login_user'] = $myusername;
header("Location: index.html");
} else {
$error = 'Incorrect UserName Or Password, Please Check your Credentials';
}
}
?>
Config.php
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'ci');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>
That is because the action attribute in your form is empty
action=""
you need to point it your php file like this:
action="/path/to/your/file.php"
If the action attribute is empty,it will point to/refresh the file that contains the form.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.