Ver Mensaje Individual
  #3 (permalink)  
Antiguo 18/12/2004, 02:04
Avatar de baklao
baklao
 
Fecha de Ingreso: noviembre-2004
Ubicación: Santa Ana - Isla de Margarita
Mensajes: 482
Antigüedad: 19 años, 4 meses
Puntos: 0
Usuarios en Linea

Pregunta: Saber cuantos usuarios hay en linea.

Autor original: Víctor Simental (vic_mx)

Respuesta:

Esta es la estructura de nuestra tabla:
Código:
CREATE TABLE usuariosenlinea (
timestamp int(15) NOT NULL default '0',
ip varchar(40) NOT NULL default '',
location varchar(100) NOT NULL default '',
KEY timestamp (timestamp),
KEY ip (ip),
KEY location (location)
) TYPE=MyISAM;
Código PHP:
 <?php

class Usuariosenlinea
{
/*
    * @param string $servidor el host para conectar, usualmente localhost
    * @param string $basededatos el nombre de la base de datos
    * @param string $nombredb el nombre de la tabla, preferentemente no editar
    * @param string $usuario usuario de la base de datos
    * @param string $pass la contraseña de la base de datos
*/

   
var $servidor 'localhost';
   var 
$basededatos '';
   var 
$nombredb '';
   var 
$usuario '';
   var 
$pass '';
  
## Fin de Conexion ##

  
var $e_rror;
  
//Segundos para borrar de la base de datos a los usuarios inactivos
  
var $segundos 120;
  var 
$ahora 0;

    
//CONSTRUCTOR
    
function Usuariosenlinea() {

    
$this->recargar();

    }

    function 
cuantos() {

    return 
$this->ahora;

    }

    function 
enlinea() {

        if(
$this->ahora == 1) {

    echo 
$this->ahora;
        }
        else
        {
    echo 
$this->ahora;
        }

    }

        function 
ipreal(){

            if (
$real_ip getenv('HTTP_X_FORWARDED_FOR')){

                
$explode_real_ip explode(","$real_ip);
                return 
trim($explode_real_ip[0]);
            }
            else
            {
            return 
getenv('REMOTE_ADDR');
            }
        }

        function 
error(){

        return 
$this->e_rror mysql_error();

        }

        function 
recargar() {

            
$tiempo_actual time();
            
$tiempo_final $tiempo_actual $this->segundos;
            
$ip $this->ipreal();

        @
mysql_connect($this->servidor$this->usuario$this->pass)
        or die(
'Error al Intentar Conectar con la base de datos '.$this->error().'');

        @
mysql_select_db($this->basededatos)
        or die(
'Error Seleccionando la base de datos '.$this->error().'');

        
$result mysql_query("SELECT ip FROM $this->nombredb WHERE ip='$ip'")
        or die(
'Error de lectura en la base de datos '.$this->error().'');

        if(
mysql_num_rows($result) == 0){

        
mysql_query("INSERT INTO $this->nombredb VALUES ('$tiempo_actual','$ip','$_SERVER[REQUEST_URI]')")
        or die(
'Error al Insertar en la base de datos '.$this->error().'');

        }
        else

        {
        
mysql_query("UPDATE $this->nombredb SET timestamp='$tiempo_actual' where ip='$ip'")
        or die(
'Error al Insertar en la base de datos '.$this->error().'');
        }

        
mysql_query("DELETE FROM $this->nombredb WHERE timestamp < $tiempo_final")
        or die(
'Error al intentar borrar en la base de datos '.$this->error().'');

        
$result mysql_query("SELECT ip FROM $this->nombredb")
        or die(
'Error de lectura en la base de datos '.$this->error().'');

        
$this->ahora mysql_num_rows($result);

        
mysql_close();

        }

}

?>
Ahora bien como hacemos el llamado en la pagina de nosotros para mostrarlo es de la siguiente manera:
Código HTML:
<?php
    include("class.online.php");
    $enlinea = new Usuariosenlinea(); 
    $enlinea->enlinea(); 
?>
Suerte y Salud2.
__________________
Atte. Mohamed :aplauso:

Última edición por Cluster; 16/02/2006 a las 11:44