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!