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

我搜索了为什么无法设置会话时经历了这个SO Q / A。

On $_GET set $_SESSION won't work

我真的不知道这是否适用于我的情况。来了

index.php

session_start();

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

?>

...

然后,当用户进行身份验证时,我称以下内容。

authenticate.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

和我的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("Inscrire un nouveau VTT en inventaire");

if (("#addNewSledButtonDiv").is(":empty"))

$("#addNewSledButtonDiv").add("Inscrire un nouvel UTT en inventaire");

if (("#addNewUtvButtonDiv").is(":empty"))

$("#addNewUtvButtonDiv").add("Inscrire une nouvelle motoneige");

$("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

我有一个巨大的二维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…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值