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!
Custom PHP script: need to check user is logged in
Moderators: tjay, seadap, Rogue4ngel, matthewhayashida
Forum rules
- pe7er
- 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
Within Joomla extensions / templates you can use:
However, you cannot use this in your wrapped pages...
Code: Select all
global $my;
if ($my->id>0){
// registered user has been logged in.
}
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
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
Re: Custom PHP script: need to check user is logged in
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.
Then you can get a user with:
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.
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.
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();
Code: Select all
$user = $mainframe->getUser();
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;
}
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.
- pe7er
- 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
Thank you for your follow-up with your solution (for 1.5)!
The script that I posted is meant for Joomla 1.0.x
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
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
-
- Joomla! Fledgling
- Posts: 3
- Joined: Wed Nov 28, 2007 10:16 pm
- Contact:
Re: Custom PHP script: need to check user is logged in
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.pe7er wrote:Within Joomla extensions / templates you can use:However, you cannot use this in your wrapped pages...Code: Select all
global $my; if ($my->id>0){ // registered user has been logged in. }
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)
http://guitarhangar.com - webmaster
http://www.amediacreative.com - programmer/security (they didn't make the above site)
Re: Custom PHP script: need to check user is logged in
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
thanks for informing me as well
Great forum