Search code examples
web-servicesauthenticationjoomla

In Joomla 3.4 JFactory::getApplication('site'); stopped working


I've used below code from this reference : https://stackoverflow.com/a/2288181/4819200

<?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

Now, the Issue

It was working in Joomla 2.3 version and it stopped working in 3.4 version. Now, it is not returning from line $mainframe =& JFactory::getApplication('site');.

You can suggest me code for latest version.

Note : I'm not joomla guy. But, I've worked on some PHP frameworks.


Solution

  • In Joomla 3 you can use this code to get User Object from outside Joomla

    <?php
    //http://domain.com/script/script.php?username=username&passwd=password
    
    $dir = '/var/www/joomla'; // path to your joomla installation directory
    
    define( '_JEXEC', 1 );
    define( 'JPATH_BASE', $dir);
    define( 'DS', '/' );
    
    require_once ( JPATH_BASE .DS . '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' );
    //Then you can call the classes this way
    $mainframe = JFactory::getApplication('site');
    
    /* 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();