Get current user from Yii outside of website

I made a simple site using Yii, and I used the Yii-user extension. I have a separate php file on the same server (let's call it loader.php).
I want the current user Yii to log in to loader.php. I realized that no session is installed in the Yii user extension, then how can I do this?

+4
source share
2 answers

I know that this is 2 months, but maybe someone can find this useful, I have the same problem, and thanks to creatoR I was able to get a solution, you can check it here How can I use a session from yii structure in my third-party annex

You must include yii.php and the configuration file as follows:

require('../../framework/yii.php'); $config = require('../../protected/config/main.php'); 

What you need to do the following:

 Yii::createWebApplication($config); 

and if you use var_dump like this, you will get the necessary information, in this example id

 var_dump(Yii::app()->User->id); 
+3
source

In Yii, you can get the user id using this:

 $userId = Yii::app()->user->Id; 

It will give the user ID if the user is logged in and the CWebUser object was saved in the session.

During the initialization process, CWebUser uses CWebUser-> getState ('__ id') to get the user ID and, by default, tries to get data from the Yii session. If you use the default Yii session component, CWebUser will look for $ _SESSION [$ key] for both ID and key $:

 CWebUser.php:567: $key=$this->getStateKeyPrefix().$key; CWebUser.php:540: return $this->_keyPrefix=md5('Yii.'.get_class($this).'.'.Yii::app()->getId()); 

So, your $ key to get user_id from the session: md5 ('Yii.'. Get_class ($ this). '.'. Yii :: app () → getId ()).

And what is Yii :: app () → getId ()?

 CApplication.php:232: return $this->_id=sprintf('%x',crc32($this->getBasePath().$this->name)); 

So, in your "loader.php" you can use this to create a key for user_id:

 $basePath = "/var/www/yii-app.com/protected";//Place here your app basePath by hands. $app_name = "My super app";//Place here your real app name $app_id = sprintf('%x',crc32($basePath.$this->name)); $class = "CWebUser";//Place here your real classname, if you using some other class (for example, I'm using my own implementation of the CWebUser class) $key = md5('Yii.'.$class.'.'.$app_id) . "__id"; session_start(); $user_id = $_SESSION[$key]; echo "USER ID is:" . $user_id; //Now you can user $user_id in any way, for example, get user name from DB: mysql_connect(...); $q = mysql_query("SELECT name FROM users WHERE id='" . (int)$user_id ."';"; $data = mysql_fetch_array($q, MYSQL_ASSOC); echo "Hello, dear " . $data['name'] . ", please dont use this deprecated mysql functions!"; 

I repeat: if you use the default CSession component in yii, it is easy to get user_id, but if you use some other class, for example, one that uses redis or mongoDB to store sessions instead of the default PHP mechanism, you will have to do one more work to get data from these storages.

+1
source

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


All Articles