How to get user id

For Joomla! 1.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
premanand19193
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Mon Mar 23, 2009 4:41 pm

Re: How to get user id

Postby premanand19193 » Mon Mar 23, 2009 5:11 pm

hi friends which exact way to get user id.many people say many ways.but everything comes in the error.say me exact path.

digip789
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Mon Mar 23, 2009 4:58 pm

Re: How to get user id

Postby digip789 » Mon Mar 23, 2009 5:14 pm

Try Imacros extract script. It is really fast to extract the data. May be this helpful to you.
Signature Rules: viewtopic.php?f=8&t=65

User avatar
dam-man
Joomla! Exemplar
Joomla! Exemplar
Posts: 7961
Joined: Fri Sep 09, 2005 2:13 pm
Location: The Netherlands
Contact:

Re: How to get user id

Postby dam-man » Mon Mar 23, 2009 9:44 pm

This one is what you need, the original Joomla! way: viewtopic.php?p=1273713#p1273713
Robert Dam - Joomla Forum Moderator
Dutch Boards | Joomla Coding Boards | English Support Boards

Jmair
Joomla! Intern
Joomla! Intern
Posts: 60
Joined: Wed Dec 12, 2007 5:42 pm

Re: How to get user id

Postby Jmair » Tue Mar 24, 2009 5:45 pm

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>";


?>

geomagnet
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Fri Mar 27, 2009 3:41 pm

Re: How to get user id

Postby geomagnet » Mon Apr 06, 2009 8:02 pm

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

User avatar
oasisfleeting
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 114
Joined: Sun Jan 13, 2008 1:46 am
Contact:

Re: How to get user id

Postby oasisfleeting » Mon Jun 01, 2009 4:59 am

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.
Check out my joomla extensions at www.acoolsip.com

tim555
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Sun Apr 26, 2009 11:42 pm
Contact:

Re: How to get user id

Postby tim555 » Sun Aug 16, 2009 12:17 pm

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

Jmair
Joomla! Intern
Joomla! Intern
Posts: 60
Joined: Wed Dec 12, 2007 5:42 pm

Re: How to get user id

Postby Jmair » Mon Aug 17, 2009 4:03 pm

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');

?>

tim555
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Sun Apr 26, 2009 11:42 pm
Contact:

Re: How to get user id

Postby tim555 » Wed Aug 19, 2009 11:55 pm

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"

ottofire
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Mon May 17, 2010 2:07 pm

Re: How to get user id

Postby ottofire » Mon May 17, 2010 2:22 pm

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.

ronnietam
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Sun Jun 13, 2010 10:50 am

Re: How to get user id

Postby ronnietam » Sun Jun 13, 2010 10:56 am

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


Return to “Joomla! 1.5 Coding”

Who is online

Users browsing this forum: No registered users and 11 guests