2

I got through this SO Q/A while searching why my session wouldn't get set.

On $_GET set $_SESSION won't work

I don't really get whether this applies to my case. Here it goes.

index.php

<?php 
session_start();

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

Then, I call the following when the user authenticates himself.

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;
?>

I also have this authentication-thing checked every 5 seconds in order to create the buttons for the section the user is in.

authenticated.php

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

and my Ajax call setup:

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();
            }
        }
    });
}

My Problem

Though I set $_SESSION["authenticated"] = false right after the session_start();, when the ajaxSession() asks whether a user is authenticated through authenticated.php, it always returns 'false'. Event after an authentication using authenticate.php with proper credentials.

Related question: PHP / Ajax : How to show/hide DIV on $_SESSION variable value?

Any help welcome! I've been working on this for a few nights and days lately, and still am.

Thanks!

4

2 回答 2

5

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

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

$_SESSION["authenticated"] = $isAuthentic;

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

注意:

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

php.net

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

于 2013-10-03T02:39:27.413 回答
2

除了检查您是否有 session_start() 之外,如果在检查它们是否已设置或检查它们的值之前没有声明它们,我还遇到了会话变量无法正确报告值的问题。

所以尝试放置这个:

$_SESSION["authenticated"];

在您之前:

if(!isset($_SESSION["authenticated"])) $_SESSION["authenticated"] = false;
于 2013-10-03T03:11:00.677 回答