Welcome Guest, Not a member yet? Register   Sign In
Visitors Online?
#11

[eluser]Piter[/eluser]
hello, my database to MYSQL 5.0.81-community PHP 5. ...
My code looks like this:
/ system / application / librares / onlinelib.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');


       /*******
       * Author: Carl Wuensche
       * Date: June 7 2008
       * What it does: It is a class that enables you to easily check if you're online,
       * and lists the online users.
       *******/

class Onlinelib {
      var $username; // The variable that stores your username
      var $time; // The variable that stores the time you've been signed on
      var $session_id; // The variable that stores the session_id

      /**
       * This is the function that loads everything. You don't really need to do anything in here,
       * however it does check to see if you are online or not
       **/

  function __construct()
  {
    $this->CI =& get_instance();
    $this->CI->load->library(array('session'));
    $this->username = "Guest";
    $this->time = time();
    $this->session_id = $this->CI->session->userdata('session_id');

    if ($this->checkOnline())
    {
     $this->CI->db->where('session_id', $this->session_id);
     $this->CI->db->update('online', array('time' => $this->time));
    } else {
    $this->CI->db->set('username', $this->username);
    $this->CI->db->set('time', $this->time);
    $this->CI->db->set('session_id', $this->session_id);
    $this->CI->db->insert('online');
    }
  }
  
   /**
    * Returns the number of people online
    **/

  function countOnline()
  {
   $count = $this->CI->db->get('online');
   return $this->CI->db->count_all();
  }
   /**
   * Checks to see if your session is in the online list.
   **/
  function checkOnline()
  {
  $this->CI->db->where('session_id', $this->session_id);
  $this->CI->db->where('time', ($this->time - $this->time) > 300);
  $query = $this->CI->db->get('online');
  if ($query)
  {
    return TRUE;
  } else {
    return FALSE;
  }
  }
   /**
   * ToDo: Make this a function that lists the usernames in the online table.
   **/
  function listOnline()
  {

  }

}
/aplication/system/config/autload.php
$autoload['libraries'] = array('database','session', 'onlinelib', 'breadcrumbs',  'layout', 'user_agent');


/application/system/controllers/filmiki.php
        $data['pageTitle'] = 'Filmiki';
        $data['affected'] = $this->onlinelib->countOnline();
        $this->layout->view('filmiki_view', $data);

/application/system/view/filmiki_view.php

<h2>Filmiki</h2>
&lt;?php echo $affected; ?&gt;
Result: Filmiki
0


What can I do wrong?


The tables in the database created as written. 'online'
#12

[eluser]stuffradio[/eluser]
I'm about to go to bed, I'll try it tomorrow with the current CI version and see if it works or not.
#13

[eluser]Piter[/eluser]
Ok, I will be waitingSmile
#14

[eluser]JoostV[/eluser]
Have you thought about this?

1. use database for your sessions
2. add a userId field to you sessiontable and fill it when a user logs in.
3. do a join query on your sessiontable (every row is a user) and your user table in the lines of
Code:
SELECT u.id, u.username FROM users as u JOIN session as s ON (s.userId = u.id)
This will return the names and Ids of every user online. You can use userId to link to their profile.

All you need to add now is maybe some garbage collecting to make sure there are no expired session data in your session table.
#15

[eluser]Piter[/eluser]
Welcome back, whether you have tested it on a new CodeIgniter 1,7 ? I have a lack of ideas. Please help.
#16

[eluser]JoostV[/eluser]
Hi PIter,

There is no 'Welcome back' in this thread. Is that stuffradio you are talking to now?
#17

[eluser]Piter[/eluser]
Sorry if I wrote something wrong, do not know much English, I use Google Translate and probably difficult for me to understand.
#18

[eluser]JoostV[/eluser]
Write a My_Session library in which you add code to save user_ids and fetch them as well.

1. Add column user_id to your session table.
2. On succesful login, delete all sessions that use the current user_id.
Code:
$this->session->delete_by_user_id($user_id);
3. Save user_id to session table.
Code:
$this->session->save_user_id($user_id);
4. Do some extra garbage collection to remove expired sessions
Code:
$this->session->gc_probability = 100;$this->session->sess_gc();
4. In the controller that you whish to show online users, retrieve all users from the session table to display a list of all online users.
Code:
// Fetch online users
$this->data['online_users'] = array();
$this->db->select('users.user_id, users.user_login');
$this->db->from('sessions');
$this->db->join('users', 'sessions.user_id = users.use_id');
$query = $this->db->get();
if ($query->num_rows() > 0) {
    $this->data['online_users'] = $query->result_array();
}
5. Display online users in view
Code:
if (count($online_users) > 0) {
    foreach ($online_users as $user) {
        echo anchor('profile/view/' .$user['user_id'], $user['user_login']) . '; ';
     }
}
#19

[eluser]David Johansson[/eluser]
The problem is in the original code from stuffradio, he wrote:

Code:
function countOnline()
  {
   $count = $this->CI->db->get('online');
   return $this->CI->db->count_all();
  }

This will always return zero since it counts the rows of an empty set.
The working code should be either one of the following:

Code:
function countOnline()
  {
   return $this->CI->db->count_all('online');
  }

or:

Code:
function countOnline()
  {
   $count = $this->CI->db->get('online');
   return $this->CI->db->num_rows();
  }




Theme © iAndrew 2016 - Forum software by © MyBB