JApplication/login: Difference between revisions
From Joomla! Documentation
No edit summary |
m preparing for archive only |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 2: | Line 2: | ||
Login authentication function. | Login authentication function. | ||
<! removed transcluded page call, red link never existed > | |||
===Syntax=== | ===Syntax=== | ||
Line 93: | Line 91: | ||
</source> | </source> | ||
<! removed transcluded page call, red link never existed > | |||
< | |||
===Examples=== | ===Examples=== | ||
< | |||
Log a user with username foo and password bar into the Joomla Front-end | |||
<source lang="php"> | |||
$credentials = array( 'username' => 'foo', 'password' => 'bar'); | |||
$login_site =& JFactory::getApplication('site'); | |||
$login_site->login($credentials, $options=array()); | |||
</source> | |||
=== Code Examples === | |||
<dpl> | <dpl> | ||
noresultsheader=\n | noresultsheader=\n | ||
category=login | category=login | ||
category=JApplication | category=JApplication | ||
namespace=CodeExample | |||
category=MethodExample | category=MethodExample | ||
include=* | include=* | ||
format= ,,, | format= ,,, | ||
</dpl> | </dpl> | ||
[[Category:Archived pages API15]] |
Latest revision as of 00:17, 25 March 2017
Description
Login authentication function.
<! removed transcluded page call, red link never existed >
Syntax
login($credentials, $options=array())
Parameter Name | Default Value | Description |
---|---|---|
$credentials | Array( 'username' => string, 'password' => string ) | |
$options | array() | Array( 'remember' => boolean ) |
Returns
boolean True on success. public
Defined in
libraries/joomla/application/application.php
Importing
jimport( 'joomla.application.application' );
Source Body
function login($credentials, $options = array())
{
// Get the global JAuthentication object
jimport( 'joomla.user.authentication');
$authenticate = & JAuthentication::getInstance();
$response = $authenticate->authenticate($credentials, $options);
if ($response->status === JAUTHENTICATE_STATUS_SUCCESS)
{
// Import the user plugin group
JPluginHelper::importPlugin('user');
// OK, the credentials are authenticated. Lets fire the onLogin event
$results = $this->triggerEvent('onLoginUser', array((array)$response, $options));
/*
* If any of the user plugins did not successfully complete the login routine
* then the whole method fails.
*
* Any errors raised should be done in the plugin as this provides the ability
* to provide much more information about why the routine may have failed.
*/
if (!in_array(false, $results, true))
{
// Set the remember me cookie if enabled
if (isset($options['remember']) && $options['remember'])
{
jimport('joomla.utilities.simplecrypt');
jimport('joomla.utilities.utility');
//Create the encryption key, apply extra hardening using the user agent string
$key = JUtility::getHash(@$_SERVER['HTTP_USER_AGENT']);
$crypt = new JSimpleCrypt($key);
$rcookie = $crypt->encrypt(serialize($credentials));
$lifetime = time() + 365*24*60*60;
setcookie( JUtility::getHash('JLOGIN_REMEMBER'), $rcookie, $lifetime, '/' );
}
return true;
}
}
// Trigger onLoginFailure Event
$this->triggerEvent('onLoginFailure', array((array)$response));
// If silent is set, just return false
if (isset($options['silent']) && $options['silent']) {
return false;
}
// Return the error
return JError::raiseWarning('SOME_ERROR_CODE', JText::_('E_LOGIN_AUTHENTICATE'));
}
<! removed transcluded page call, red link never existed >
Examples
Log a user with username foo and password bar into the Joomla Front-end
$credentials = array( 'username' => 'foo', 'password' => 'bar');
$login_site =& JFactory::getApplication('site');
$login_site->login($credentials, $options=array());
Code Examples