I'm a newbie to Joomla and want to understand how Joomla! API works.
I have this piece of code in an external file that connects to another joomla site call APITEST
Basically, I have already created a user that has username: demo and password demo12. I want to use the login() method from JApplication class.
<?php
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', dirname(__FILE__).DS."../apitest/");
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' );
$user = JFactory::getApplication('site');
$credential = array('demo', 'demo12');
$remember = array(true);
print_r($user->login($credential, $remember));
?>
However it gives me this error message:
Notice: Undefined index: username in C:\xampp\htdocs\apitest\libraries\joomla\user\authentication.php on line 321
Notice: Undefined index: username in C:\xampp\htdocs\apitest\libraries\joomla\user\authentication.php on line 326
Notice: Undefined index: password in C:\xampp\htdocs\apitest\libraries\joomla\user\authentication.php on line 331
I expect the result to return True. How can I do so? Thanks.
Joomla is looking for the username and password in an associative array, not a numerical keyed array, which is what you have.
Change to:
$credential = array('username' => 'demo', 'password' => 'demo12');
Related
I'm well familiar with coding in PHP and other languages, but I'm totally unfamiliar with Joomla.
What I want to accomplish is to know when a user has logged in on the Joomla site (under /web) and differentiate this script's behaviour accordingly.
I have a Joomla 2.5 site under /web/ and I have a script under /web/test/index.php, where the following code lies (collected from other replies in here and elsewhere):
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
echo dirname(dirname(__FILE__)) . "\n";
if (file_exists(dirname(dirname(__FILE__)) . '/defines.php'))
include_once dirname(dirname(__FILE__)) . '/defines.php';
if (!defined('_JDEFINES')) {
define('JPATH_BASE', dirname(dirname(__FILE__)));
require_once JPATH_BASE.'/includes/defines.php';
}
require_once JPATH_BASE.'/includes/framework.php';
$app = JFactory::getApplication('site');
$app->initialise();
print_r(JFactory::getUser());
What I get back is
/web
JUser Object
(
[isRoot:protected] =>
[id] => 0
[name] =>
...
Thank you,
Use this code it's working:
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/' ));
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' );
$mainframe = JFactory::getApplication('site');
var_dump(JFactory::getUser());
I just want to set a user to a specific group. I have tried many things but not helped. One solutiuon is worked for me, but I think it is deprecated because in the Joomla's admin page I see other groups.
My code:
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', $_SERVER[ 'DOCUMENT_ROOT' ] );
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' );
$user =JFactory::getUser(joe); //joe is the username
$GroupJoomla = array('14'=>14); //I want to change it's group to 14
$user->set('groups',$GroupJoomla); //Method one, which I have found on documentations
print_r ( $user->get('groups')); //Succesfully sets, BUT if I check on the Joomla's admin page, the user is still on other group. Maybe this is a deprecated function and the Joomla didnt use it now.
$user->groups = $GroupJoomla;
$user->save(); //Method two. Unfortunatelly this is always returns to false for me, dont know why. There is no error message. (Apache error log also empty)
print_r ($user->getAuthorisedGroups()); //This is the good to get the user's groups. This is correct.
if ($user->save() === false)
{
throw new Exception($user->getError(), 500); // This gives me this error: Error displaying the error page: Application Instantiation Error: Application Instantiation Error
}
What am I doing wrong? :/
I havent got Joomfish, so it isnt why the $user->save() returns false.
Update:
This code is also returns false on $user->save().
$uid = 1748;
$user =JFactory::getUser($uid); //joe is the username
$GroupJoomla = array(14); //I want to change it's group to 14
$user->set('groups',$GroupJoomla);
$user->groups = $GroupJoomla;
$user->save();
jimport( 'joomla.access.access' );//Call the Access Class
/*If the below code is not there it wont save.*/
if ($user->save() === false){ //This is gives me false somehow
throw new Exception($user->getError(), 500);
}
$groups = JAccess::getGroupsByUser($uid, false);
print_r($groups); exit; //Receives Group id 15 (Not 14)
With both code samples I have got the good user object. $user->get('username'); This gives me the good username. (Joomla 3.6.5)
As #itoctopus said you need to enter id instead of username but also there are some other mistakes that needs to be taken care of like initiating the group variable. It should be
$GroupJoomla = array(14);
if more than one group
$GroupJoomla = array(13,14);
then to get the groups you can use
$groups = JAccess::getGroupsByUser($id, false);
So finally your code will be
<?php
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', 'C:\xampp\htdocs\joomla' );
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' );
$uid = 404;
$user =JFactory::getUser($uid); //joe is the username
$GroupJoomla = array(4,6); //I want to change it's group to 14
$user->set('groups',$GroupJoomla);
$user->groups = $GroupJoomla;
$user->save();
jimport( 'joomla.access.access' );//Call the Access Class
/*If the below code is not there it wont save.*/
if ($user->save() === false){
throw new Exception($user->getError(), 500);
}
$groups = JAccess::getGroupsByUser($uid, false);
print_r($groups); exit;
A quick glimpse at your code, and I think this line:
$user =JFactory::getUser(joe);
Should be:
$user =JFactory::getUser(5);
Where 5 is the id of the user with username "joe". You cannot get a user by username by using the function JFactory::getUser.
Alternatively, if you really want to get a user by username, then you should use:
$userId = JUserHelper::getUserId('joe'); //note that there are quotes around joe (in your code you omitted the quote)
$user =JFactory::getUser($userId);
Hope this helps.
i want use joomla methods in external php file and i use this code and it's work:
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../../..' ));
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' );
$mainframe =& JFactory::getApplication('site');
now i want use joomla JRoute method but it's not work correct in external file. how can use JRoute ?
thank you.
I am doing coding in Joomla and now i want to retrieve the User id that were Log in. When the user got logged in then the it works fine but when it clicks on my page then it shows an error. I think their may be caching or session problem. is it?
Here is my Joomla page code:
<?php
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', $_SERVER[ 'DOCUMENT_ROOT' ] );
defined('_JEXEC') or die;
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' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
// Initialise variables.
$app = JFactory::getApplication();
$user = JFactory::getUser();
$userId = $user->get('id');
$dispatcher = JDispatcher::getInstance();
$userId = $user->get('id');
echo $userId;
?>
Now in this code it retrieves 00 as a user id. But the user is logged in. So what is the problem?
use this code for detail users logged
$user = JFactory::getUser(); $userid = $user->id; echo $userid;
This line of code will get the current logged in user's ID.
$user_id = JFactory::getUser()->id;
I am having trouble in accessing the data of the login user in joomla site.
Can someone help me on it?
I've tried this code:
define( '_JEXEC', 1 );
define( 'DS', '/' );
define( 'JPATH_BASE', $_SERVER[ 'DOCUMENT_ROOT' ] );
require_once( JPATH_BASE . DS .'xampp' . DS .'UPOnlineLeave '. DS .'includes' . DS . 'defines.php' );
require_once( JPATH_BASE . DS .'xampp' . DS .'UPOnlineLeave '. DS . 'includes' . DS . 'framework.php' );
require_once( JPATH_BASE . DS .'xampp' . DS .'UPOnlineLeave '. DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
$user =& JFactory::getUser();
$session =& JFactory::getSession();
$user->username;
but it produces JOSerror:application instantation error.
Can someone help me?
Fisrt you need to load the entire Joomla Framework:
// Get Joomla! framework
define( '_JEXEC', 1 );
define( '_VALID_MOS', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__)));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
Only then you can try to get the user
$user =& JFactory::getUser();
$session =& JFactory::getSession();
You have to include the following lines to include the joomla default libraries.
You can also refer the code on the root path of the project index.php file
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
if (file_exists(dirname(__FILE__) . '/defines.php')) {
include_once dirname(__FILE__) . '/defines.php';
}
if (!defined('_JDEFINES')) {
define('JPATH_BASE', dirname(__FILE__));
require_once JPATH_BASE.'/includes/defines.php';
}
require_once JPATH_BASE.'/includes/framework.php';