当输入正确的ID和密码时,login.php页面会成功重定向到主页。现在,我想在输入错误的ID或密码时在login.php页面上显示错误消息。这是我的代码 -
login_save.php
<?php
function SignIn($loginname,$pass) {
include '../../common/common.php';
//require_once '../../common/createdbconn.php';
$login_id='';
$login_status='';
$error_message= '';
if(!empty($loginname) && !empty($pass)){ //checking the 'user' name and password, is it empty or have some text
$login_id=validate_login($loginname,$pass);
if ($login_id != '') {
header("location:homeaction.php?session_id=".session_id());
}
else {
// invalid user check
$error_message=INVALID_USERNAME;
//$_SESSION['error_message']=$error_message;
$login_status=$error_message;
session_unset();
session_destroy();
}
}
else {
//empty check
$error_message=NO_USERNAME_PASSWORD;
//$_SESSION['error_message']=$error_message;
$login_status=$error_message;
}
return $login_status;
}
function validate_login($loginname,$pass) {
require '../../common/createdbconn.php';
// validate user
$sql="SELECT id, fullname, designation_id from m_user where login_name='$loginname' and password='$pass'";
//$sql="SELECT mu.id as id, mu.fullname as fullname, mu.office_id as office_id, xurm.role_id as role_id from m_user mu, x_user_role_mapping xurm where mu.id = xurm.user_id and mu.login_name='$loginname' and mu.password='$pass'";
$result=mysqli_query($conn,$sql);
$login_id=mysqli_fetch_row($result);
session_start();
$sessionid = session_id();
$sql_sessionid="update m_user set session_id='$sessionid' where login_name='$loginname'";
$result=mysqli_query($conn,$sql_sessionid);
$_SESSION['login_id']=$login_id[0];
$_SESSION['fullname']=$login_id[1];
$_SESSION['logged_in']=true;
$_SESSION['loginname']=$loginname;
return $login_id[0];
}
?>
login_action.php
<?php
require_once '../../common/createdbconn.php';
require_once '../model/login_save.php';
$loginname=$_POST["user"];
$password=$_POST["pass"];
$pass=md5($password);
$login_status='';
$login_status=SignIn($loginname,$pass);
if ($login_status=='') { // Validation passed
session_start(); //starting the session for user profile page
}
else {$error ="Username or Password is invalid";
header("location:../view/login.php?login_status=$login_status");
session_destroy();
}
?>
的login.php
<!DOCTYPE html>
<?php
require '../../common/createdbconn.php';
?>
<html>
<head>
<meta charset="UTF-8">
<title>CBS HELPDESK,ASSAM CIRCLE</title>
<link rel="icon" href="../../images/IP.png" type="image/png" sizes="100x56">
<link rel="stylesheet" href="../../css/style.css">
</head>
<body>
<img src="../../images/indiapost.jpg" style="width:1600px;height:120px;">
<h1>Welcome to Circle Processing Centre-Assam</h1>
<div class="wrapper">
<div class="container">
<h2>Login to Continue</h2>
<form method="post" action="../controller/login_action.php" class="form">
<input id="user" name="user" type="text" placeholder="Enter Your UserID">
<input id="pass" name="pass" type="password" placeholder="Enter Your Password">
<button id="login" name="login" type="submit">Login</button>
</form>
</div>
</div>
</body>
</html>