Visualizzazione dei risultati da 1 a 3 su 3

Discussione: contatore in php

  1. #1

    contatore in php

    salve a tutti!

    devo fare un contatore visite in php...ed oltre al contatore normale di visite del sito (memorizzando indirizzi ip e altre info "inutili" :master: ) volevo fare uno scriptino "utenti online", ma non mi viene in mente una soluzione...devo fare tutto con DB (MySQL), e pensavo di far memorizzare un valore in un campo del DB, ma come fa poi il sistema ad accorgersi che l'utente è uscito dal sito (o semplicemente ha chiuso il browser)?
    per risolvere questo problema devo necessariamente usare le sessioni?

    grazie in anticipo, e buona giornata a tutti!

    Fabrizio

    P.S.: attualmente un contatore c'è già, è stato messo da un mio collega, ma è uno script di terzi, un servizio collegato... dovrei in pratica simulare il suo funzionamento... se volete dare un'occhiata: www.premiobarocco.com (il contatore si trova in basso...sotto la colonna sinistra del sito.

    ciauz

  2. #2
    per gli user online prova questa classe :
    Codice PHP:
    <?php

        
    include "config.php";
    $db mysql_connect("$server""$username""$password");
    mysql_select_db("$database",$db);
    /*
    usersOnline.php
    Author: Ilir Fekaj
    Contact: [email]tebrino@hotmail.com[/email]
    Date: December 21, 2003
    Latest version & info: [url]http://www.free-midi.org/scripts/[/url]
    Demo: [url]http://www.free-midi.org[/url]

    This very simple class enables you to track number of visitors online in
    an easy and accurate manner. It's free for all purposes, just please don't
    claim you wrote it. If you have any problems, please feel free to contact me.
    Also if you use it, please send me the page URL.

    Example usage:

    include_once ("usersOnline.class.php");
    $visitors_online = new usersOnline();

    if ($visitors_online->count_users() == 1) {
        echo "There is " . $visitors_online->count_users() . " visitor online";
    }
    else {
        echo "There are " . $visitors_online->count_users() . " visitors online";
    }

    Important: You need to create database connection and select database before creating object!
    --------------------------------------------
    Table structure:
    CREATE TABLE `useronline` (
      `id` int(10) NOT NULL auto_increment,
      `ip` varchar(15) NOT NULL default '',
      `timestamp` varchar(15) NOT NULL default '',
      PRIMARY KEY (`id`),
      UNIQUE KEY `id`(`id`)
    ) TYPE=MyISAM COMMENT='' AUTO_INCREMENT=1 ;

    */

    class usersOnline {

        var 
    $timeout 600;
        var 
    $count 0;
        
        function 
    usersOnline () {
            
    $this->timestamp time();
            
    $this->ip $this->ipCheck();
            
    $this->new_user();
            
    $this->delete_user();
            
    $this->count_users();
        }
        
        function 
    ipCheck() {
        
    /*
        This function checks if user is coming behind proxy server. Why is this important?
        If you have high traffic web site, it might happen that you receive lot of traffic
        from the same proxy server (like AOL). In that case, the script would count them all as 1 user.
        This function tryes to get real IP address.
        Note that getenv() function doesn't work when PHP is running as ISAPI module
        */
            
    if (getenv('HTTP_CLIENT_IP')) {
                
    $ip getenv('HTTP_CLIENT_IP');
            }
            elseif (
    getenv('HTTP_X_FORWARDED_FOR')) {
                
    $ip getenv('HTTP_X_FORWARDED_FOR');
            }
            elseif (
    getenv('HTTP_X_FORWARDED')) {
                
    $ip getenv('HTTP_X_FORWARDED');
            }
            elseif (
    getenv('HTTP_FORWARDED_FOR')) {
                
    $ip getenv('HTTP_FORWARDED_FOR');
            }
            elseif (
    getenv('HTTP_FORWARDED')) {
                
    $ip getenv('HTTP_FORWARDED');
            }
            else {
                
    $ip $_SERVER['REMOTE_ADDR'];
            }
            return 
    $ip;
        }
        
        function 
    new_user() {
            
    $insert mysql_query ("INSERT INTO useronline(timestamp, ip) VALUES ('$this->timestamp', '$this->ip')");
        }
        
        function 
    delete_user() {
            
    $delete mysql_query ("DELETE FROM useronline WHERE timestamp < ($this->timestamp - $this->timeout)");
        }
        
        function 
    count_users() {
        


    $useronline = ("SELECT DISTINCT ip FROM useronline");
      
    $exec_useronline mysql_query($useronline) Or DIE (mysql_error());
      
    $count mysql_num_rows($exec_useronline);
              return 
    $count;
        }
          }

    ?>

  3. #3
    grazie mille, la provo più tardi e ti dico come è andata

    ciao ciao

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.