API15

API15:JApplication/login

From Joomla! Documentation

Description

Login authentication function.

[Edit Descripton]

Template:Description:JApplication/login

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'));
}

[Edit See Also] Template:SeeAlso:JApplication/login

Examples

<CodeExamplesForm />