/** * @inheritdoc */ public function writeSession($id, $data) { // exception must be caught in session write handler // http://us.php.net/manual/en/function.session-set-save-handler.php try { $userId = new Expression('NULL'); if (!Yii::$app->user->isGuest) { $userId = Yii::$app->user->id; } $expire = time() + $this->getTimeout(); $query = new Query(); $exists = $query->select(['id'])->from($this->sessionTable)->where(['id' => $id])->createCommand($this->db)->queryScalar(); if ($exists === false) { $this->db->createCommand()->insert($this->sessionTable, ['id' => $id, 'data' => $data, 'expire' => $expire, 'user_id' => $userId])->execute(); } else { $this->db->createCommand()->update($this->sessionTable, ['data' => $data, 'expire' => $expire, 'user_id' => $userId], ['id' => $id])->execute(); } } catch (\Exception $e) { $exception = ErrorHandler::convertExceptionToString($e); // its too late to use Yii logging here error_log($exception); echo $exception; return false; } return true; }
public function writeSession($id, $data) { try { $query = new Query(); $exists = $query->select(['session_id'])->from($this->sessionTable)->where(['session_id' => $id])->createCommand($this->db)->queryScalar(); if ($exists !== false) { $this->db->createCommand()->update($this->sessionTable, ['modified' => time(), 'session_data' => $data], ['session_id' => $id])->execute(); } } catch (\Exception $e) { $exception = \yii\web\ErrorHandler::convertExceptionToString($e); // its too late to use Yii logging here error_log($exception); echo $exception; return false; } return true; }
/** * Session write handler. * Do not call this method directly. * @param string $id session ID * @param string $data session data * @return boolean whether session write is successful */ public function writeSession($id, $data) { // exception must be caught in session write handler // http://us.php.net/manual/en/function.session-set-save-handler.php try { $query = new Query(); $exists = $query->select(['id'])->from($this->sessionTable)->where(['id' => $id])->createCommand($this->db)->queryScalar(); $fields = $this->composeFields($id, $data); if ($exists === false) { $this->db->createCommand()->insert($this->sessionTable, $fields)->execute(); } else { unset($fields['id']); $this->db->createCommand()->update($this->sessionTable, $fields, ['id' => $id])->execute(); } } catch (\Exception $e) { $exception = ErrorHandler::convertExceptionToString($e); // its too late to use Yii logging here error_log($exception); echo $exception; return false; } return true; }