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 1, 2  Next
Author Message
 Post subject: How to get user id
PostPosted: Mon Apr 07, 2008 10:06 am 
Joomla! Fledgling
Joomla! Fledgling

Joined: Mon Apr 07, 2008 9:39 am
Posts: 1
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


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Mon Apr 07, 2008 10:40 am 
User avatar
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Mar 29, 2007 6:25 pm
Posts: 33
Location: Poland
What kind of external script?\I think it should be helpful:
http://dev.joomla.org/component/option, ... tableuser/

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

it should works ok;)


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Tue Apr 15, 2008 7:59 am 
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Mon Aug 22, 2005 11:25 am
Posts: 213
Hi, I am trying this code ;

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


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Tue Apr 15, 2008 9:48 pm 
Joomla! Fledgling
Joomla! Fledgling

Joined: Tue Apr 15, 2008 9:43 pm
Posts: 1
Hi,
try this:

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

Bye,
moggway


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Wed Apr 16, 2008 6:20 am 
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Mon Aug 22, 2005 11:25 am
Posts: 213
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.


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Wed Apr 16, 2008 11:15 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Mon Mar 05, 2007 9:06 am
Posts: 6
moggway wrote:
Hi,
try this:

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


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Thu Apr 17, 2008 1:15 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Mon Mar 05, 2007 9:06 am
Posts: 6
It seems one way to do it is to load the framework with

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


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Thu Apr 17, 2008 2:22 pm 
User avatar
Joomla! Exemplar
Joomla! Exemplar

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


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Thu Apr 17, 2008 4:46 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Mon Mar 05, 2007 9:06 am
Posts: 6
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:
require someclass;
require someotherclass;
//ok now you got the session values available do as you please with them


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Fri Apr 18, 2008 4:14 pm 
User avatar
Joomla! Exemplar
Joomla! Exemplar

Joined: Fri Sep 09, 2005 2:13 pm
Posts: 7962
Location: The Netherlands
I've tested this:

Code:
<?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:
$user =& JFactory::getUser();


And in the template they're using this:
Code:
<?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


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Sat Apr 19, 2008 6:16 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Mon Mar 05, 2007 9:06 am
Posts: 6
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:
<?php
echo 'hello';
$user =& JFactory::getUser();
echo 'You ar user' .$user.'! Welcome!';
?>

It;s obvious that it doesn't work.
If I add
Code:
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
Quote:
$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.


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Tue Apr 22, 2008 4:49 pm 
User avatar
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Sep 06, 2007 10:33 am
Posts: 13
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?


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Wed Apr 23, 2008 7:58 am 
User avatar
Joomla! Exemplar
Joomla! Exemplar

Joined: Fri Sep 09, 2005 2:13 pm
Posts: 7962
Location: The Netherlands
did you connected the database by this script?

_________________
Robert Dam - Joomla Forum Moderator
Dutch Boards | Joomla Coding Boards | English Support Boards


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Wed Apr 23, 2008 9:30 am 
User avatar
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Sep 06, 2007 10:33 am
Posts: 13
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.


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Sun Jun 08, 2008 7:29 pm 
Joomla! Fledgling
Joomla! Fledgling

Joined: Sun Jun 08, 2008 3:42 pm
Posts: 4
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:
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);


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Tue Jun 24, 2008 9:28 am 
User avatar
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Sep 06, 2007 10:33 am
Posts: 13
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?


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Tue Jun 24, 2008 12:21 pm 
Joomla! Fledgling
Joomla! Fledgling

Joined: Sun Jun 08, 2008 3:42 pm
Posts: 4
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 ?


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Tue Jun 24, 2008 1:19 pm 
User avatar
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Sep 06, 2007 10:33 am
Posts: 13
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  
 
 Post subject: Re: How to get user id
PostPosted: Tue Aug 12, 2008 11:25 pm 
Joomla! Fledgling
Joomla! Fledgling

Joined: Wed Jun 28, 2006 10:43 pm
Posts: 3
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.


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Tue Aug 26, 2008 10:08 am 
Joomla! Fledgling
Joomla! Fledgling

Joined: Tue Aug 26, 2008 10:00 am
Posts: 1
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:
<input type="text" readonly="yes" value="userID">

On the place of userID should just come your ID number.
Could anyone help plz?

Thx


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Sat Aug 30, 2008 8:28 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Sun Aug 03, 2008 5:09 pm
Posts: 30
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:
<?php
echo 'hello';
$user =& JFactory::getUser();
echo 'You ar user' .$user.'! Welcome!';
?>

It;s obvious that it doesn't work.
If I add
Code:
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
Quote:
$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
Quote:
$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


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Mon Sep 01, 2008 5:57 am 
User avatar
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Wed May 09, 2007 7:53 am
Posts: 235
Location: Bangalore, Karnataka, India
Hi there :pop ,
've u ppl tryed
Code:
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/


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Fri Oct 10, 2008 7:45 am 
Joomla! Fledgling
Joomla! Fledgling

Joined: Fri Oct 10, 2008 7:40 am
Posts: 2
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:

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


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Sun Nov 23, 2008 12:00 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Sun Aug 03, 2008 6:05 am
Posts: 38
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.


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Mon Nov 24, 2008 2:12 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Fri Jan 11, 2008 7:38 am
Posts: 26
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 :

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


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Mon Nov 24, 2008 7:34 pm 
User avatar
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Oct 23, 2008 7:00 pm
Posts: 19
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


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Tue Nov 25, 2008 11:22 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Wed Nov 19, 2008 7:42 am
Posts: 5
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.


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Thu Feb 19, 2009 11:30 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Feb 12, 2009 1:11 pm
Posts: 15
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!


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Fri Mar 20, 2009 3:41 pm 
Joomla! Fledgling
Joomla! Fledgling

Joined: Fri Mar 20, 2009 3:39 pm
Posts: 1
I use this code to get id from user


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


Top
 Profile  
 
 Post subject: Re: How to get user id
PostPosted: Mon Mar 23, 2009 10:20 am 
Joomla! Fledgling
Joomla! Fledgling

Joined: Mon Mar 23, 2009 10:12 am
Posts: 1
Hello everybody,

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

Code:
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:
$u_id = $_COOKIE["userid_m"];


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 1, 2  Next



Who is online

Users browsing this forum: No registered users and 23 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