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;
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.
Is there a quick fire method of accessing all the data for a product from an external file by using its sku?
I have looked at How can I get prodcut data of a VirtueMart 2 product in an external file?
and tried to replace the Product ID with my desired id but to no avail:
if (!class_exists( 'VmConfig' )) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart'.DS.'helpers'.DS.'config.php');
VmConfig::loadConfig();
if (!class_exists( 'VmModel' )) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart'.DS.'helpers'.DS.'vmmodel.php');
$productModel = VmModel::getModel('Product');
$product = $productModel->getProduct(Product_ID);
In short terms, I'm looking for a way to access product data.
Product sku may not be unique. so I have found a solution that works for me. Hope this helps you. This is the full code that you need to call product by sku externally.
<?php
define( '_JEXEC', 1 );
define('JPATH_BASE', 'C:\Server\www\joomla' );//you will have diff location for your site
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
jimport('joomla.application.module.helper');
jimport('joomla.application.component.helper');
$mainframe = JFactory::getApplication('site');
$mainframe->initialise();
function getproductBySKU($sku){
$db = JFactory::getDbo();
$db->setQuery(true);
$db->setQuery("SELECT virtuemart_product_id FROM #__virtuemart_products WHERE product_sku= $sku");
$productids = $db->loadAssocList();
return $productids;
}
function getProduct($id)
{
if (!class_exists('VmConfig')) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart' . DS . 'helpers' . DS . 'config.php');
VmConfig::loadConfig();
if (!class_exists('VmModel')) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart' . DS . 'helpers' . DS . 'vmmodel.php');
$productModel = VmModel::getModel('Product');
$product = $productModel->getProduct($id);
return $product;
}
$products = getproductBySKU(1);//In this example SKU is 1 having 2 products
var_dump($products);//Gives the product id's in the SKU
foreach($products as $product){
var_dump(getProduct($product));
}
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';
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Accessing session data outside Joomla
I tried to use the below script to retrieve Joomla's Session, but it's not working.
Can anyone help me...
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(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');
$session = &JFactory::getSession();
This is what i use :
//Joomla Stuff
define( '_JEXEC', '1' );
define('JPATH_BASE', 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');
/**
* INITIALISE THE APPLICATION
*
* NOTE :
*/
// set the language
$mainframe->initialise();
JPluginHelper::importPlugin('system');
// trigger the onAfterInitialise events
JDEBUG ? $_PROFILER->mark('afterInitialise') : null;
$mainframe->triggerEvent('onAfterInitialise');
// End Joomla Stuff