PHP - GET 请求未正常运行

时间:2021-04-16 22:45:13

标签: javascript php html

假设我的域名为 example。我创建了一个名为 sub 的子域,其中存储了所有内容。我有一个登录系统,登录系统存储 cookie 的值:teamlastLoginauth。登录后,我检查了 cookie,所有 cookie 都设置正确。现在,我有一个如下所示的代码:

<?php

setlocale(LC_TIME, array('no_NB .UTF-8','no_NB@euro','no_NB','norwegian'));
include "db_connect.php"; // Using database connection file here
date_default_timezone_set('Europe/Oslo');


if (isset($_COOKIE["auth"])) {
    $stmt = $link -> prepare('SELECT ident FROM users WHERE auth = ?');
    $stmt -> bind_param('s', $_COOKIE["auth"]); 
    $stmt -> execute();
    $stmt -> store_result();
    $stmt -> bind_result($myIdent);
    if($stmt->num_rows == 0) {
        logout();
    }
    $stmt -> fetch();
    $stmt -> close();
    
    $auth = $_COOKIE["auth"];
    
    if (isset($_COOKIE["team"])) {
        $myTeam = $_COOKIE["team"];
    }
    
    if (isset($_COOKIE["lastLogin"])) {
        $lastLogin = $_COOKIE["lastLogin"];
    }
    
} else {
    logout();
}

function logout() {
    
    $past = time() - 3600;
    foreach ( $_COOKIE as $key => $value )
    {
        setcookie( $key, $value, $past, '/' );
    }
    
    header("Location: login.php");
    exit;
}

if (isset($_GET['selectedTeam'])) {
    $selectedTeam = $_GET["selectedTeam"];
} else {
    $selectedTeam = $myTeam;
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Home</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel='stylesheet' href='https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css'>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="https://kit.fontawesome.com/f10f36656a.js" crossorigin="anonymous"></script>
</head>

<body>
    <center>
        <div class="selectedTeam" style="color:black;margin: 5px;">
            <form action="#" method="POST">
                <label for="teams">Velg Team:</label>
                <select name="teams" id="teams" onchange="selectedTeam()" style="background:none;border:none;font-weight: bold;font-size:12px;">
                    <option value="both">Alle Teams</option>
                    <option value="2">Team 2</option>
                    <option value="3">Team 3</option>
                </select>
            </form>
        </div>
    </center>
</body>

<script>
var my_var = <?php echo json_encode($selectedTeam); ?>;
document.getElementById('teams').value = my_var;

function selectedTeam() {
    window.location.href = "https://sub.example.com/index.php?selectedTeam=" + document.getElementById('teams').value;
}
</script>

因此,当我登录时,我是团队 2,并且团队 cookie 中的值为 2。但是当将下拉选择更改为团队 3 时,它会重新加载页面并让我自动注销。这是为什么?我做错了什么?

0 个答案:

没有答案