Current location: Hot Scripts Forums » Programming Languages » PHP » extend Recent Login Code


extend Recent Login Code

Reply
  #1 (permalink)  
Old 02-08-10, 06:42 AM
sarahmx sarahmx is offline
Newbie Coder
 
Join Date: Jan 2010
Posts: 27
Thanks: 5
Thanked 0 Times in 0 Posts
Question extend Recent Login Code

Hi..

I'm a php newbie...

this code below is to show recent login member in xoops cms

is there anyway to extend this code to show when actually did the user login..
example minutes/seconds/hours/days ago

PHP Code:

$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,$poster,$last_login) = $xoopsDB->fetchRow($result) ) {
$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></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></td>";
}

if(
$kira == 4)

 {

  
$count=0;

  echo 
"</tr><tr>";

 }}

echo 
"</tr></table>"
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 02-08-10, 06:19 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
I'm assuming $last_login is a timestamp column in the database.

PHP Code:

<?php
/***************************************************************************************************
        *    Copyright (C) 2009  JCBones.                                                                            *
        *                                                                                                                            *
        *    This program is free software: you can redistribute it and/or modify                            *
        *    it under the terms of the GNU General Public License as published by                    *
        *    the Free Software Foundation, either version 3 of the License, or                            *
        *    any later version.                                                                                                *
        *                                                                                                                            *
        *    This program is distributed in the hope that it will be useful,                                    *
        *    but WITHOUT ANY WARRANTY; without even the implied warranty of                    *
        *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
        *    GNU General Public License for more details.                                                        *
        *                                                                                                                            *
        *    You should have received a copy of the GNU General Public License                        *
        *    along with this program.  If not, see <http://www.gnu.org/licenses/>.                        *
        *                FUNCTION TIMEDIFFERENCE.                                                                *
        *    set real dates for start and end, otherwise *nix the strtotime() lines.                        *
        *    $return 'days' will return days/hours/minutes/seconds.                                            *
        *    $return 'hours' will return hours/minutes/seconds.                                                    *
        *    $return 'minutes' will return minutes/seconds.                                                        *
        *    $return 'seconds' will return seconds.                                                                    *
        *                                                                                                                            *
        ***************************************************************************************************/
function timeDifference($start,$end,$return='days') {
    
//change times to Unix timestamp.
    //$start = strtotime($start);
    //$end = strtotime($end);
    //subtract dates
    
$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';
    }
    
    return 
$time;   
    
}

$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,$poster,$last_login) = $xoopsDB->fetchRow($result) ) {
//Put this variable in the echo statements where you would like them to show up.
$sinceLastLogin 'Last Login was: ' timeDifference($last_login,$now,'days') . ' 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></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></td>";
}

if(
$kira == 4)

 {

  
$count=0;

  echo 
"</tr><tr>";

 }}

echo 
"</tr></table>";  
?>
This should fix you up, Please read the comments, and put the variable where you would like it in the echo statement(s).

Last edited by Jcbones; 02-08-10 at 06:29 PM. Reason: It looks like $time is a unix stamp, fixing function for that.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 02-09-10, 12:18 AM
sarahmx sarahmx is offline
Newbie Coder
 
Join Date: Jan 2010
Posts: 27
Thanks: 5
Thanked 0 Times in 0 Posts
Thanks JCbones...this is almost working :-)

