我正在构建一个自定义joomla组件,我正在研究如何在不使用帐户密码的情况下设置登录会话。我已经有了:
$app = &JFactory::getApplication('site');
$result = $app->login(array(
'username' => 'james',
'password' => 'password'
));
显然需要用户密码。我可以访问用户ID和用户名,因此可以使用这些句柄中的任何一个。是否有其他对象或方法可用于登录用户或是否有一些我可以使用的自定义解决方案,例如手动设置所有必需的$ _SESSION变量?
在此先感谢,任何帮助非常感谢:)
答案 0 :(得分:6)
重要提示:在//注册所需的会话变量
之后,需要添加会话对象// Register the needed session variables
$session =& JFactory::getSession();
$session->set('user', $jUser);
很好,thx
答案 1 :(得分:5)
//log user in
if(!JFactory::getUser()->id)
{
$email = (string)$response['linkedin']->{'email-address'};
$db = JFactory::getDbo();
$app = JFactory::getApplication();
$sql = "SELECT * FROM #__users WHERE email = " . $db->quote($email);
$db->setQuery($sql);
$result = $db->loadObject();
if($result->id)
{
$jUser = JFactory::getUser($result->id);
//$userarray = array();
//$userarray['username'] = $jUser->username;
//$userarray['password'] = $jUser->password;
//$app->login($userarray);
$instance = $jUser;
$instance->set('guest', 0);
// Register the needed session variables
$session =& JFactory::getSession();
$session->set('user',$jUser);
// Check to see the the session already exists.
$app->checkSession();
// Update the user related fields for the Joomla sessions table.
$db->setQuery(
'UPDATE '.$db->quoteName('#__session') .
' SET '.$db->quoteName('guest').' = '.$db->quote($instance->get('guest')).',' .
' '.$db->quoteName('username').' = '.$db->quote($instance->get('username')).',' .
' '.$db->quoteName('userid').' = '.(int) $instance->get('id') .
' WHERE '.$db->quoteName('session_id').' = '.$db->quote($session->getId())
);
$db->query();
// Hit the user last visit field
$instance->setLastVisit();
//return true;
$app->redirect('index.php?option=com_community&view=profile');
}
else
{
$url = "index.php?option=com_community&view=register";
$app->redirect($url,'We did not find your email address in our system. Please register.');
//echo "redirect to registration page";
//exit();
//$url = 'index.php?option=com_users&view=registration&name=' . $user_profile['name'] . '&username=' . $user_profile['username'] . '&email=' . $user_profile['email'];
//$url = JRoute::_($url);
//$app->redirect($url);
}
}