I have recently been asked to work on a legacy code base written using Yii 1.1 and Php 5.3. We have a custome session implementation calling a rest api. The code looks something like below:-
CSessionService extends CHTTPSession {
public function open()
{
Session::setJSessionId();
parent::open();
}
public function getUseCustomStorage()
{
return true;
}
public function regenerateID($deleteOldSession=false)
{
Session::setRegeneratedSessionId(
SessionServiceAdapter::getInstance()->generateSessionId(), $deleteOldSession
);
}
public function readSession($id)
{
return SessionServiceAdapter::getInstance()->getSessionData($id);
}
public function writeSession($id,$data)
{
return SessionServiceAdapter::getInstance()->replaceSession($id, $this->toArray());
}
public function destroySession($id)
{
if($id!=='')
{
SessionServiceAdapter::getInstance()->deleteSession($id);
@session_unset();
@session_destroy();
}
}
}
The helper session class looks like below:-
class Core_Session
{
protected static $_cache = array();
public static function generateId()
{
session_regenerate_id(false);
}
public static function add($key, $data)
{
self::$_cache[$key] = $data;
if ($data instanceof Transfer_Interface) {
$data = array(
'class' => get_class($data),
'data' => $data->toArray()
);
}
Yii::app()->session->add($key, $data);
}
public static function get($key)
{
if (!isset(self::$_cache[$key])) {
$data = Yii::app()->session->get($key);
if (is_array($data) && array_key_exists('class', $data)) {
$className = $data['class'];
$data = new $className($data['data']);
}
self::$_cache[$key] = $data;
return $data;
} else {
return self::$_cache[$key];
}
}
public static function remove($key)
{
$value = Yii::app()->session->remove($key);
if (isset(self::$_cache[$key])) {
unset(self::$_cache[$key]);
}
return $value;
}
public static function isRegistered($key)
{
if (isset(self::$_cache[$key])) {
return true;
}
$result = in_array($key, Yii::app()->session->getKeys());
return $result;
}
public static function getId()
{
return Yii::app()->session->sessionID;
}
public static function generateSessionId($customerId = null)
{
return SessionServiceAdapter::getInstance()->generateSessionId();
}
public static function setJSessionId() {
session_id(static::generateSessionId());
}
}
The autostart is set to true. I am facing a problem in which after Session open I am not seeing the $_SESSION getting populated even though readSession is getting called. Also session_start is called before trying to access $_SESSION?
Can someone let me know what could be going wrong? Why $_SESSION is not getting populated?
If I modify my readSession like below:-
public function readSession($id)
{
$val = SessionServiceAdapter::getInstance()->getSessionData($id);
$_SESSION = $val;
return $val;
}
Then I am seeing $_SESSION is populated. But after going over the documentation of Yii it appears to me that I should not be explicitly setting $_SESSION. That Yii should automatically do if I am calling session_start before accessing $_SESSION.