PHP 登录脚本只工作一次



我创建了一个只能工作一次的模式登录脚本。我不确定我哪里出错了。有时它会发送$_POST数据,有时不会。我不知道发生了什么。

以下是我手动调用登录脚本的方法:

<button onclick="document.getElementById('LoginModal').style.display='block'">Login</button>

这是实际形式:

<form class="modal-content animate" method="post" action="<?php echo $_SERVER['HTTP_REFERER']; ?>">
<div class="imgcontainer">
<img src="/Images/FrontierLogo294-117.png" alt="Avatar" class="avatar">
</div>
<div>
<label><b>UserName</b></label>
<input class="Login" type="text" placeholder="UserName required (use CorpID)" name="UserName" required><br>
<label><b>Password</b></label>
<input class="Login" type="password" placeholder="Password not currently required" name="password">
<button type="submit" class="Green">Login</button>
</div>
<div class="container" style="background-color: #f1f1f1">
<button type="button" onclick="document.getElementById('LoginModal').style.display='none'" class="cancelbtn">Cancel</button>
<!--span class="psw">Forgot <a href="#">password?</a></span-->
</div>
<?php echo $_SERVER['HTTP_REFERER'];
//error_log(date("Y/m/d h:i:sa")." LoginModal.php line 16 HTTP_REFERER: " .$_SERVER['HTTP_REFERER']. "n",3,'D:WebContentengsys.corp.ftr.comHelperLogPHP.txt'); ?>
</form>
<script>
// Get the modal
var modal = document.getElementById('LoginModal');
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>

这是我尝试在Cookie中设置 CorpID 的地方:

if(isset($_POST['UserName']))
{
$UserName = $_POST['UserName'];    
if(isset($UserName))
{
$Expiration = time() + (60*60*24*7);
if(isset($_COOKIE['UserName']))
{
setcookie("UserName",$_COOKIE['UserName'],$Expiration,'/','.engsys.corp.ftr.com',0);
setcookie("CookieTime",$Expiration,time() + (60*60*24*7),'/','.engsys.corp.ftr.com',0);
error_log(date("Y/m/d h:i:sa")." AdminPage.php line 40 Cookie: " .$_COOKIE['UserName']. "n",3,'D:WebContentengsys.corp.ftr.comHelperLogPHP.txt');
echo "<script>location.href = 'http://" .$_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']. "'</script>";
}
else
{
setcookie("UserName",$UserName,$Expiration,'/');
setcookie("CookieTime",$Expiration,time() + (60*60*24*7),'/','.engsys.corp.ftr.com',0);
error_log(date("Y/m/d h:i:sa")." AdminPage.php line 47 Cookie: " .$_COOKIE['UserName']. "n",3,'D:WebContentengsys.corp.ftr.comHelperLogPHP.txt');
echo "<script>location.href = 'http://" .$_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']. "'</script>";
}
}
}
else
{
error_log(date("Y/m/d h:i:sa")." AdminPage.php line 61 UserName was not set!n",3,'D:WebContentengsys.corp.ftr.comHelperLogPHP.txt');
}  

我已经尝试过这个,没有重新加载页面echo,但我仍然得到相同的结果,$_COOKIE没有得到更新。当我第一次登录时,它工作正常,然后每当我尝试更新它并以其他人身份登录时,它都不起作用。我很确定我的代码是合理的,只是没有做我认为应该做的事情。

这一切都在同一个文件中,除了模态的代码位于一个included的单独文件中。这些文件分别称为AdminPage.phpLoginModal.php

我在哪里搞砸了?

如果您第一次以userA身份登录,您将点击第 47 行,并且 cookieUserName将设置为值userA。如果您随后按userB登录,它将点击第 40 行。但是作为 cookie 值,您不使用新用户名,而是使用已存储在 cookie 中的用户名!使用$_POST['UserName']而不是$_COOKIE['UserName']

setcookie("UserName",$_POST['UserName'],$Expiration,'/','.engsys.corp.ftr.com',0);

最新更新