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

未定义的变量:类中的_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!

图片转代码服务由CSDN问答提供 功能建议

奇怪的情况。 我有一个Projects类,它有以下构造函数

  public function __construct($ db){
 $ this-&gt; db = $ db; 
 $ this-&gt  ; lang = strtolower($ _ SESSION ['la']); 
} 
   
 
 

我正在使用AJAX来调用一个简单的get()函数但错误 返回

 &lt; b&gt;注意&lt; / b&gt ;:未定义变量:_SESSION in&lt; b&gt; D:\ Sites \ proman \ class \ Projects.php&lt; / b&gt  ; 在线&lt; b&gt; 10&lt; / b&gt;&lt; br /&gt; 
   
 
 

在我的index.php中,我有session_start(),所以这不是问题。 我甚至可以在构造方法中print_r($ _ SESSION),我看到'la'的会话变量。##

我尝试将session_start()添加到我的项目顶部。 php类,但后来我得到会话已经开始的警告。

这是我在index.php顶部的会话init ## it也设置了默认语言(la)变量 如果用户没有选择

 &lt;?php 
 session_start(); 
 if(!isset($ _ SESSION ['la']))$ _SESSION [  'la'] =“EN”; 
?&gt; 
   
 
 

这是Projects Constructor ## Array中print_r()的输出( [la] =&gt; FR) 任何想法都可能在这里出错? 谢谢!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题