我在通过这个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_start():
所以尽量把这个:
你之前? – TheNytangel
@Nytangel:不,我不知道。只有'index.php'可以开始一个会话。然后,我主要使用Ajax来更改和加载页面上的各个部分,全部使用'.post()','$ .ajax()','$(“#div_id”).load()'等。 –
session_start()在所有PHP页面上都是必需的。如果已经有cookie告诉PHP有一个现有的会话,它会正确地填充$ _SESSION []。如果它是一个干净的开始,它会做初始化。因此,将session_start()放在每个有你想要的PHP文件上。 –