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

[eluser]EEssam[/eluser]
Hello,

Is their a feature in CI to display how many visitors are connected to the application?

Thanks.
#2

[eluser]Derek Allard[/eluser]
Welcome to CodeIgniter EEssam. You'd need to build it yourself, and it would depend on how you tracked your visitors.
#3

[eluser]stuffradio[/eluser]
I started making one for you... I need to do stuff for now so I will post it later.
#4

[eluser]stuffradio[/eluser]
This is as far as I got:


The two tables:

Code:
CREATE TABLE `online` (
  `id` bigint(20) NOT NULL auto_increment,
  `session_id` varchar(40) NOT NULL,
  `username` varchar(50) NOT NULL,
  `time` varchar(30) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

Code:
CREATE TABLE `users` (
  `id` bigint(20) NOT NULL auto_increment,
  `username` varchar(50) NOT NULL,
  `time` varchar(30) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
file: 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 Onlinelib()
  {
    $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', $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->affected_rows();
  }
   /**
   * 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()
  {

  }

}

To use it:
Code:
$this->load->library('onlinelib', '', TRUE);

Code:
$data['affected'] = $this->onlinelib->countOnline();
   $this->load->view('hello', $data);

In your view file:

Code:
echo "Guests online:" . $affected;


That's as far as I got so far. What do you think? How can you improve it?

Also someone needs to fix this error:
Quote:A Database Error Occurred

Error Number: 1054

Unknown column '1212884761' in 'field list'

UPDATE `online` SET `1212884761` = '' WHERE `session_id` = '2022a8d666cecb1d2e3e41fafd185093'
#5

[eluser]Seppo[/eluser]
The whole countOnline() is wrong (affected_rows should be num_rows, and you can even use the count_all method instead).

The error seems to be when you call to the update method

$this->CI->db->update('online', array('time' => $this->time));
#6

[eluser]stuffradio[/eluser]
Thanks,

fixed this:

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 Onlinelib()
  {
    $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()
  {

  }

}

It all works now except it doesn't update the time online.
#7

[eluser]EEssam[/eluser]
Wow, thank you very much!
#8

[eluser]stuffradio[/eluser]
No problem, glad I could help Smile
#9

[eluser]Piter[/eluser]
Hi, count me this does not work all the time shows Online: 0, not at all want to save the data to the database on CI 1.7
I did everything as it is written here.
Please help

I greet the Polish
#10

[eluser]stuffradio[/eluser]
Hi, I wrote this a year a go. What is your database like?




Theme © iAndrew 2016 - Forum software by © MyBB