繁体   English   中英

PHP登录脚本只能运行一次

[英]PHP login script only works once

我创建了一个只能运行一次的模式登录脚本。 我不确定哪里出了问题。 有时它将发送$_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:\WebContent\engsys.corp.ftr.com\Helper\LogPHP.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:\WebContent\engsys.corp.ftr.com\Helper\LogPHP.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:\WebContent\engsys.corp.ftr.com\Helper\LogPHP.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:\WebContent\engsys.corp.ftr.com\Helper\LogPHP.txt');
}  

我已经尝试过在没有echo下重新加载页面,但是仍然得到相同的结果, $_COOKIE不会得到更新。 首次登录时,它工作正常,然后每当我尝试对其进行更新并以其他人身份登录时,它均无法正常工作。 我很确定我的代码是正确的,只是没有按照我的预期去做。

除了用于模态的代码在include d的单独文件中之外,所有内容都在同一文件中。 这些文件分别称为AdminPage.phpLoginModal.php

我在哪里弄糟?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM