Custom PHP script: need to check user is logged in

Discussion and education for beginner / novice programmers interested in embarking on the development process to take advantage of the extensible nature of the Joomla! CMS.

Moderators: tjay, seadap, Rogue4ngel, matthewhayashida

Forum rules
Post Reply
svivian
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Wed Feb 27, 2008 4:03 pm

Custom PHP script: need to check user is logged in

Post by svivian » Wed Feb 27, 2008 4:14 pm

I am working on a custom PHP script for a client. We use Joomla for the website's content so I have created some pages for registered users only. They use the "Wrapper" feature to put custom URLs inside an iframe.

While users need to log in to get to these pages, the scripts that are displayed in the iframe are currently unprotected. Is there a way to check if a user is logged into Joomla, eg a function that can be called? I've looked in some of the Joomla code but I'm not really sure what is where!

User avatar
pe7er
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 162
Joined: Thu Aug 18, 2005 8:55 pm
Location: Nijmegen, The Netherlands
Contact:

Re: Custom PHP script: need to check user is logged in

Post by pe7er » Wed Feb 27, 2008 4:33 pm

Within Joomla extensions / templates you can use:

Code: Select all

global $my;

if ($my->id>0){
// registered user has been logged in.
}
However, you cannot use this in your wrapped pages...
Kind Regards,
Peter Martin (aka pe7er)
db8.nl - Joomla! implementation, programming, template and component development [Dutch]
>> Questions? Get help more easily with JTS-post Assistant: viewtopic.php?f=428&t=272481

svivian
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Wed Feb 27, 2008 4:03 pm

Re: Custom PHP script: need to check user is logged in

Post by svivian » Mon Mar 03, 2008 3:31 pm

OK done some browsing of the Joomla code, the following PHP snippet should check that a user is logged in. First you need some setup. This PHP script is in a subfolder so it includes files from the directory above.

Code: Select all

define( '_VALID_MOS', 1 );
include_once '../configuration.php';
include_once '../globals.php';
include_once $mosConfig_absolute_path . '/includes/joomla.php';
// set up Joomla 'session'
$mainframe = new mosMainFrame( $database, 'login', '..' );
$mainframe->initSession(); 
Then you can get a user with:

Code: Select all

$user = $mainframe->getUser(); 
It appears that if there is a valid user, this function will return an object with the id, name, username, etc. If there is no user logged in, it returns the object but with everything blank. So I check for a valid user with the following, redirecting if they're invalid. It uses a JS redirection in this case because it's being called from an iframe and I want to redirect the whole page, not the iframe.

Code: Select all

if ( $user->id == 0 || strlen(trim($user->username)) == 0 )
{
    // there is no user: redirect
    echo '<script type="text/javascript">window.parent.location.replace("LOGIN URL HERE");</script>';
    exit;
} 
Now I simply include this script at the very top of any page requiring a logged in Joomla user.

NOTE: I don't know if this is very secure, since there are other fields in the user object like 'activation' - which could return a user object if the user has registered but is not confirmed by an admin. Also I don't check the 'usertype' (Publisher, Editor, etc) in my script since I don't need it, but others might. Anyone using this might want to to a print_r and check other attributes in case they're required. I'd appreciate any comments/improvements from other members.

User avatar
pe7er
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 162
Joined: Thu Aug 18, 2005 8:55 pm
Location: Nijmegen, The Netherlands
Contact:

Re: Custom PHP script: need to check user is logged in

Post by pe7er » Mon Mar 03, 2008 4:01 pm

Thank you for your follow-up with your solution (for 1.5)!
The script that I posted is meant for Joomla 1.0.x
Kind Regards,
Peter Martin (aka pe7er)
db8.nl - Joomla! implementation, programming, template and component development [Dutch]
>> Questions? Get help more easily with JTS-post Assistant: viewtopic.php?f=428&t=272481

guitarhangar
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Wed Nov 28, 2007 10:16 pm
Contact:

Re: Custom PHP script: need to check user is logged in

Post by guitarhangar » Mon Mar 03, 2008 4:31 pm

pe7er wrote:Within Joomla extensions / templates you can use:

Code: Select all

global $my;

if ($my->id>0){
// registered user has been logged in.
}
However, you cannot use this in your wrapped pages...
You can if you use Jumi. (Sorry to be a contrarian :) ) Jumi will import all Joomla variables/objects for your using pleasure. Just make sure you include the prerequisite if defined _VALID or die blah blah.

Just write the .html in a file, upload it to your server, and have Jumi include it.
It's really easy to make things complicated- the trick is to make them simple.
http://guitarhangar.com - webmaster
http://www.amediacreative.com - programmer/security (they didn't make the above site)

ozenbas
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Mon Jul 16, 2007 12:33 pm

Re: Custom PHP script: need to check user is logged in

Post by ozenbas » Mon Mar 03, 2008 4:37 pm

I am so glad someone coded this, i will give it a try and see what happens but it seems that it will work with no problem. i will let u know if i need help or if there is any problem.

thanks for informing me as well

Great forum :)


Post Reply