简体   繁体   中英

How can i use session from yii framework into my 3rd party application

I use Yii framework and in the framework i use 3rd party application which is mibew Messenger (or chat ).

What I need is to pass $_session variable (username and password) from yii framework to Mibew messenger , I need this because I want to be log in automatically when I log in into my yii application.

Mibew messenger folder is in the app folder of the application.

So how can I use the same session outside of yii framework ?

Thanks for the help.

I think you may do following:

1) In file of 3rd party application where you need to get an access to SESSION:

 require('/path/to/framework/YiiBase.php'); 

2) If you have specific configs for sessions, than you need you configs:

 $config = require('/path/to/protected/config/main.php'); $session = YiiBase::createComponent($config['components']['session']); 

3) For standard sessions (instead step #2) you should try:

 $session = new CHttpSession(); 

Than you can work with sessions as in framework: $session[$var_name] or $session->get/set($var_name) .

I don't check it solution. If there will be an error - write it on comments.

UPDATED

Just need to do:

 require('/path/to/framework/YiiBase.php'); $config = require('/path/to/configs_directory/main.php'); Yii::createWebApplication($config); 

Than you can use all framework features by Yii::app()

You can get the logged in user ID by Yii::app()->user->id; then query the username and password at your database using the logged in id as follows:

$userInfo = User::model()->findByPk(array('id'=>Yii::app()->user->id));
$user = $userInfo->username;
$password = md5($this->userInfo->password);

Now you can put these variables into session what I do not think is necessary. Because you should put these variables to the mibew directly. However if you would like to put these variable into the Yii session just do this following:

Yii::app()->session['usename']  = $user;
Yii::app()->session['password'] = $password;

This is details just make for your own.

For Yii2 it is done like this:

defined('YII_DEBUG') or define('YII_DEBUG', true);  //set these according to your needs
defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

$config = require(__DIR__ . '/../config/web.php');

new yii\web\Application($config);

Then you'll probably want to immediately open the session so that $_SESSION will always be available hereafter:

Yii::$app->session->open();

You can now use either $_SESSION like normal, or you can use eg. Yii::$app->session['sessionvarname'] = 'somevalue'; (or any method described in the guide chapter Session and Cookies ).

Also, if for example you wanted to use some of the assets from Yii you could load them like this:

$asset = Yii::$app->assetManager->getBundle('yii\web\YiiAsset', true);
echo yii\helpers\Html::jsFile(Yii::$app->assetManager->getAssetUrl($asset, 'yii.js'));
// -- or --
echo '<script type="text/javascript" src="'. Yii::$app->assetManager->getAssetUrl($asset, 'yii.js') .'"></script>';

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