How to get user id

For Joomla! 1.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
luisfgf
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Mon Apr 07, 2008 9:39 am

How to get user id

Post by luisfgf » Mon Apr 07, 2008 10:06 am

Hi,

Anyone can help me with de complet code for extract the user id or user name in the joomla 1.5 with the external scripts.

Thanks,
LF

User avatar
wojtekkk87
Joomla! Apprentice
Joomla! Apprentice
Posts: 34
Joined: Thu Mar 29, 2007 6:25 pm
Location: Poland
Contact:

Re: How to get user id

Post by wojtekkk87 » Mon Apr 07, 2008 10:40 am

What kind of external script?\I think it should be helpful:
http://dev.joomla.org/component/option, ... tableuser/

or

Code: Select all

$user =& JFactory::getUser();
$user->get('id');
it should works ok;)

Alex53
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 236
Joined: Mon Aug 22, 2005 11:25 am

Re: How to get user id

Post by Alex53 » Tue Apr 15, 2008 7:59 am

Hi, I am trying this code ;

Code: Select all

<?php

defined('_JEXEC') OR defined('_VALID_MOS') OR die( "Direct Access Is Not Allowed" );

echo "Hello";
echo "<br><br>User: ";

$jAp= & JFactory::getApplication();
$user =& JFactory::getUser();
$user->get('id');
echo $user;
?>
But all I get is 'Object' whoever is logged in. any ideas?

moggway
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Tue Apr 15, 2008 9:43 pm

Re: How to get user id

Post by moggway » Tue Apr 15, 2008 9:48 pm

Hi,
try this:

Code: Select all

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

Alex53
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 236
Joined: Mon Aug 22, 2005 11:25 am

Re: How to get user id

Post by Alex53 » Wed Apr 16, 2008 6:20 am

thanks, it works. I am also using a couple of functions member oc66 posted in another thread.

So now I have Joomla's ldap authentication working, I can get the php scripts included by the Jumi plugin to get the currently logged in user, but I cant get php to bind to the AD to check that this user belongs to a group. I am using a class called adldap, but its never worked for me since we upgraded to Windows Server 2003 from 2000.

ilektrojohn
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Mon Mar 05, 2007 9:06 am

Re: How to get user id

Post by ilektrojohn » Wed Apr 16, 2008 11:15 am

moggway wrote:Hi,
try this:

Code: Select all

$user =& JFactory::getUser();
$usr_id = $user->get('id');
echo $usr_id;
Bye,
moggway
Hello, I'm trying to implement this in a php script but it doesn;t seem to get any values. The whole user array values are 0 and null
Do i have to include any more classes ?
I have a testauth.php script which i call from a joomla article with a link.

Thanks in advance.

John

ilektrojohn
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Mon Mar 05, 2007 9:06 am

Re: How to get user id

Post by ilektrojohn » Thu Apr 17, 2008 1:15 pm

It seems one way to do it is to load the framework with

Code: Select all

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

$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
But isn't that an overkill ? Can it be done without reloading the whole framework in every php script i'm using ?

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

Post by dam-man » Thu Apr 17, 2008 2:22 pm

sorry ilektrojohn, what you're telling here is not the way to do this..

Take a look at for example to the login module, if you're lofgged in it will show your name.
This is the method, this session is started, and allmost all data is from jos_user with the specific id thats logged in. Is availeble through the session
Robert Dam - Joomla Forum Moderator
Dutch Boards | Joomla Coding Boards | English Support Boards

ilektrojohn
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Mon Mar 05, 2007 9:06 am

Re: How to get user id

Post by ilektrojohn » Thu Apr 17, 2008 4:46 pm

I was pretty sure that way was not the way to go, so there's no need to be sorry :D

Still I cannot see how the information about the user is taken from session in mod login. I can't see no call to JSession class in mod_login.php or helper.php .
JFactory::getuser is used there also
All I'm trying to do is implement a way for a non joomla application running in the same server to get user credentials (only their username) from joomla , so I can use it to authenticate the user.
I think I know how to get the information from the session, but every time i try it i get null values whether the user is logged in in joomla or not.
Could you or somebody else provide me with a minimal working example?
Meaning something like