Quote:
I'm assuming $last_login is a timestamp column in the database.
hmm..no :-(

its int(10) with attributes: unsigned

the value in the database is save something like this :1259899647
and in user profile page it is display something like: 2010/2/9 13:06

what do i need to change ...?

if i didn't change the database this is what the result look like
Quote:
14649 Days, 5 Hours, 7 Minutes, 26 Seconds ago.
the time is wrong..cause i just log in as in the db and my profile page shows - 2010/2/9 13:06

actually what i wanted is the last login time is updated from seconds to minutes to hours etc ...not display all in one line

Last edited by sarahmx; 02-09-10 at 12:24 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 02-09-10, 10:37 AM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
Quote:
actually what i wanted is the last login time is updated from seconds to minutes to hours etc ...not display all in one line
??? Not sure what you mean by this.

I figured that your database was holding that value, it is a unix timestamp (seconds since Jan 01, 1970). I fixed the function to return time values based on that.

So, I'll explain the code for you.

time(); is a php function that returns a unix timestamp.
$last_login from your database also holds a unix timestamp.

time() - $last_login will give you the number of seconds from your last login, to this very moment.

Subtract 86400 seconds per day, 3600 seconds per hour, 60 seconds per minute, and your left with the remaining seconds. That is all that this function does.

If you will give me the layout you would like to accomplish, I will help you to get it integrated.

Jc.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 02-09-10, 07:23 PM
sarahmx sarahmx is offline
Newbie Coder
 
Join Date: Jan 2010
Posts: 27
Thanks: 5
Thanked 0 Times in 0 Posts
thanks jcbones..for your explanantion

my code is just for 24 hours right?..so the record will update every 24 hours right?

i want it something like this...

http://www.freeimagehosting.net/uploads/e28724047f.jpg

earlier should be ago..and in my code there is avatar

is this do-able ?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 02-09-10, 08:12 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
See what this does for you.

PHP Code:

<?php
function timeDifference($start,$end,$return='days') {
    
//change times to Unix timestamp.
    //$start = strtotime($start);
    //$end = strtotime($end);
    //subtract dates
    
$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, ';     
    }
    
    return 
$time;   
    
}

$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,$poster,$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') . ' earlier ]';
$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>$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>$sinceLastLogin</td>";
}

if(
$kira == 4)

 {

  
$count=0;

  echo 
"</tr><tr>";

 }}

echo 
"</tr></table>";  
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 02-10-10, 02:48 AM
sarahmx sarahmx is offline
Newbie Coder
 
Join Date: Jan 2010
Posts: 27
Thanks: 5
Thanked 0 Times in 0 Posts
Thank you JC Bones

the code show...
Quote:
351607 Hours, 42 Minutes, earlier

i refresh the page the minutes are updating...which is exactly what i wanted

hours is obviously wrong
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 02-10-10, 05:20 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
Sarahmx,

I would like to do some testing, as you stated, the hours are way off. Try this, and paste what the timestamps are.

PHP Code:

<?php
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, ';     
    }
    
    return 
$time;   
    
}

$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,$poster,$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') . ' earlier ]';
$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>$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>$sinceLastLogin</td>";
}

if(
$kira == 4)

 {

  
$count=0;

  echo 
"</tr><tr>";

 }}

echo 
"</tr></table>";  
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 02-10-10, 05:39 PM
sarahmx sarahmx is offline
Newbie Coder
 
Join Date: Jan 2010
Posts: 27
Thanks: 5
Thanked 0 Times in 0 Posts
it display

Quote:
Start Timestamp:
End Timestamp: 1265841320


sarahmx[ 351622 Hours, 35 Minutes, earlier ]
i just log-in i think minute is wrong too..i just login seconds ago but it shows 35 minutes earlier ?


i checked the last login fields show the correct time when i last login
2010/2/11 6:38

Last edited by sarahmx; 02-10-10 at 05:42 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 02-10-10, 08:46 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
OK, the problem is that we are not getting that last login field from the database. You can see that the Start Timestamp does not have a value set.

The reason I suspect is these lines:
PHP Code:

SELECT distinct uidunameuser_avatarlast_login FROM 

PHP Code:

list($uid$uname,$user_avatar,$poster,$last_login
Does not match, you have an extra variable there "$poster", which is probably holding your $last_login value. To test this, we should echo that out. Try this script here.

PHP Code:

<?php
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, ';     
    }
    
    return 
$time;   
    
}

$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,$poster,$last_login) = $xoopsDB->fetchRow($result) ) {
//Put this variable in the echo statements where you would like them to show up.
$sinceLastLogin '[ ' timeDifference($poster,$now,'hours') . ' earlier ]';
$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>$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>$sinceLastLogin</td>";
}

if(
$kira == 4)

 {

  
$count=0;

  echo 
"</tr><tr>";

 }}

echo 
"</tr></table>";  
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP inside JavaScript? pikaz JavaScript 20 07-10-10 01:55 AM
Help Needed Urgently with JS Login Script! semendemon JavaScript 5 11-25-07 12:24 PM
Login Script problem Justin171985 Script Requests 0 07-02-05 01:10 AM
Combine Multiple Login Scripts fernandose PHP 2 02-04-05 05:09 PM
HELP! Php to login to HTACCESS? godfather PHP 4 03-06-04 01:03 AM


All times are GMT -5. The time now is 02:50 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.