如何在类的数据成员中存储会话数据?

时间:2015-02-03 07:17:16

标签: php codeigniter

class Project extends ci_Controller
    {
        public var $email=$this->session->userdata('email');
        if(!isset($_SESSION['email']))
        {
            echo "you are not logged in ";
            exit();
        }
}

错误消息:

  

解析错误:语法错误,意外' $ this' (T_VARIABLE)

2 个答案:

答案 0 :(得分:1)

尝试使用索引方法的代码:

class Project extends CI_Controller
    {
   function __construct() {
        parent::__construct();
}
function index(){
        $email = $this->session->userdata('email');
        if(!isset($_SESSION['email']))
        {
            echo "you are not logged in ";
            exit();
        }
}
}

答案 1 :(得分:0)

因为您正在访问$this外部函数/构造函数。(有关详细信息,请参阅@ Rizier123 注释)另请使用CI_Controller而不是ci_Controller尝试创建函数像

class Project extends CI_Controller
    {
        public function my_func() {
          $email=$this->session->userdata('email');
          if(!isset($_SESSION['email']))
          {
            echo "you are not logged in ";
            exit();
          }
       }
}