How can I use a session from yii system in my third-party application

I use the Yii framework , and in the framework I use a third-party application that is mibew Messenger (or chat).

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

Folder

Mibew messenger is located in the application’s application folder.

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

Thanks for the help.

+3
source share
3 answers

I think you can do the following:

1) In the file of the third-party application, where you need to access the SESSION:

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

2) If you have certain configurations for sessions, then you need configs:

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

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

 $session = new CHttpSession(); 

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

I will not verify his decision. If an error occurs - write it in the comments.

UPDATED

Just need to do:

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

The more features you can use with Yii::app()

+1
source

You can get the user ID in the input file Yii::app()->user->id; and then request the username and password in your database using the registered id as follows:

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

Now you can include these variables in the session, which I do not think is necessary. Because you have to put these variables in mibew directly. However, if you want to put this variable in a Yii session, do the following:

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

These are the details that you can do yourself.

+1
source

For Yii2, this is done as follows:

 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 probably want to immediately open a session so that $ _SESSION is always available in the future:

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

Now you can use either $_SESSION as usual, or use, for example. Yii::$app->session['sessionvarname'] = 'somevalue'; (or any method described in the chapter of the Session and Cookies manual).

In addition, if you want to use some of the Yii assets, you can download them as follows:

 $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>'; 
0
source

Source: https://habr.com/ru/post/1498062/


All Articles