drkxgs9358 2014-10-21 08:34
浏览 115
已采纳

未定义的变量:类中的_SESSION

Strange Situation. I have a Projects Class which has the following constructor

public function __construct($db){
    $this->db = $db;
    $this->lang = strtolower($_SESSION['la']);
}

I'm using AJAX to call a simple get() function but the error returned is

<b>Notice</b>:  Undefined variable: _SESSION in <b>D:\Sites\proman\class\Projects.php</b> on line <b>10</b><br />

In my index.php I have session_start() so that's not the problem. I can even print_r($_SESSION) in the construct method and i see the session variable of 'la'.##

I tried adding session_start() also to the top of my Projects.php class but then I get the warning that session has already started.

This is my session init at the top of index.php ## it also set's a default language (la) variable if none is chosen by the user

<?php
    session_start();
    if(!isset($_SESSION['la'])) $_SESSION['la'] = "EN";
?>

This is the output of the print_r() in the Projects Constructor ## Array ( [la] => FR ) Any idea whay might be going wrong here? Thanks!

  • 写回答

1条回答 默认 最新

  • du13520157325 2014-10-21 09:18
    关注

    I think

    <b>Notice</b>:  Undefined variable: _SESSION in <b>D:\Sites\proman\class\Projects.php</b> on line <b>10</b><br />
    

    is the result of your ajax call. Right?

    The issue is that you are missing session_start() in the file requested with ajax. I think the file is Projects.php in you class. Since you are using ajax, not include/require the session_start() on your index.php page is not sufficient. Separate session_start() is needed for the ajax page (Projects.php here).

    Add a new session_start() on your ajax page(Projects.php) and let me know whether it fixed the issue.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配