Page 2 of 2

Re: How to get user id

Posted: Mon Mar 23, 2009 5:11 pm
by premanand19193
hi friends which exact way to get user id.many people say many ways.but everything comes in the error.say me exact path.

Re: How to get user id

Posted: Mon Mar 23, 2009 5:14 pm
by digip789
Try Imacros extract script. It is really fast to extract the data. May be this helpful to you.

Re: How to get user id

Posted: Mon Mar 23, 2009 9:44 pm
by dam-man
This one is what you need, the original Joomla! way: http://forum.joomla.org/viewtopic.php?p ... 3#p1273713

Re: How to get user id

Posted: Tue Mar 24, 2009 5:45 pm
by Jmair
Here is the script I have working as an example and some comments. The file is within the joomla directory.

You should be able to copy and paste this into yourtest.php file, then log into your joomla site and see how it works.

Code: Select all

<?php

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');
$mainframe->initialise();

$mainframe->route();
$user =& JFactory::getUser();

//print_r($user); //Uncomment to see what fields you can grab. Below are examples.


echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";

$user =& JFactory::getUser();
echo "Full User Name : " .$user->get('name') ."<br>"; 
echo "Username : " .$user->get('username') ."<br>"; 
echo "Last Visit Date : " .$user->get('lastvisitDate') ."<br>"; 
echo "Email : " .$user->get('email') ."<br>"; 


?>

Re: How to get user id

Posted: Mon Apr 06, 2009 8:02 pm
by geomagnet
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');
$mainframe->initialise();

$mainframe->route();
$user =& JFactory::getUser();
$user_id = $user->get('id');

works good for scripts not associated with Joomla (it basically makes them associated by inclusion). BUT...

What about AJAX or Remote Procedural Calls? The only thing I've found you can do is include a GET or POST query, or a COOKIE with the user-id. However, everyone gives me a hard time for exposing the user-id in the URL and using cookies. I would like to use sessions but can figure out how to access the current session via AJAX. Perhaps using and IP based storage would work since the AJAX requests are coming from the same IP (as expected).

Any other ideas would be greatly appreciated.

Thanks
JS

Re: How to get user id

Posted: Mon Jun 01, 2009 4:59 am
by oasisfleeting
if you're not logged in to the site... AKA $user->guest == true then your user->get('id') = 0

user id is 0 for guest/public users.

Re: How to get user id

Posted: Sun Aug 16, 2009 12:17 pm
by tim555
Hi,

I am trying to retrieve the user id. So far I have used the code posted in this thread
<?php
$user =& JFactory::getUser();
echo $user->get('id') ;
?>
But I get the following error:

Code: Select all

Catchable fatal error: Object of class JUser could not be converted to string
I'm trying to get the user id so that I can create a folder on the server named as the user's id to store an image that the user has uploaded. This is the code relating to my problem if that helps at all:

Code: Select all

$imagename = $_FILES['images']['name']; //find the name of the uploaded file

$user =& JFactory::getUser();
$user->get('id');

//creates the dir to store the product images in a folder specific to who uploaded the product
	if (!(file_exists('images/fitness-products/'.$user))) {
		mkdir('images/fitness-products/'.$user);
	}
	
	//upload the image to the directory, if this works then update the database
	if ($imagename = move_uploaded_file($imagename,'images/fitness-products/'.$user)) {
		savetodb();
		echo '<h1 align="center">product saved</h1>';
	}

I have tried using variations of all the code posted throughout this thread but so far I cant work out the problem, all help would be really appreciated.

Thanks,
Tim

Re: How to get user id

Posted: Mon Aug 17, 2009 4:03 pm
by Jmair
I was only able to return an ID using the following line just now. But I think with this, the line has to be in the root of the joomla (i.e. in the www folder along with joomla.) I didnt try it anywhere else. Oh, and it will return blank if you are not logged into the joomla site.

Code: Select all

<?php
//GET JOOMLA USERNAME
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');
$mainframe->initialise();

$mainframe->route();

$user =& JFactory::getUser();
echo $user->get('id');

?>

Re: How to get user id

Posted: Wed Aug 19, 2009 11:55 pm
by tim555
Thanks for the help Jmair. I looked through the joomla code and eventually I found the code that works:

Code: Select all

$user =& JFactory::getUser(); 
	$usr_id = $user->get('id');
It's almost exactly the same as the normal code posted earlier in this thread:
$user =& JFactory::getUser();
$user = $user->get('id') ;
All I had to do was assign a different variable name to "$user->get('id');" instead of re-using "$user"

Re: How to get user id

Posted: Mon May 17, 2010 2:22 pm
by ottofire
It's not going to work outside of Joomla! the way you're trying cause when the user goes to your non-Joomla page, he is no longer a logged-in member of the site and there's no session variables etc.

If you're really determined your script can't run inside of Joomla, you have to "get" that Joomla variables are not going to be accessible unless you specifically hand them over. So you need to write a URL for the user on a *Joomla* page, like this:

<? $user =& JFactory::getUser(); ?>
<a href="http://www.mysite.com/nonjoomla.php?userid=<? echo $user->get('id'); ?>">

That writes a querystring that will hand a Joomla variable over to a non-Joomla script.

If you have too many variables to hand over like this, write a form with hidden inputs that you populate and have it submit to the non-Joomla script when the user hits the button.

And then when you get to the non-Joomla page you can extract the userid from the querystring or form in normal php fashion and do whatever you like with it.
--------------------------------------------------------------------------------------------

i tryed to folow this way... i use a wrapper, so , when i create the wrapped link in joomla interface

i tryed to replace
<? $user =& JFactory::getUser(); ?>
<a href="http://www.mysite.com/nonjoomla.php?userid=<? echo $user->get('id'); ?>">
with this link
http://www.mysite.com/nonjoomla.php?userid=<? $user =& JFactory::getUser(); echo $user->get('id'); ?>"

but it' s not working. parameters is not set in the new page.
i try te obtain the name and not the id
so i replaced exactly with

http://www.mysite.com/nonjoomla.php?userid=<? $user =& JFactory::getUser(); echo $user->get('name'); ?>"

thank's for your help.

Re: How to get user id

Posted: Sun Jun 13, 2010 10:56 am
by ronnietam
I just use realpath with ../ to tell Joomla my php is sitting in my customized folder (D:\xampp\htdocs\Joomla\myphp)

define( 'DS', DIRECTORY_SEPARATOR );
define('JPATH_BASE', realpath('./../'));
echo JPATH_BASE;

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );


jaguas wrote:I changed the two lines like this:

Code: Select all

require_once ('C:\Apache\htdocs\joomla\includes\defines.php');
require_once ('C:\Apache\htdocs\joomla\includes\framework.php');
Now i have this message:
No configuration file found and no installation code available. Exiting...

The Joomla 1.5 is installed here: 'C:\Apache\htdocs\joomla\'

If i change my 'problem.php' file to 'C:\Apache\htdocs\joomla\' it works fine with the first code you gave me:

Code: Select all

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
and i can see the content of the array.

But i would like to organise my php files in other directorys...