TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   i am just using my fantasi but can i do like this ? (http://www.talkphp.com/absolute-beginners/3520-i-am-just-using-my-fantasi-but-can-i-do-like.html)

codefreek 10-24-2008 09:13 PM

i am just using my fantasi but can i do like this ?
 
"don't flame me now as i am really tired have been up all night",
yeah well my question is can i do this ?

PHP Code:

 function onlineCheck()
{
    
$q " SELECT * FROM users WHERE last_active='NOW()'";
    
$pp mysql_query($q); 
    
$icheckd mysql_num_rows($pp);
    if (
$icheckd 1
    {
    
        print 
"</br>he/she is still online!";
        exit;
    
    }

    else 
    {
        
$q "SELECT * FROM users WHERE last_active='NOW()'";
        
$pp mysql_query($q); 
        
$icheckd mysql_num_rows($pp);
        if (
$icheckd 0)
        { 
            
$sql "UPDATE `users` SET `last_active` = 'NOW()'";            
            
$query mysql_query($sql) or die(mysql_error());
        }
    }


Thank you in advance!
^^:-$

codefreek 10-25-2008 12:58 AM

**fantasy**

masfenix 10-25-2008 05:35 AM

Simply put: no. Time is always changing. They only way that could work is that you CONSTANTLY update the last_active column (almost every second or half a second) and even then it will be really inaccurate.

You can acheive this with Sessions I think.

codefreek 10-25-2008 07:18 AM

thats true i will try that.

Tanax 10-25-2008 01:57 PM

Here's how to do it with sessions:

php Code:
<?php   
        public function user_login($user_id) {
           
            $sql = sprintf("    UPDATE
                                    `%s`
                                SET
                                    `%s` = '%s',
                                    `%s` = NOW(),
                                    `%s` = NOW()
                                WHERE
                                    `%s` = '%d'"
,
                   
                                $this->db->table['users'],
                                $this->db->col['user_session'],
                                session_id(),
                                $this->db->col['user_last_visit'],
                                $this->db->col['user_last_action'],
                                $this->db->col['user_id'],
                                $user_id);
                               
            $this->db->query($sql);
           
        }
       
        public function user_is_active() {
           
            $sql = sprintf("    UPDATE
                                    `%s`
                                SET
                                    `%s` = NOW()
                                WHERE
                                    `%s` = '%s'
                                LIMIT 1"
,
                               
                                $this->db->table['users'],
                                $this->db->col['user_last_action'],
                                $this->db->col['user_session'],
                                session_id());
                               
            $this->db->query($sql);
           
        }
       
        public function is_user_logged_in() {
           
            $sql = sprintf("    SELECT
                                    `%s`
                                FROM
                                    `%s`
                                WHERE
                                    `%s` = '%s'
                                LIMIT 1"
,
                               
                                $this->db->col['user_id'],
                                $this->db->table['users'],
                                $this->db->col['user_session'],
                                session_id());
           
                           
            $query = $this->db->query($sql);
           
            if(@mysql_num_rows($query)) {
               
                return true;
               
            }
           
            return false;
           
           
        }
       
        public function user_logout() {
           
            $sql = sprintf("    UPDATE
                                    `%s`
                                SET
                                    `%s` = ''
                                WHERE
                                    `%s` = '%s'
                                LIMIT 1"
,
                               
                                $this->db->table['users'],
                                $this->db->col['user_session'],
                                $this->db->col['user_session'],
                                session_id());
                               
            $query = $this->db->query($sql);
           
            if(!$query) {
               
                return false;
               
            }
           
            return true;
           
        }
?>

codefreek 10-25-2008 02:02 PM

or you could do like this xD

PHP Code:

$query "SELECT `username` FROM `users` WHERE `last_online` >= DATE_SUB(NOW(), INTERVAL 15 MINUTES)";
$result mysql_query($query);

if(
mysql_num_rows($result) === 0)
{
   echo 
"<p>No one is online.</p>";
}
else
{
   echo 
'<p>Current online users:</p><ul>';
   while(
$row mysql_fetch_assoc($result))
   {
          echo 
'<li>'$row['username'], '</li>';
   }
   echo 
'</ul>';



output on all the user sites ;)
PHP Code:

$user $_SESSION['username'];

$query sprintf("UPDATE `users` SET `last_online` = NOW() WHERE `username` = '%s'"mysql_real_escape_string($user));

mysql_query($query); 


Tanax 10-25-2008 02:41 PM

Well yes.. But that's not using sessions, which was the tip you got from masfenix

codefreek 10-26-2008 01:47 AM

yeah well this worked for me also i learned something new of it :)

>= DATE_SUB(NOW(), INTERVAL 15 MINUTES)";
$result = mysql_query($query);


All times are GMT. The time now is 01:33 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0