API15

JApplication/login

From Joomla! Documentation

< API15:JApplication

The "API15" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

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