How to log into joomla through an external script?

user77413 picture user77413 · Jan 16, 2010 · Viewed 17.8k times · Source

We have a standalone script on our site that sits adjacent to a Joomla 1.5 installation. We are using Joomla authentication to restrict access to our script. At this point we are redirecting any unauthorized users to the Joomla site to log in. We want to add a login capability within our script, though. Does anyone know how to log into joomla from an external script using a username/password? Thanks!

Answer

user276006 picture user276006 · Feb 18, 2010
<?php
//http://domain.com/script/script.php?username=username&passwd=password

define( '_JEXEC', 1 );
define('JPATH_BASE', '../' );
define( 'DS', DIRECTORY_SEPARATOR );
require_once('../configuration.php');
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );

/* Create the Application */
$mainframe =& JFactory::getApplication('site');
jimport('joomla.plugin.helper');

$credentials = array();
$credentials['username'] = JRequest::getVar('username', '', 'method', 'username');
$credentials['password'] = JRequest::getVar('passwd', '', 'method', 'passwd');

//perform the login action
$error = $mainframe->login($credentials);
$user = JFactory::getUser();
//now you are logged in

$mainframe->logout();
//now you are logged out