简体   繁体   中英

Why does session_start() function does not work in php constructor?

This is my below code

first.php

class Session
{
   function __construct()
   {
      session_start();
   }
   function start()
   {
      $_SESSION['id'] = "1";
   }
}

second.php

 require "first.php";
 $session = new Session();
 $session->start();
 echo "Session Id ".$_SESSION['id'];

It gives me an error that _SESSION not defined So i have to define session_start in start function and then it worked but i want to use it in constructor!!

我已经在PC(Ubuntu 16.4)上尝试过您的代码,并且可以正常工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM