free counter using MySql ?

Discussion in 'PC Gaming' started by Howling Wolf, Apr 23, 2003.

  1. Howling Wolf

    Howling Wolf We did not deserve this !

    Messages:
    1,243
    Location:
    Amongst the Native Ones
    I'm looking for a free text counter (not graphical) that could use an already existing and working MySql database...

    Any idea is more than welcome :)

    I'd like to be displayed on my page a simple text number that increments with every unique visitor like this [ 286 ] for example.
    Where and how should I insert this counter into my page(s) ?

    Thank you in advance for your help !
     
  2. Jason2k1

    Jason2k1 Guest

  3. NetRyder

    NetRyder Tech Junkie Folding Team

    Messages:
    13,256
    Location:
    New York City
    You can just use PHP and a single text file. No need of a MySQL database.

    Code:
    <?php 
    $counter = "counter.txt"; 
    
    function displayCounter($counter) { 
         $fp = fopen($counter,rw); 
         $num = fgets($fp,9999); 
         fclose($fp); 
         $fp = fopen($counter,w); 
         $num += 1; 
         print "$num"; 
         fputs($fp, $num); 
         fclose($fp); 
    } 
    
    displayCounter($counter); 
    ?>
    
     
  4. Howling Wolf

    Howling Wolf We did not deserve this !

    Messages:
    1,243
    Location:
    Amongst the Native Ones
    Thank you both !
    NetRyder, could you be a little more precise ?
    What variable should I set/change ? Where should I insert the code, in my html page ? Should I change from .html to .php ? Will it count unique visitors and not all hits ?
    Again, I'd like to use the table 'count' that's already working on my MySql database...
    As you can see, I have no idea of what's to be done, thank you for your help !

    P.s.: I have a counter at the moment, but I don't like it much (pretty ugly)
     
  5. NetRyder

    NetRyder Tech Junkie Folding Team

    Messages:
    13,256
    Location:
    New York City
    You're welcome ;)

    This is basically a very simple counter script that displays the total number of hits (not unique visitors) as plain text.
    You don't need to change any part of the code. Just copy and paste it exactly where you want the number to be displayed on your page.
    Yes, you will need to rename your file to .php
    You will also need to create a single blank text file called counter.txt in the same directory as your .php page.

    That's all there is to it :)
     
  6. Howling Wolf

    Howling Wolf We did not deserve this !

    Messages:
    1,243
    Location:
    Amongst the Native Ones
    NetRyder,
    is it you script ?
    If so, what could be added to it so that it only counts uniques and doesn't count me ?
    That would be ideal for me !

    Thank you ! :)
     
  7. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    I use this

    PHP:
    <?php
    // Unique hit counter
    $ip $_SERVER['REMOTE_ADDR'];
    $browser $_SERVER['HTTP_USER_AGENT'];

    // mysql get array
    $mysql mysql_pconnect("mysql_host","mysql_username","mysql_password");
    mysql_select_db("mysql_database");
    $cnt_qry mysql_query("SELECT * FROM stats");
    $count mysql_num_rows($cnt_qry);
    $query mysql_query("SELECT * FROM stats WHERE ip='$ip'");
    $rows mysql_num_rows($query);
    if(
    $rows 1)
    {
        
    $add_hit mysql_query("INSERT INTO stats (ip,browser) VALUES ('$ip','$browser')");
        
    $count++;
    }
    echo 
    "$count Unique";
    ?>
    It counts only Unique hits

    MySQL structure is
    CREATE TABLE `stats` (
    `id` INT( 11 ) NOT NULL AUTO_INCREMENT,
    `ip` VARCHAR( 24 ) NOT NULL ,
    `browser` VARCHAR( 32 ) NOT NULL ,
    PRIMARY KEY ( `id` )
    ) TYPE = MYISAM ;


    The PHP Query to create if you cant use phpMyAdmin
    PHP:
    $sql 'CREATE TABLE `stats` ( `id` INT( 11 )'
    $sql .= ' NOT NULL AUTO_INCREMENT, `ip` VARCHAR( 24 )'
    $sql .= ' NOT NULL , `browser` VARCHAR( 32 ) NOT NULL , '
    $sql .= 'PRIMARY KEY ( `id` ) ) TYPE = MYISAM ; ';
    You could then put the stats php code into an includes directory in your site, and then use this to get it
    PHP:
    <?php include "include/stats.php"?>
    This assumes that the page getting the counter is in the parent folder to the include folder

    public_html
    | +images
    | +include
    `- stats.php
    | index.php

    You can see this code workin on my site It is the top one, the one below is supposed to count all hits but not ones which "come from my site" (like when you click one of the links on my site it shouldnt increment but it does)
     
  8. NetRyder

    NetRyder Tech Junkie Folding Team

    Messages:
    13,256
    Location:
    New York City
    Very nice! :) Thanks Geffy! That could be useful.
     
  9. SPeedY_B

    SPeedY_B I may actually be insane.

    Messages:
    15,800
    Location:
    Midlands, England
    I use awstats myself, I dont like hit counters :)
     
  10. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    Awstats all the way.