开发者

PHP Sessions problems

I'm working with a third party COM object from PHP and I want to save the object on a session variable to access it in future server calls.

If I save the object in session:

$_SESSION['collItem'] = $collItem;

I can access it's methods and properties thr开发者_开发知识库ough $_SESSION['collItem'] inmediately after definition.

But, in future calls to server, if I try to use $_SESSION['collItem'], I can't access it again.

I write here some more code to clarify.

Method to initailize COM object in my script "functions.php":

public function setAppData() {

    try {
        $appD = new COM('ASData.CASDataApp');
        $appD->InitMasterData(true, 1, 91);
        $appD->DateMask = 'ymd';
        $_readDB = $appD->InitApp($this->readDB());
    } catch (Exception $e) {
        $err = 'Connection error: ' . htmlentities(addslashes(strip_tags($e->getMessage())));
        $this->setError($err);
        return false;
    }

    $appD->appPath = str_replace('\app\include', '', __DIR__);

    $this->iniciarCollections($appD);

    $this->appData = $appD;
}

Call to method from my script "edit_json.php":

require_once('functions.php');

if (!session_id()) {
    session_start();
}

// We recover $mbw object saved in session and initialize COM object
if (isset($_SESSION['mbw'])) {
    $mbw = unserialize($_SESSION['mbw']);
}
$mbw->setAppData();
$appData = $mbw->getAppData();
$_SESSION['appData'] = $appData;

If I try access $_SESSION['appData'] inmediately after COM initialization I can work with it without problems, but if I try next code in future server calls (whith $appData object saved in $_SESSION['appData']:

     if (!session_id()) {
        session_start();
    }
    $appData = $_SESSION['appData']; 
    // I can't work with $appData object if I don't initialize it again

Reinitializing COM object isn't a good solution for me because I lose all changes I made.


Use session_start() at the beginning of your script. You need it to retrieve session data from the server into the $_SESSION variable.

From http://php.net/manual/en/function.session-start.php :

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.


You should edit your question and include your getAppData() method, as well, so we can do a better diagnostic. But what I can guess is that your getAppData() returns an object, and, in that case, when you have a code like

$appData = $mbw->getAppData();
$_SESSION['appData'] = $appData;

what PHP is doing is saving in $_SESSION['appData'] only a reference to the $mbw->getAppData() object, and not the real object.

If you want to store the real object in session, you would have to do the following:

$_SESSION['appData'] = serialize($appData);

and then, whenever you want to use the stored object, you would do:

if (isset($_SESSION['appData'])) $appData = unserialize($_SESSION['appData']);

on the beggining of every file that uses the $appData that was saved in the session.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