Reputation: 63
What is the best (fastest/safest/better for the long run) way to check if a member is signed in Joomla.
1# way: $user =& JFactory::getUser();
if($user->id!=0){
2# way: $user =JFactory::getUser()->guest;
or another way?
These are snippets out of a custom script to check if a user is a guest to tell him to signup and if he signed in to welcome him.
I am using Joomla 2.5
I have read on Joomla 2.5 check user logged in that some codes are different depending on the php version I am using? I am puzzled.
Thanks
Upvotes: 2
Views: 4425
Reputation: 951
For Joomla there are two ways you can check if user is loged in
$user = JFactory::getUser();
if($user->guest = 1){
//User in not logged in
}
else
{
//User is logged in
}
Or second one is
$user = JFactory::getUser();
if($user->id != 0){
//User is logged in
$id = $user->id; // You can use this ID for further Use
}
else
{
//User is no logged in
}
I personally Prefer Second one as it fulfill two usability at same time.
Upvotes: 1
Reputation: 71
/* some information about the current logged in user is displayed, but only when the user is actually logged in. */
$user =& JFactory::getUser();
if (!$user->guest) {
echo 'You are logged in as:<br />';
echo 'User name: ' . $user->username . '<br />';
echo 'Real name: ' . $user->name . '<br />';
echo 'User ID : ' . $user->id . '<br />';
}
Upvotes: 1
Reputation: 19733
You can use either assuming you're running PHP 5.3, however, if you're running anything below that, then use:
$user = JFactory::getUser();
if($user->id!=0){
//your code goes here
}
Personally, I would go for the second method as it goes through the Joomla API and is a tad more thorough.
On a side note, if you do use the first method, you can remove the &
.
Upvotes: 2