1

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.

Community
  • 1
  • 1
Mayur Chauhan
  • 682
  • 1
  • 12
  • 25

2 Answers2

2

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();
Amit Ray
  • 3,445
  • 2
  • 19
  • 35
  • I've tested this code in my sight and it stops execution from JFactory::getApplication('site');. I did tested further it is calling libraries/joomla/application/application.php file. and it is not in our new version (3.4) joomla. – Mayur Chauhan Jun 23 '16 at 09:35
  • I am using Joomla version 3.4.4 and its working fine. let me check with 3.5 then. – Amit Ray Jun 23 '16 at 09:39
  • Can you tell me how you found out that it is calling libraries/joomla/application/application.php file, so that I can cross check? – Amit Ray Jun 23 '16 at 09:51
  • I've backup of 2.4 version and in that I found a class called application.php which was being called from JFactory::getApplication. While in 3.5 version there is no class called application.php. I know, I should not debug the in core files. But still I wanted to know what is causing the issue. – Mayur Chauhan Jun 23 '16 at 11:09
  • Your code is the same as mine in question. But It started working now. Issue was something with my post data. IDK why it happend. But, still thanks for your help. – Mayur Chauhan Jun 23 '16 at 11:14
  • 1
    In Joomla 3.x, you should **not** be using `JRequest` or `getVar()` as they're deprecated. Please look at [JInput](http://docs.joomla.org/Retrieving_request_data_using_JInput) – Lodder Jun 23 '16 at 12:10
  • @Lodder Joomla team is finding it difficult to get rid of JRequest as this has been deprecated since joomla 2.5 and i feel it will remain deprecated till J4. – Amit Ray Jun 23 '16 at 12:30
  • Yes, but nonetheless, using it it custom code/extensions shouldnt be done – Lodder Jun 23 '16 at 12:43
0
$code = NULL;
$plugin_param->params = new JRegistry($plugin_param->params);
$is_valid = $plugin->checkAnswer($code);
  • 1
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. [From Review](/review/low-quality-posts/28028202) – double-beep Jan 08 '21 at 12:08