Check if joomla user is logged in in external php script

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.
vrillon
Joomla! Apprentice
Joomla! Apprentice
Posts: 25
Joined: Tue Jan 04, 2011 3:25 pm

Check if joomla user is logged in in external php script

Postby vrillon » Tue Jan 04, 2011 3:37 pm

I am writing a page that has nothing to do with joomla, but i need to check if a joomla user is logged in and the username of the joomla user.

Now i have the following to check if a user is logged in:

Code: Select all

define( '_JEXEC', 1 );

define( '_VALID_MOS', 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' );



/* Create the Application */

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

$mainframe->initialise();


/* Make sure we are logged in at all. */

if (JFactory::getUser()->id == 0)

   die("You have to be logged in.");


And this to save the username:

Code: Select all

$name = JFactory::getUser()->username;


This works for me and some other users, but some users get to see that they're not logged in and it doesn't matter if they log out and login again.

How can i solve this so that it works for all the users?

vrillon
Joomla! Apprentice
Joomla! Apprentice
Posts: 25
Joined: Tue Jan 04, 2011 3:25 pm

Re: Check if joomla user is logged in in external php script

Postby vrillon » Wed Jan 05, 2011 1:51 pm

I have created a test user and logged in on my pc, again it works, i let someone where it wouldn't work log in with that same account and it still wouldn't work..
what is different about what i do in my code than what is happening on the joomla site itself? because on the joomla site their login is recognized.

Fly06
Joomla! Intern
Joomla! Intern
Posts: 83
Joined: Tue Aug 12, 2008 1:58 pm
Contact:

Re: Check if joomla user is logged in in external php script

Postby Fly06 » Wed Jan 05, 2011 2:58 pm

Hello,

You code is correct except that you do not trigger the onAfterInitialize plugins.

One of these plugins is called RememberMe and prevents some users from having to log in again when they come back even if their session has expired.
http://docs.joomla.org/Screen.plugins.e ... emember_Me

To support this RememberMe mecanism just add the following two lines to your code (after the initialization):

Code: Select all

JPluginHelper::importPlugin('system');
$mainframe->triggerEvent('onAfterInitialise');

Regards,
-Fly06

vrillon
Joomla! Apprentice
Joomla! Apprentice
Posts: 25
Joined: Tue Jan 04, 2011 3:25 pm

Re: Check if joomla user is logged in in external php script

Postby vrillon » Wed Jan 05, 2011 3:56 pm

@fly06
Thank you for your respons..
I have tested this, but it still wouldn't work.

With some people it does work and with some people it doesn't and it also doesn't work if they just have logged in.

I have placed the code under: $mainframe->initialise(); in my code above.

edit: With people where it does work, including myself, it works even if i have it openened for hours.. (it's a page which is constantly refreshed by AJAX).

Fly06
Joomla! Intern
Joomla! Intern
Posts: 83
Joined: Tue Aug 12, 2008 1:58 pm
Contact:

Re: Check if joomla user is logged in in external php script

Postby Fly06 » Wed Jan 05, 2011 5:31 pm

vrillon wrote:With some people it does work and with some people it doesn't and it also doesn't work if they just have logged in.

Are you sure these people are not simply testing using two different browsers?

vrillon wrote:edit: With people where it does work, including myself, it works even if i have it openened for hours.. (it's a page which is constantly refreshed by AJAX).

What page is constantly refreshed by ajax?
And what is the purpose of this?

Regards,
-Fly06

vrillon
Joomla! Apprentice
Joomla! Apprentice
Posts: 25
Joined: Tue Jan 04, 2011 3:25 pm

Re: Check if joomla user is logged in in external php script

Postby vrillon » Wed Jan 05, 2011 6:24 pm

Fly06 wrote:
vrillon wrote:With some people it does work and with some people it doesn't and it also doesn't work if they just have logged in.

Are you sure these people are not simply testing using two different browsers?

Yes i am sure.

Fly06 wrote:What page is constantly refreshed by ajax?
And what is the purpose of this?

The php page where is checked if the user is logged in.. the purpose is that it is a sort of chat.

If i refer a person where it doesn't work to my home pc with a default joomla installation and let them login there, then this script does work for them. (of course runned on my home pc) But on the site it is meant for it doesn't work for some users.
It is very strange.

Fly06
Joomla! Intern
Joomla! Intern
Posts: 83
Joined: Tue Aug 12, 2008 1:58 pm
Contact:

Re: Check if joomla user is logged in in external php script

Postby Fly06 » Thu Jan 06, 2011 9:29 am

vrillon wrote:If i refer a person where it doesn't work to my home pc with a default joomla installation and let them login there, then this script does work for them. (of course runned on my home pc) But on the site it is meant for it doesn't work for some users.
It is very strange.

I am not sure to understand what you are doing.

I already mentioned a basic requirement on the client side to make this works.

On the server side, the external script must be placed at the root of the Joomla site on which your users are logged in.

Regards,
-Fly06

vrillon
Joomla! Apprentice
Joomla! Apprentice
Posts: 25
Joined: Tue Jan 04, 2011 3:25 pm

Re: Check if joomla user is logged in in external php script

Postby vrillon » Thu Jan 06, 2011 1:09 pm

It doesn't even matter what i am doing..

For me and some other users, it works.. so we open the page and we get to see we are logged in. (Ofcourse i get to see i am not logged in, when i am not logged in.)
For some other users it doesn't work.. so they open the page and get to see they are NOT logged in. (while they are in fact logged in)

Ofcourse the script is in the root of the joomla site, otherwise it also wouldn't work for me.

When i let a user where it doesn't work log in with the test account i made, which did work when I was logged into that account, it still didn't work... that would make you think that it has something to do with the user's pc..
But on the main joomla site they are recognised to be logged in.
So either i am missing something in my script, or the impossible has become possible.

Fly06
Joomla! Intern
Joomla! Intern
Posts: 83
Joined: Tue Aug 12, 2008 1:58 pm
Contact:

Re: Check if joomla user is logged in in external php script

Postby Fly06 » Thu Jan 06, 2011 5:44 pm

vrillon wrote:So either i am missing something in my script, or the impossible has become possible.

I can see two differences between your script and the standard Joomla code:
1/ define( '_VALID_MOS', 1 );
==> Not necessary but should not have any impact
2/ if (JFactory::getUser()->id == 0) { /* Your code */ }
==> The Joomla-compliant property to check is the visitor is logged in is the 'guest' property:
$user = JFactory::getUser();
if ($user->get('guest')) { /* Your code */}

if I where you I would also closely inspect the jos_session table...

Regards,
-Fly06

vrillon
Joomla! Apprentice
Joomla! Apprentice
Posts: 25
Joined: Tue Jan 04, 2011 3:25 pm

Re: Check if joomla user is logged in in external php script

Postby vrillon » Wed Jan 12, 2011 4:18 pm

@fly06 thank you for your response(s) but the last post still didn't work..
I just found out that on that site community builder is used, also for login, maybe there are differences?
But i also login through that community builder login.

And what could i be able to see in the jos_session table..?

Fly06
Joomla! Intern
Joomla! Intern
Posts: 83
Joined: Tue Aug 12, 2008 1:58 pm
Contact:

Re: Check if joomla user is logged in in external php script

Postby Fly06 » Thu Jan 13, 2011 8:19 am

I am running out of ideas.

Provided that:
- Tests are performed using only one browser
- The script is located at the root of the Joomla site on which your users are logged in
- This script is a perfect copy of the Joomla index.php file (the first half part of this file)

Well, it should just work.

Above you wrote that this script is actually runned using ajax, have you experienced the same problems by using direct http calls?

Regards,
-Fly06

cbehan
Joomla! Apprentice
Joomla! Apprentice
Posts: 30
Joined: Thu Feb 01, 2007 5:13 pm

Re: Check if joomla user is logged in in external php script

Postby cbehan » Sat Feb 05, 2011 9:23 pm

Great stuff and works exactly as we were looking for but I'm having a separate issue.

I've added this script to an functions file we've created for some external code that we're wrappering in our Joomla install.

Part of the code needs to do user auth, so we're using this script to validate the user is logged in and displaying user-unique data that resides mostly in another database.

Once we add this function into the code, we can no longer query our other database....it defaults all queries to the joom database instead of our other db.

Can anyone lend a hand?

cbehan
Joomla! Apprentice
Joomla! Apprentice
Posts: 30
Joined: Thu Feb 01, 2007 5:13 pm

Re: Check if joomla user is logged in in external php script

Postby cbehan » Sun Feb 06, 2011 5:12 pm

anyone?

Fly06
Joomla! Intern
Joomla! Intern
Posts: 83
Joined: Tue Aug 12, 2008 1:58 pm
Contact:

Re: Check if joomla user is logged in in external php script

Postby Fly06 » Tue Feb 08, 2011 5:00 pm

cbehan wrote:Once we add this function into the code, we can no longer query our other database....it defaults all queries to the joom database instead of our other db.

If you are using the Joomla database api in your code than it's quite normal that all queries are targeted to the joomla database.

You can still use the Joomla database api but you must first recreate à JDatabase object by passing your own database connexion data in the $option parameter of the getInstance() method of the JDatabase class (see method _createDBO() of the JFactory class).

Hope this helps.

-Fly06

2020media
Joomla! Apprentice
Joomla! Apprentice
Posts: 13
Joined: Tue Aug 07, 2007 3:46 pm
Location: London, UK
Contact:

Re: Check if joomla user is logged in in external php script

Postby 2020media » Mon Oct 31, 2011 12:29 pm

Thanks for this tip, it sorted my problem -

I assume it does the external script does not have to be in the site root if the paths are updated?
2020Media.com - UK Web Hosting
Joomla Hosting: http://www.2020media.com/joomla

yalcone
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Tue Apr 15, 2008 5:30 pm

Re: Check if joomla user is logged in in external php script

Postby yalcone » Tue Dec 20, 2011 6:16 pm

Thanks so much @vrillon. It works perfect.

maredzki
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Tue Jul 10, 2007 7:18 pm

Re: Check if joomla user is logged in in external php script

Postby maredzki » Sun Aug 05, 2012 3:04 am

This works perfectly if logged in through the front-end but is there a way to do exactly the same thing to check if the user has logged in via the back-end?

Thanks,
Marek

User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 24525
Joined: Mon Oct 27, 2008 9:27 pm
Location: Akershus, Norway

Re: Check if joomla user is logged in in external php script

Postby Per Yngve Berg » Sun Aug 05, 2012 1:04 pm

Code: Select all

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


Replace 'site' with 'administrator'


Return to “Joomla! 1.5 Coding”

Who is online

Users browsing this forum: No registered users and 7 guests