Page 1 of 1
Custom PHP script: need to check user is logged in
Posted: Wed Feb 27, 2008 4:14 pm
by svivian
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!
Re: Custom PHP script: need to check user is logged in
Posted: Wed Feb 27, 2008 4:33 pm
by pe7er
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...
Re: Custom PHP script: need to check user is logged in
Posted: Mon Mar 03, 2008 3:31 pm
by svivian
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:
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.
Re: Custom PHP script: need to check user is logged in
Posted: Mon Mar 03, 2008 4:01 pm
by pe7er
Thank you for your follow-up with your solution (for 1.5)!
The script that I posted is meant for Joomla 1.0.x
Re: Custom PHP script: need to check user is logged in
Posted: Mon Mar 03, 2008 4:31 pm
by guitarhangar
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.
Re: Custom PHP script: need to check user is logged in
Posted: Mon Mar 03, 2008 4:37 pm
by ozenbas
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