1. 云栖社区>
  2. PHP教程>
  3. 正文

[Wiki] Check online users / set specific user offline

作者:用户 来源:互联网 时间:2017-12-01 15:26:13

[Wiki] Check online users / set specific user offline - 摘要: 本文讲的是[Wiki] Check online users / set specific user offline, How to check the online users or how to logout a specific user from your admin panel? This wiki shows how to do that 1)

How to check the online users or how to logout a specific user from your admin panel? This wiki shows how to do that

1) In protected/config/main.php in components section add this

'session' => array(//'class' => 'CDbHttpSession','class' => 'components.ExDbHttpSession','connectionID' => 'db','timeout' => 86400, ),

2) Extends the CDbHttpSession as components/ExDbHttpSession.php

class ExDbHttpSession extends CDbHttpSession { 	public function setUserId($userId) {		$db = $this->getDbConnection();		$db->setActive(true);		$db->createCommand()->update(				$this->sessionTableName, array('id_user' => $userId),				'id=:id', array(':id' => session_id())		);	} 	protected function createSessionTable($db, $tableName) {		parent::createSessionTable($db, $tableName);		$db->createCommand()->addColumn($tableName, 'id_user', 'integer');	} 	public function getDbConnection() {		return parent::getDbConnection();	} }

3) In the components/UserIdentity.php add the below code before the $this->errorCode = self::ERROR_NONE;

...$this->id = $record->id;Yii::app()->session->setUserId($this->id);$this->errorCode = self::ERROR_NONE;

Add also this code

//override the getId methodpublic function getId() {return $this->id;}

In you User model add this method, to check if a specific user is logged

public function isOnlineBySession() {	$table = Yii::app()->session->sessionTableName;	$db = Yii::app()->session->getDbConnection(); 	$row = $db->createCommand()->select()->from($table)->where('id_user=:id', array(':id' => $this->id))->queryRow(); 	if ($row) {	return true;	}	return false;}//logout a userpublic function logOutSession() { 	$table = Yii::app()->session->sessionTableName;	$db = Yii::app()->session->getDbConnection();	$row = $db->createCommand()->delete($table, 'id_akm_user=:id', array(':id' => $this->id)); 	if ($row)	return true; 	return false;}

Results:

  • In your view/admin (CGridView) you can use the isOnlineBySession to display whether a user is online!
  • In your controller you can use the logOutSession to logout any user

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索,以便于您获取更多的相关知识。