PHP登录脚本只能运行一次

时间:2017-08-09 17:35:41

标签: javascript php login

我创建了一个只能运行一次的模态登录脚本。我不知道我哪里出错了。有时它会发送$_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

我在哪里弄乱?

1 个答案:

答案 0 :(得分:1)

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

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