php - css 因未知原因覆盖 php

标签 php html css cookies

由于某些原因,在以下代码中,如果 css 中指示的行存在,则不会在登录屏幕上设置 cookie。其他 css 元素也可以看到类似的行为。我已经在这里待了 3 天了,不明白为什么。

对于那些测试,用户名是'test',密码是'debug'

感谢所有帮助,并提前致谢。

<html>
    <head>
        <?php

$LOGIN_INFORMATION = array(
  'test' => 'debug'
);

// time out after NN minutes of inactivity. Set to 0 to not timeout
define('TIMEOUT_MINUTES', 10);

define('TIMEOUT_CHECK_ACTIVITY', true);

$GLOBALS['programname'] = "Testing";



            define('LOGOUT_URL', 'test.php');
            define('USE_USERNAME', true);
            $timeout = (TIMEOUT_MINUTES == 0 ? 0 : time() + 
TIMEOUT_MINUTES * 60);
            if(isset($_GET['logout'])) {
                setcookie("verify", '', $timeout, '/'); // clear 
password;
                header('Location: ' . LOGOUT_URL);
                exit();
            }
            if(isset($_COOKIE['user'])){
                $usr= $_COOKIE['user'];
            }
        ?>
        <link href="https://fonts.googleapis.com/css?family=Rubik" 
rel="stylesheet">
        <style>

label, p, a, h1, h2, h3, th, input{
    font-family: 'Rubik', sans-serif;
}
h1.header{
    font-weight: bold;
    font-size:55px;
}
div.navigation{
    text-align: justify;
    text-justify: distribute-all-lines;
    border: 2px solid #000000;
    border-radius: 5px;
    background-color: #f2f2f2;
    height:30px;
}
div.nav{
    vertical-align:top;
    display: inline-block;
    *display: inline;
}
div.nav p{
    margin:auto;
    transform: translateY(40%);
}
span.stretch {
    width: 100%;
    display: inline-block;
    font-size: 0;
    line-height: 0;
}
body{
    margin:40px;
    margin-top:20px;
    min-width:795px;
}
form, h1, table{
    margin: 10px;
}
table {
    border-collapse: collapse;
    width: 80%;
    empty-cells: show;
}
th, td {
    text-align: left;
    padding: 8px;
}
tr:nth-child(even){
    background-color: #f7f7f7;
}
tr:nth-child(odd){
    background-color: #ffffff;
}
th:first-child, table td.line:nth-child(2){
    border-radius: 5px 0px 0px 5px;
}
th:last-child, table td.line:last-child{
    border-radius: 0px 5px 5px 0px;
}
th {
    background-color: #4CAF50;
    color: white;
}
div.img { 
    display: none;
    position: fixed;
    top: 10px;
    left: 10px;
}
div.img img{
    width: 20vw;
    border-radius: 10px;
}
a{
    text-decoration:none;
    color:black;
}
a p{
    margin-left:10px;
}
a:hover p{
    color:#33cc33;
    text-decoration: underline;
}
p.img:hover{
    color:#33cc33;
    text-decoration: underline;
}
a:hover div.img { 
    display: block;
}
td.line1{
    background-color: #f2f2f2;
}
label{
    display: inline-block;
    margin-left:10px;
}
div.form-group{
    display: inline-block;
}
table td.line:last-child{
    border-right: 1px solid #DDD;
}
td.line{
    background-color: #4CAF50;
    color: white;
}
input[type=text], input[type=password], input[type=date], 
input[type=number], input[type=file], select {
    padding: 12px 20px;
    margin: 8px 0;
    display: inline-block;
    border: 1px solid #ccc;
    border-radius: 4px;
    box-sizing: border-box;
    background-color: #ffffff;
}
input[type=submit], input[type=button]{
    width: 100px;
    background-color: #4CAF50;
    color: white;
    padding: 14px 20px;
    margin: 8px 0;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}
