The Joomla! Forum ™



Forum rules


Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 41 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: How to get user id
PostPosted: Mon Mar 23, 2009 5:11 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Mon Mar 23, 2009 4:41 pm
Posts: 10
hi friends which exact way to get user id.many people say many ways.but everything comes in the error.say me exact path.


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Mon Mar 23, 2009 5:14 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Mon Mar 23, 2009 4:58 pm
Posts: 12
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


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Mon Mar 23, 2009 9:44 pm 
User avatar
Joomla! Exemplar
Joomla! Exemplar

Joined: Fri Sep 09, 2005 2:13 pm
Posts: 7962
Location: The Netherlands
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


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Tue Mar 24, 2009 5:45 pm 
Joomla! Intern
Joomla! Intern

Joined: Wed Dec 12, 2007 5:42 pm
Posts: 60
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:
<?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>";


?>


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Mon Apr 06, 2009 8:02 pm 
Joomla! Fledgling
Joomla! Fledgling

Joined: Fri Mar 27, 2009 3:41 pm
Posts: 3
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


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Mon Jun 01, 2009 4:59 am 
User avatar
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Sun Jan 13, 2008 1:46 am
Posts: 114
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


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Sun Aug 16, 2009 12:17 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Sun Apr 26, 2009 11:42 pm
Posts: 7
Hi,

I am trying to retrieve the user id. So far I have used the code posted in this thread

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


But I get the following error:

Code:
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:
$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


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Mon Aug 17, 2009 4:03 pm 
Joomla! Intern
Joomla! Intern

Joined: Wed Dec 12, 2007 5:42 pm
Posts: 60
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:
<?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');

?>


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Wed Aug 19, 2009 11:55 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Sun Apr 26, 2009 11:42 pm
Posts: 7
Thanks for the help Jmair. I looked through the joomla code and eventually I found the code that works:

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


It's almost exactly the same as the normal code posted earlier in this thread:

Quote:
$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"


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Mon May 17, 2010 2:22 pm 
Joomla! Fledgling
Joomla! Fledgling

Joined: Mon May 17, 2010 2:07 pm
Posts: 1
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.


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Sun Jun 13, 2010 10:56 am 
Joomla! Fledgling
Joomla! Fledgling

Joined: Sun Jun 13, 2010 10:50 am
Posts: 1
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:
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:
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...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 41 posts ]  Go to page Previous  1, 2



Who is online

Users browsing this forum: No registered users and 16 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group