我搜索了为什么无法设置会话时经历了这个SO Q / A。

On $_GET set $_SESSION won't work

我真的不知道这是否适用于我的情况。来了

index.php

<?php
session_start();

if (!isset($_SESSION["authenticated"])) $_SESSION["authenticated"] = false;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
...


然后,当用户进行身份验证时,我称以下内容。

authenticate.php

<?php
require_once "data/data_access.php";

$userName = "";
$password = "";

if (isset($_REQUEST["userName"])) $userName = $_REQUEST["userName"];
if (isset($_REQUEST["password"])) $password = $_REQUEST["password"];

$isAuthentic = isAuthenticUser($userName, $password);
$_SESSION["authenticated"] = $isAuthentic;
echo $isAuthentic;
?>


我还每隔5秒检查一次身份验证内容,以便为用户所在的部分创建按钮。

authenticated.php

<?php echo isset($_SESSION["authenticated"]) && $_SESSION != false ? 'true' : 'false'; ?>


和我的Ajax呼叫设置:

my_project.js

$(document).ready(function() { startAuthenticationChecking(); });

function startAuthenticationChecking() {
    setInterval(function() { ajaxSession("authenticated.php"); }, 5000);
}

function ajaxSession(actionURL) {
    var authenticated = false;
    $.ajax({
        async: false,
        url: actionURL,
        success: function(authenticated) {
            alert(authenticated);
            if (authenticated) {
                if (("#addNewAtvButtonDiv").is(":empty"))
                    $("#addNewAtvButtonDiv").add("<button id='newAtvButton'>Inscrire un nouveau VTT en inventaire</button>");
                if (("#addNewSledButtonDiv").is(":empty"))
                    $("#addNewSledButtonDiv").add("<button id='newSledButton'>Inscrire un nouvel UTT en inventaire</button>");
                if (("#addNewUtvButtonDiv").is(":empty"))
                    $("#addNewUtvButtonDiv").add("<button id='newUtvButton'>Inscrire une nouvelle motoneige</button>");
                $("button").button();
            } else {
                $("#addNewAtvButtonDiv").children().remove();
                $("#addNewSledButtonDiv").children().remove();
                $("#addNewUtvButtonDiv").children().remove();
            }
        }
    });
}



我的问题


尽管我在$_SESSION["authenticated"] = false之后设置session_start();,但是当ajaxSession()询问用户是否通过authenticated.php进行身份验证时,它总是返回'false'。使用具有适当凭据的authenticate.php进行身份验证后的事件。

相关问题:PHP / Ajax : How to show/hide DIV on $_SESSION variable value?

任何帮助欢迎!最近,我已经为此工作了几天和几天,现在仍然如此。

谢谢!

最佳答案

确保所有使用$_SESSION变量的页面在任何输出之前都已声明session_start();

看起来authenticate.php没有声明session_start();,但是在下面使用了会话请求。


$ _SESSION [“ authenticated”] = $ isAuthentic;


除非您包含/需要文件authenticate.php,并且父文件声明了session_start();,否则这将导致问题。

N.B:

session_start();将创建一个会话或恢复一个会话。使用会话变量的每个页面上都需要它。

php.net


session_start()创建会话或基于通过GET或POST请求传递或通过cookie传递的会话标识符恢复当前会话。

关于php - PHP:$ _SESSION似乎没有设置。任何的想法?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19149772/

10-12 00:05
查看更多