Question

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

Was it helpful?

Solution

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 &.

OTHER TIPS

/* 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 />';
}

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top