input[type=submit]:hover {
    background-color: #45a049;
}
select{
   -webkit-border-radius: 5px;
   -moz-border-radius: 5px;
   border-radius: 5px;
   height: 40px;
   background-color: #ffffff;
   overflow: hidden;
}
select select {
   background: transparent;
   border: none;
   font-size: 14px;
   height: 29px;
   padding: 5px;
   width: 268px;
}
p.small{
    font-size:15px;
    margin-left:10px;
}
input.red{
    background-color: #CF0000;
}
input.red:hover{
    background-color: #Bd0000;
}
div.border{
    border: 2px solid #000000;
    border-radius: 5px;
    background-color: #f2f2f2;
    padding: 20px;
}
.border, .fadeonce, .img{
    animation: fadein 1s;
    -moz-animation: fadein 1s;
    -webkit-animation: fadein 1s;
    -o-animation: fadein 1s;
}
@keyframes fadein {
    from {
        opacity:0;
        max-height:1px;
        overflow: hidden;                       /*   DELETE THIS LINE 
TO SEE EXPECTED BEHAVIOR */
    }
    to {
        opacity:1;
        max-height:100%;
        overflow: hidden;                       /*   DELETE THIS LINE 
TO SEE EXPECTED BEHAVIOR */
    }
}
@-moz-keyframes fadein {
    from {
        opacity:0;
        max-height:1px;
        overflow: hidden;                       /*   DELETE THIS LINE 
TO SEE EXPECTED BEHAVIOR */
    }
    to {
        opacity:1;
        max-height:100%;
        overflow: hidden;                       /*   DELETE THIS LINE 
TO SEE EXPECTED BEHAVIOR */
    }
}
@-webkit-keyframes fadein {
    from {
        opacity:0;
        max-height:1px;
        overflow: hidden;                       /*   DELETE THIS LINE 
TO SEE EXPECTED BEHAVIOR */
    }
    to {
        opacity:1;
        max-height:100%;
        overflow: hidden;                       /*   DELETE THIS LINE 
TO SEE EXPECTED BEHAVIOR */
    }
}
@-o-keyframes fadein { /* Opera */
    from {
        opacity:0;
        max-height:1px;
        overflow: hidden;                       /*   DELETE THIS LINE 
TO SEE EXPECTED BEHAVIOR */
    }
    to {
        opacity: 1;
        max-height:100%;
        overflow: hidden;                       /*   DELETE THIS LINE 
TO SEE EXPECTED BEHAVIOR */

    }
}

        </style>
        <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
        <META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
        <?php
            if(!function_exists('showLoginPasswordProtect')) {
                // show login form
                function showLoginPasswordProtect($error_msg) {
        ?>
    </head>
    <body>
        <center>
            <h1 class="header"><?php echo $GLOBALS['programname']; ?>
</h1>
        </center>
        <br>
        <div style="width:500px; margin-left:auto; margin-right:auto; 
text-align:center">
            <div class="border">
                <form method="post">
                    <h2>Please enter your password</h2>
                    <font color="red"><?php echo $error_msg; ?>
</font><br />
                    <?php if (USE_USERNAME) echo '<p>Login:</p><input 
type="text" name="access_login" /><p>Password: </p>'; ?>
                    <input type="password" name="access_password" />
                    <br><br>
                    <input type="submit" name="Submit" value="Submit" 
/>
                </form>
            </div>
        </div>
    </body>
</html>
<?php
                    // stop at this point
                    die();
                }
            }
            // user provided password
            if (isset($_POST['access_password'])) {
                $login = isset($_POST['access_login']) ? 
$_POST['access_login'] : '';
                $pass = $_POST['access_password'];
                if (!USE_USERNAME && !in_array($pass, 
$LOGIN_INFORMATION) || (USE_USERNAME && ( !array_key_exists($login, 
$LOGIN_INFORMATION) || $LOGIN_INFORMATION[$login] != $pass ))){
                    showLoginPasswordProtect("Incorrect password.");
                }else{
                    // set cookie if password was validated
                    setcookie("verify", md5($login.'%'.$pass), 
$timeout, '/');
                    setcookie("user", $login, $timeout, '/');
                    $usr = $login;                                              
                    unset($_POST['access_login']);
                    unset($_POST['access_password']);
                    unset($_POST['Submit']);
                }
            }else{
                // check if password cookie is set 
                if (!isset($_COOKIE['verify'])) {
                    showLoginPasswordProtect("");
                }
                // check if cookie is good
                $found = false;
                foreach($LOGIN_INFORMATION as $key=>$val) {
                    $lp = (USE_USERNAME ? $key : '') .'%'.$val;
                    if ($_COOKIE['verify'] == md5($lp)) {
                        $found = true;
                        // prolong timeout
                        if (TIMEOUT_CHECK_ACTIVITY) {
                            setcookie("verify", md5($lp), $timeout, 
'/');
                            setcookie("user", $usr, $timeout, '/');
                        }
                        break;
                    }
                }
                if (!$found) {
                    showLoginPasswordProtect("");
                }
            } 
        ?>

    </head>
    <body>
        <center>
            <h1 class="header"><?php echo $GLOBALS['programname']; ?>