Code: Select all

require someclass;
require someotherclass;
//ok now you got the session values available do as you please with them 

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

Post by dam-man » Fri Apr 18, 2008 4:14 pm

I've tested this:

Code: Select all

<?php 
$user =& JFactory::getUser();
echo $user->get('id') ; 
?>
And this is working properly in Joomla! 1.5.2

Check in for example ../modules/mod_login/mod_login.php
Here you will find:

Code: Select all

$user =& JFactory::getUser(); 


And in the template they're using this:

Code: Select all

<?php echo JText::sprintf( 'HINAME', $user->get('name') ); ?>
This will show you your name. If you change this into "id" it will work
Robert Dam - Joomla Forum Moderator
Dutch Boards | Joomla Coding Boards | English Support Boards

ilektrojohn
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Mon Mar 05, 2007 9:06 am

Re: How to get user id

Post by ilektrojohn » Sat Apr 19, 2008 6:16 pm

Yes i understand what you re saying but i think you dont understand what I'm trying to do.
If I use the getUser function in module or component it works, but what i'm trying to do is access the method from a php script that is in the same server , but not part of joomla .
Lets say i have a simple php script that says

Code: Select all

<?php 
echo 'hello';
$user =& JFactory::getUser();
echo 'You ar user' .$user.'! Welcome!';
?>
It;s obvious that it doesn't work.
If I add

Code: Select all

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
in the begining of the script i dont;get any warnings, as it knows where to find the specific classes and functions,
but getUser always returns null.

Now, if i also add
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
I get the user name.

What I'm asking is what are the minimal classes i will have to load in order for getUSer to work .

Thanks in advance.

User avatar
jaguas
Joomla! Apprentice
Joomla! Apprentice
Posts: 13
Joined: Thu Sep 06, 2007 10:33 am

Re: How to get user id

Post by jaguas » Tue Apr 22, 2008 4:49 pm

Hello.

I have a form in a .php file in the same server of joomla but in another database.

I need to insert the username in the table.

With this code:
$user_joomla =& JFactory::getUser();
$usr_username = $user_joomla->get('username');

I get this error:
Class 'JFactory' not found

How can I solve this?

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

Post by dam-man » Wed Apr 23, 2008 7:58 am

did you connected the database by this script?
Robert Dam - Joomla Forum Moderator
Dutch Boards | Joomla Coding Boards | English Support Boards

User avatar
jaguas
Joomla! Apprentice
Joomla! Apprentice
Posts: 13
Joined: Thu Sep 06, 2007 10:33 am

Re: How to get user id

Post by jaguas » Wed Apr 23, 2008 9:30 am

Yes.

In this script i connect to the other database (not the joomla database) to read the content and to insert new content using the form.

tristanputman
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Sun Jun 08, 2008 3:42 pm

Re: How to get user id

Post by tristanputman » Sun Jun 08, 2008 7:29 pm

I had the same problem as others. I'm using an external PHP file to attempt to access information about the currently logged on Joomla user (while not under the Joomla path).
This is the PHP code I had to add to get access to the user array.

Code: Select all

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

User avatar
jaguas
Joomla! Apprentice
Joomla! Apprentice
Posts: 13
Joined: Thu Sep 06, 2007 10:33 am

Re: How to get user id

Post by jaguas » Tue Jun 24, 2008 9:28 am

Hi.

I've been in holidays.

I tried the code but now i get this error message:

Warning: require_once(C:\Apache\htdocs\joomla\test\includes\defines.php) [function.require-once]: failed to open stream: No such file or directory in C:\Apache\htdocs\joomla\test\problems.php on line 24

Fatal error: require_once() [function.require]: Failed opening required 'C:\Apache\htdocs\joomla\test\includes\defines.php' (include_path='.;C:\php5\pear') in C:\Apache\htdocs\joomla\test\problems.php on line 24

