2015-07-05 66 views
1

我正在爲humhub構建自定義網絡研討會。我正在使用Custom_pages模塊(https://www.humhub.org/marketplace/details?id=13) 如何獲取我的應用程序中當前登錄用戶的用戶詳細信息?使用HumHub中的第三方應用程序獲取用戶詳細信息

有人給了我這個代碼嘗試

//plug in to Yii application 
require_once('../protected/vendors/yii/yii.php'); 
Yii::createWebApplication('../protected/config/main.php'); 

//------------------ 

//print_r(Yii::app()); 

echo "<br />"; 

//check if user is logged in (not a guest) 
if(!Yii::app()->user->isGuest) 
{ 
    //if logged in, display the username and ID 
    echo "<strong>Logged In.</strong>"; 
    echo "<br />"; 
    echo "Username: ".Yii::app()->user->name; 
    echo "<br />"; 
    echo "User ID: ".Yii::app()->user->id; 
} 
else 
{ 
    //if not logged in, display username (Guest) 
    echo "<strong>Not Logged In.</strong>"; 
    echo "<br />"; 
    echo "Username: ".Yii::app()->user->name; 
} 

但由於某些原因line 3使得頁面無法加載。如果我評論它,頁面加載但Yii是空的。

有沒有什麼辦法可以讓我的第三方應用程序中的登錄用戶的用戶詳細信息?

的任何方法,將不勝感激

回答

1

我用你的代碼,它的工作對我很好,但是我需要修改我的需要。也許我的修改會爲你工作。

<?PHP 
//this script gets the id and email "as name" from hum hub 
require_once('../humhub-master/protected/vendors/yii/yii.php'); 
Yii::createWebApplication('../humhub-master/protected/config/main.php'); 

//I create these vars to use in my custom pages 
    $hum_uid=Yii::app()->user->id; 
    $hum_name=Yii::app()->user->name; 
    // this should get me a real username 
$servername = "******"; 
$username = "******"; 
$password = "*****"; 
$dbname = "humhub"; 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 

$sql = "SELECT id, username FROM user WHERE id='$hum_uid'"; 
$result = $conn->query($sql); 

if ($result->num_rows > 0) { 
    // output data of each row 
    while($row = $result->fetch_assoc()) { 
     $hum_username = $row["username"]; 
     echo $hum_username; 
    } 
} else { 
    echo "0 results"; 
} 
?> 

我把它放在humhub的iframe自定義頁面的頂部。當你需要用戶名時,只需使用$ hum_username即可。

使用SQL選擇,你可以得到你想要的數據庫中的user表中的任何WHERE ID = '$ hum_uid'

0

更容易:

$userUsername = Yii::$app->user->identity->username; 

其他有用瓦爾:

$userDisplayName = Yii::$app->user->identity->getDisplayName(); 
$userAttributesArray = Yii::$app->user->identity->getSearchAttributes(); // all profile fields and groups  
$userIsGuest = Yii::$app->user->isGuest; // true = loggedOut, false = loggedIn 
$userIsAdmin = \humhub\modules\admin\widgets\AdminMenu::canAccess(); 
相關問題