[英]Yii2 form submission fails in the first attempt but works in the second attempt after clearing the web browser history
[英]Login attempt fails on first attempt, but works after that
我的登录 PHP代码在我的本地服务器上运行良好。 它也可以在实时服务器上运行,但始终在首次尝试时失败,而不会给出任何错误(验证或用户验证错误)。
无法将完整的代码放在实时服务器上。 在网上发现了很多问题,但都没有问题(即使在stackOverflow上也是如此)。
请帮忙!
登录页面:
<form class="form-signin" id="form-signin" action="./" name="LoginForm" role="form">
<h2 class="form-signin-heading">Please Sign In</h2>
<div class="input-group">
<span class="input-group-addon"></span>
<input type="text" class="form-control" id="username" name="username" placeholder="Username" autofocus>
</div>
<div class="input-group">
<span class="input-group-addon"></span>
<input type="password" class="form-control" id="password" name="password" placeholder="Password" >
</div>
<button type="submit" class="btn btn-lg btn-primary btn-block" id="signinBtn" data-loading-text="Verifying...">Sign in</button>
</form>
javascript:
$(document).ready(function(){
$("#form-signin").submit(function(){ // actions when form is submitted
$("#msg").hide(); //hide the danger alert
var btn = $("#signinBtn");
btn.button('loading'); // change sign-in button state to loading till ajax complete
username = $("#username").val();
password = $("#password").val();
if ((username==null || username=="") || (password==null || password=="") ) // i.e if any of the username password field is empty
{
$("#msg").html("Please Fill the form completely");
btn.button('reset');
$("#form-signin").effect('shake'); // shake effect --> needs jquery.ui
$("#msg").show(200);
return false;
}
else{
$.ajax({
type: "POST",
url: "verify.php",
data: "username="+username+"&password="+password,
success: function(verified){
// if user is verified redirect according to the level of user
if(verified == 'true1')
window.location = "http://example.com/page1.php";
else if(verified == 'true2')
window.location = "http://example.com/page2.php";
else if(verified == 'true3')
window.location = "http://example.com/page3.php";
else if(verified == 'false')
{
//else display error
$("#msg").html("<strong>Invalid</strong> username password combo!");
$("#form-signin").effect('shake');
$("#msg").show(200);
btn.button('reset');
}
}
});
return false;
}
}); // .submit function ends
}); //.ready function ends
PHP代码:
<?php
include("DataBaseLogin.php");
session_start();
$user = $_POST["username"];
$pass = $_POST["password"];
$user = htmlspecialchars($user);
$pass = htmlspecialchars($pass);
$user = mysql_real_escape_string($user, $db_handle);
$pass = mysql_real_escape_string($pass, $db_handle);
if ($db_found)
{
$result = mysql_query("SELECT * FROM user_table WHERE uname = '$user' AND pword = '$pass'");
$resultArray=mysql_fetch_assoc($result);
if($resultArray['uname']==$user && $resultArray['pword']==$pass)
{
//If user verified
$_SESSION['login'] = "1";
$_SESSION["current_user"]=$user;
$_SESSION['Level'] = $resultArray['Level'];
$Level = $_SESSION['Level'];
if($Level==1)
echo 'true1';
else
if($Level==2)
echo 'true2';
else if($Level==3)
echo 'true3';
}
else
{
$_SESSION['login'] = "";
echo 'false';
mysql_close($db_handle);
}
}
else
{
echo "db not found";
mysql_close($db_handle);
}
?>
第1页的代码//验证后
<?PHP
session_start();
include("DataBaseLogin.php");
if (!(isset($_SESSION['login']) && $_SESSION['login'] != ''))
{
header ("Location: index.php");
}
else if($_SESSION['Level'] !=1)
die("You are not allowed on this page");
?>
现在,第1页的html代码可以显示是否允许用户。
<?PHP
session_start();
include("DataBaseLogin.php");
if (isset($_SESSION['login']) && !empty($_SESSION['login']))
{
header ("Location: page1.php");
}
else if($_SESSION['Level'] !=1 || empty($_SESSION['login'])) {
header ("Location: index.php");
}
?>
尝试将其放在脚本的末尾:
session_write_close();
如果您使用的是类(例如,使用框架控制器时),请将其放在destruct函数中
function __destruct(){
session_write_close();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.