PHP:$ _SESSION似乎没有设置。任何的想法? - php

我搜索了为什么无法设置会话时经历了这个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 session header()重定向后丢失 - php

这是我第一次尝试创建会话。另外,成功登录后,我使用header()函数重定向页面,但是在重定向的页面上,我不再有会话。有代码:建立工作阶段:function userLogin($user){ session_start(); $_SESSION['username'] = $user; header("Location: /~…

PHP Count数组元素 - php

嗨,有人可以解释为什么这会返回“数组由0个元素组成”。 :$arr = array(1,3,5); $count = count($arr); if ($count = 0) { echo "An array is empty."; } else { echo "An array has $count elements.…

PHP:从函数返回值并直接回显它? - php

这可能是一个愚蠢的问题,但是……的PHPfunction get_info() { $something = "test"; return $something; } html<div class="test"><?php echo get_info(); ?></div> 有没有办…

PHP:将数据从二维数组复制到一维数组的最快方法 - php

我有一个巨大的二维PHP数组,带有500万行。$t = [ [ "id" => 1, "name" => "foo" ], [ "id" => 2, "name" => "bar" ] ]; 现在,我必须将此数组的I…

php-printf和sprintf具有不同的输出 - php

我编写了以下微型php程序来测试printf和sprintf:<?php $str_1 = printf("%x%x%x", 65, 127, 245); $str_2 = sprintf("%x%x%x", 65, 127, 245); echo $str_1 . "\n"; echo $s…