</h1>
        </center>
        <br>
        <div class="navigation  <?php if ($_GET['page'] == "") {echo 
' fadeonce';}?>">
            <div class="nav"></div>
            <a href="?page=1">
                <div class="nav">
                    <p>test1</p>
                </div>
            </a>
            <a href="?page=2">
                <div class="nav">
                    <p>test2</p>
                </div>
            </a>
            <a href="?page=3">
                <div class="nav">
                    <p>test3</p>
                </div>
            </a>
            <a href="?page=4">
                <div class="nav">
                    <p>test4</p>
                </div>
            </a>
            <a href="?page=0">
                <div class="nav">
                    <p>test0</p>
                </div>
            </a>
            <a href="?logout=1">
                <div class="nav">
                    <p>Logout <?php echo $usr;?></p>
                </div>
            </a>
            <div class="nav"></div>
            <span class="stretch"></span>
        </div>
        <br>

        <div class="border" <?php if ($_GET['page'] != "0") {echo 
'style="display: none;"';}?>>
            <h1>Test 0</h1>
            <p>this is a test page</p>
        </div>

        <div class="border" <?php if ($_GET['page'] != "1") {echo 
'style="display: none;"';}?>>
            <h1>Test 1</h1>
            <p>this is a test page</p>
        </div>

        <div class="border" <?php if ($_GET['page'] != "2") {echo 
'style="display: none;"';}?>>
            <h1>Test 2</h1>
            <p>this is a test page</p>
        </div>

        <div class="border" <?php if ($_GET['page'] != "3") {echo 
'style="display: none;"';}?>>
            <h1>Test 3</h1>
            <p>this is a test page</p>
        </div>

        <div class="border" <?php if ($_GET['page'] != "4") {echo 
'style="display: none;"';}?>>
            <h1>Test 4</h1>
            <p>this is a test page</p>
        </div>

        <div  <?php if ($_GET['page'] == "") {echo 
'class="fadeonce"';}?>>
            <p><b>Footer Text</p>
        </div>
    </body>
</html>

最佳答案

不要认为这与 css 代码有关。问题可能是,在 header 已发送后,您无法发送 cookie。

在你的情况下,你必须把 setcookie <html> 之前的代码

<?php
// ...
setcookie(...);
//...
?>
<html>
    <head>
    ...

关于php - css 因未知原因覆盖 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43644205/

相关文章:

html - CSS 中的填充背景/文本填充?

css - Select2 css 元素 - 如何删除顶行

jquery - 实时显示高度变化

c# - CKEditor 可以在 WinForms 应用程序中用于 (X)HTML 编辑吗?

javascript - AngularJS 和大 JSON 数据

php - Yii2 Gridview - 如何在页脚属性上使用总计

php - 如何从php中的链接下载视频?

javascript - NodeJS + Hapi + CSS 麻烦

php - 如何从javascript调用表单提交?

php - 找不到存储库方法