1
sarahmx
Help with User Recent Last Login Code
  • 2010/2/12 0:24

  • sarahmx

  • Quite a regular

  • Posts: 381

  • Since: 2007/10/28


Hi,


This code below will show user who recently login to your site

example like

sarahmx
2 minutes and 25 seconds ago

and in the code if the user have not yet have an avatar it will show an image from /uploads/blank_avatar

and if the user has avatar uploaded it will show his/her avtar


create a php custom block and named it as recent login and enter this code


//credits to JCbones of Hotscripts.com and XOOPS community
function timeDifference($start,$end,$return='days') {
    
//change times to Unix timestamp.
    //$start = strtotime($start);
    //$end = strtotime($end);
    //subtract dates
//echo 'Start Timestamp: ' . $start . '<br/>End Timestamp: ' . $end . '<br/>';
    
$difference max($end$start) - min($end,$start);
    
$time NULL;
    
//calculate time difference.
    
switch($return) {
        case 
'days':
             
$days floor($difference/86400);
                
$difference $difference 86400;
                    
$time['days'] = $days;
        case 
'hours':
            
$hours floor($difference/3600);
                
$difference $difference 3600;
                    
$time['hours'] = $hours;
        case 
'minutes':
            
$minutes floor($difference/60);
                
$difference $difference 60;
                    
$time['minutes'] = $minutes;
        case 
'seconds':
            
$seconds $difference;
                
$time['seconds'] = $seconds;
    }
    
    if(
is_array($time))
        foreach(
$time as $key => $value) {
            if(
$value 0) {
                
$output[] = $value ' ' ucwords($key);
            }
        }
        return 
implode(',',$output);
    }


$now time();
$hours 24;
$kira=0;
$time = ( intval$hours ) > ) ? time() - ( intval$hours ) * 3600 ) : ( time() - 24*3600 );
global 
$xoopsDB;


echo 
"<table><tr>";
$sql "SELECT distinct uid, uname, user_avatar, last_login FROM ".$xoopsDB->prefix("users")." WHERE level > 0 AND uid NOT IN (0) AND last_login >= '" $time "' ORDER BY last_login DESC";
$result $xoopsDB->query($sql);

while (list(
$uid$uname,$user_avatar,$last_login) = $xoopsDB->fetchRow($result) ) {
//Put this variable in the echo statements where you would like them to show up.
$sinceLastLogin ' ' timeDifference($last_login,$now,'hours') . ' ago ';
$kira++;
if (
$user_avatar == 'blank.gif')
{
echo 
"<td style='padding:5px;text-align:center'><a href='".XOOPS_URL."/userinfo.php?uid=".$uid."'><img src='".XOOPS_URL."/uploads/blank_avatar.gif' title='".$uname."' border='0' alt='".$uname."' height='48' width='48'></a><br /><a href='".XOOPS_URL."/userinfo.php?uid=".$uid."'>".$uname."</a><br />$sinceLastLogin</td>";
}

else

{
echo 
"<td style='padding:5px;text-align:center'><a href='".XOOPS_URL."/userinfo.php?uid=".$uid."'><img src='".XOOPS_URL."/uploads/".$user_avatar."' title='".$uname."' border='0' alt='".$uname."' height='48' width='48'></a><br /><a href='".XOOPS_URL."/userinfo.php?uid=".$uid."'>".$uname."</a><br />$sinceLastLogin</td>";
}

if(
$kira == 4)

 {

  echo 
"</tr><tr>";

 }}

echo 
"</tr></table>";


the problem right now is it seems the time can't reach more than 5 minutes

after it reach more than 5 minutes

it will reset back to 0 seconds ago....



btw does anyone know how to autorefresh this block or make it ajaxized

2
Mamba
Re: Help with User Recent Last Login Code
  • 2010/2/12 7:09

  • Mamba

  • Moderator

  • Posts: 11366

  • Since: 2004/4/23


Did you try this "Online History" module from Germany?

3
ghia
Re: Help with User Recent Last Login Code
  • 2010/2/12 7:45

  • ghia

  • Community Support Member

  • Posts: 4953

  • Since: 2008/7/3 1