In fact, the directory "includes" does not exist in that path.

What am i doing wrong?

tristanputman
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Sun Jun 08, 2008 3:42 pm

Re: How to get user id

Post by tristanputman » Tue Jun 24, 2008 12:21 pm

FYI - that code is for Joomla 1.5.

I recommend you perform a search in the Joomla install directory for your "defines.php" and adjust the path accordingly.

Instead of this:
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );

You may just neeed to hardcode the path as follows:
require_once ( 'C:\Apache\htdocs\joomla\includes\defines.php' );

Is Joomla installed in the "test" directory ?

User avatar
jaguas
Joomla! Apprentice
Joomla! Apprentice
Posts: 13
Joined: Thu Sep 06, 2007 10:33 am

Re: How to get user id

Post by jaguas » Tue Jun 24, 2008 1:19 pm

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

mhamberg
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Wed Jun 28, 2006 10:43 pm

Re: How to get user id

Post by mhamberg » Tue Aug 12, 2008 11:25 pm

I had this same problem and ended up having to put the files in the root directory and call the mainframe getapplication. It's not pretty but it works.

strega
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Tue Aug 26, 2008 10:00 am

Re: How to get user id

Post by strega » Tue Aug 26, 2008 10:08 am

Hi, I've a question.
I'm using the Joomla version 1.8 .
I want to get the user ID of logged on members into an input like this:

Code: Select all

<input type="text" readonly="yes" value="userID">
On the place of userID should just come your ID number.
Could anyone help plz?

Thx

siranm
Joomla! Apprentice
Joomla! Apprentice
Posts: 30
Joined: Sun Aug 03, 2008 5:09 pm

Re: How to get user id

Post by siranm » Sat Aug 30, 2008 8:28 pm

ilektrojohn wrote:Yes i understand what you re saying but i think you dont understand what I'm trying to do.
If I use the getUser function in module or component it works, but what i'm trying to do is access the method from a php script that is in the same server , but not part of joomla .
Lets say i have a simple php script that says

Code: Select all

<?php 
echo 'hello';
$user =& JFactory::getUser();
echo 'You ar user' .$user.'! Welcome!';
?>
It;s obvious that it doesn't work.
If I add

Code: Select all

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
in the begining of the script i dont;get any warnings, as it knows where to find the specific classes and functions,
but getUser always returns null.

Now, if i also add
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
I get the user name.

What I'm asking is what are the minimal classes i will have to load in order for getUSer to work .

Thanks in advance.

Hi!

Im having the same problem?

Did you find a workaround ?

I have the additional problem of wanting to redirect to another directory in the same server, but after doing
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
i can't send no headers.... (in particular header("Location: http://sameserver/otherdirectory")

???

desperate here !

thanks in advance,

siran

User avatar
b.sanjay
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 235
Joined: Wed May 09, 2007 7:53 am
Location: Bangalore, Karnataka, India
Contact:

Re: How to get user id

Post by b.sanjay » Mon Sep 01, 2008 5:57 am

Hi there :pop ,
've u ppl tryed

Code: Select all

include_once/include "The_File_Containing_the_Class_and_Function.php";
:D
As you ppl are trying to access the database outsite the Joomla..... ??? all the things you 've to do is manually.... :eek:
Thank you.

Regards,
Sanjay Beedi
Senior Software Engineer,
Construe Solutions, Hyderabad, AP, India
http://www.construesol.com/

Reelix
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Fri Oct 10, 2008 7:40 am

Re: How to get user id

Post by Reelix » Fri Oct 10, 2008 7:45 am

So close...

It works perfectly when added onto the index page, but when I try it on my own page...


http://mysite.com/test.php

Code: Select all


<?php
require_once ('C:\Inetpub\Domains\mySite\includes\defines.php');
require_once ('C:\Inetpub\Domains\mySite\includes\framework.php');

$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();

