2015-07-05 65 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(); 
相关问题