I don't see this problem with your code.

If you view the users profile, is their last_login date corresponding with the showed value?

4
sarahmx
Re: Help with User Recent Last Login Code
  • 2010/2/12 8:32

  • sarahmx

  • Quite a regular

  • Posts: 381

  • Since: 2007/10/28


Quote:

Did you try this "Online History" module from Germany?

yes i know ..have tried it before ...

its not working properly in my site...don't know how to make it work properly...its not showing online users like in myxoops.org

and the module missing language definition
_MB_ONLINEHISTORY_MAXUSER

the code pasted above worked as i wanted..just have the problem i stated...

Quote:

I don't see this problem with your code.

If you view the users profile, is their last_login date corresponding with the showed value?


yes ?

the time will increase after you login from 0 seconds ago to minutes ago and to hours ago as time goes by

the problem is just its resetting the time back to 0 seconds ago after it reached 5 minutes..strange..

where else can i check that is causing this ?

5
ghia
Re: Help with User Recent Last Login Code
  • 2010/2/12 8:53

  • ghia

  • Community Support Member

  • Posts: 4953

  • Since: 2008/7/3 1


The time is corresponding with the last login date.
For logged in users, this last login is updated every 5 minutes in common.php, to cope with the remember me which can give else inaccurate results.
I did the test with unlogged users, which gave correct results for the longer periods.

6
sarahmx
Re: Help with User Recent Last Login Code
  • 2010/2/12 9:07

  • sarahmx

  • Quite a regular

  • Posts: 381

  • Since: 2007/10/28


Thank you Ghia

Quote:

The time is corresponding with the last login date.
For logged in users, this last login is updated every 5 minutes in common.php, to cope with the remember me which can give else inaccurate results.


so can this be change and still make the remember me feature working ? and what to change ?


Quote:

I did the test with unlogged users, which gave correct results for the longer periods.


how to test the code with guest ?

7
ghia
Re: Help with User Recent Last Login Code
  • 2010/2/12 9:20

  • ghia

  • Community Support Member

  • Posts: 4953

  • Since: 2008/7/3 1


Put this in comment
if ((intval($xoopsUser->getVar('last_login')) + 60 5) < time()) {
            
$sql "UPDATE " $xoopsDB->prefix('users')
                 . 
" SET last_login = '" time()
                 . 
"' WHERE uid = " $_SESSION['xoopsUserId'];
            @
$xoopsDB->queryF($sql);
        }

Remember me stays working, but the last login date stays on the time the user filled in his password.

Save your code in tstlastlogin.php and add
<?php
include 'mainfile.php';

in front and
?>

at the end

8
sarahmx
Re: Help with User Recent Last Login Code
  • 2010/2/12 10:24

  • sarahmx

  • Quite a regular

  • Posts: 381

  • Since: 2007/10/28


Thanks for all the Help Ghia...

need another help....

is it possible if the time reach to 1 hour don't show seconds

just show like 2 hours, 23 minutes ago

instead of 2 hours,23 minutes, 24 seconds ago

TQ


9
ghia
Re: Help with User Recent Last Login Code
  • 2010/2/12 11:17

  • ghia

  • Community Support Member

  • Posts: 4953

  • Since: 2008/7/3 1


Try with:
if (count($time) >= 2) break;
        case 
'minutes':
            
$minutes floor($difference/60);
                
$difference $difference 60;
                    
$time['minutes'] = $minutes;
                       if (
count($time) >= 2) break;

10
sarahmx
Re: Help with User Recent Last Login Code
  • 2010/2/12 12:07

  • sarahmx

  • Quite a regular

  • Posts: 381

  • Since: 2007/10/28


thanks Ghia

Now any seconds is not displaying at all even for the first 60 seconds

i want the seconds not to display only if it reach 1 hours

or


maybe just show seconds for first 60 seconds only





Login

Who's Online

227 user(s) are online (146 user(s) are browsing Support Forums)


Members: 0


Guests: 227


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Apr 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits