首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PHP登录脚本只能运行一次

PHP登录脚本只能运行一次
EN

Stack Overflow用户
提问于 2017-08-10 01:35:41
回答 1查看 57关注 0票数 0

我已经创建了一个只工作一次的模式登录脚本。我不确定我哪里出错了。有时它会发送$_POST数据,有时不会,我搞不清楚到底是怎么回事。

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

代码语言:javascript
复制
<button onclick="document.getElementById('LoginModal').style.display='block'">Login</button>

下面是实际的表单:

代码语言:javascript
复制
<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的地方

代码语言:javascript
复制
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不会更新。当我第一次登录时,它工作得很好,然后每当我尝试更新它并以其他人的身份登录时,它都不起作用。我非常确定我的代码是健全的,只是它没有做我认为它应该做的事情。

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

我哪里搞砸了?

EN

Stack Overflow用户

回答已采纳

发布于 2017-08-10 03:00:53

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

代码语言:javascript
复制
setcookie("UserName",$_POST['UserName'],$Expiration,'/','.engsys.corp.ftr.com',0);
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45597236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档