echo 'hello';
$user =& JFactory::getUser();
echo 'You are user' .$user.'! Welcome!';
?>


Although if I go to the page, i just get a:

"Restricted access"

Where do I define that it should be unrestricted access, but only to this page, or add this page to the unrestricted list, or something... ? ;)

jpatti
Joomla! Apprentice
Joomla! Apprentice
Posts: 38
Joined: Sun Aug 03, 2008 6:05 am

Re: How to get user id

Post by jpatti » Sun Nov 23, 2008 12:00 am

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.

sonny_s
Joomla! Apprentice
Joomla! Apprentice
Posts: 26
Joined: Fri Jan 11, 2008 7:38 am

Re: How to get user id

Post by sonny_s » Mon Nov 24, 2008 2:12 am

luisfgf wrote:Hi,

Anyone can help me with de complet code for extract the user id or user name in the joomla 1.5 with the external scripts.

Thanks,
LF
Hi luis,

the only way to do this things, you can using Wrapper link method at Joomla 1.5.7. I have tested this from my several joomla website.

But, first things to do is you must read this :

http://forum.joomla.org/viewtopic.php?f=304&t=343435

NB : I have tested several times using JFactory class like anyone mention above, but it's only giving "blank page" result. So I using Wrapper link method.. and it works and run properly..

Cheers,
Sonny_S

User avatar
TheMick07
Joomla! Apprentice
Joomla! Apprentice
Posts: 19
Joined: Thu Oct 23, 2008 7:00 pm

Re: How to get user id

Post by TheMick07 » Mon Nov 24, 2008 7:34 pm

jaguas wrote:Hello.

I have a form in a .php file in the same server of joomla but in another database.

I need to insert the username in the table.

With this code:
$user_joomla =& JFactory::getUser();
$usr_username = $user_joomla->get('username');

I get this error:
Class 'JFactory' not found

How can I solve this?
I have the same problem when loading up 1.0.15 after already having 1.0.5

sarajohn
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Wed Nov 19, 2008 7:42 am

Re: How to get user id

Post by sarajohn » Tue Nov 25, 2008 11:22 am

it should be helpful:
http://dev.joomla.org/component/option, ... tableuser/
http://beachrealestateagents.com/ Myrtle Beach Real Estate
http://www.aptas.net/ Telephone Answering Service
Signature rules - Literal URL's Only.

SoulBuccaneer
Joomla! Apprentice
Joomla! Apprentice
Posts: 15
Joined: Thu Feb 12, 2009 1:11 pm

Re: How to get user id

Post by SoulBuccaneer » Thu Feb 19, 2009 11:30 am

Hi all, a question very similar...

I've added a filed in the jos_users table...
there's a way to get the field value I've added?

somothing like: $user->my_field;

tnx a lot!

manuellg
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Fri Mar 20, 2009 3:39 pm

Re: How to get user id

Post by manuellg » Fri Mar 20, 2009 3:41 pm

I use this code to get id from user

Code: Select all

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

but in my new server i can get the id but i recive this wornnigs:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/winbetpt/public_html/casas/teste.php:10) in /home/winbetpt/public_html/libraries/joomla/session/session.php on line 423

Warning: Cannot modify header information - headers already sent by (output started at /home/winbetpt/public_html/casas/teste.php:10) in /home/winbetpt/public_html/libraries/joomla/session/session.php on line 426

i can don't recive this wornnings

harwil
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Mon Mar 23, 2009 10:12 am

Re: How to get user id

Post by harwil » Mon Mar 23, 2009 10:20 am

Hello everybody,

Now you can easily use member id on a non joomla page. Add the below code on index.php

Code: Select all

session_start();
$user =& JFactory::getUser();
$umsrf = $user->get('id');
setcookie("userid_m",$umsrf, time()+840);
It'll create a cookie of the user who has logged in..... and you can easily access this cookie on a non joomla page. using the below code

Code: Select all

$u_id = $_COOKIE["userid_m"];


Locked

Return to “Joomla! 1.5 Coding”