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.