简体   繁体   中英

PHP: $_SESSION doesn't seem to get set. Any idea?

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!

Make sure that all pages that use $_SESSION variables have session_start(); declared before any output.

It looks like authenticate.php doesnt have session_start(); declared, but uses a session request below.

$_SESSION["authenticated"] = $isAuthentic;

This will cause problems unless you have included/required the file authenticate.php , and the parent file has the session_start(); declared.

NB:

session_start(); will either create a session or resume one. It is required on every page that uses session variables.

php.net

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.

In addition to checking that you have session_start(), I have run into issues with session variables not reporting values correctly if they have not been declared before checking if they are set or checking their value.

So try placing this:

$_SESSION["authenticated"];

Before your:

if(!isset($_SESSION["authenticated"])) $_SESSION["authenticated"] = false;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM