free counter using MySql ?

Howling Wolf

We did not deserve this !
Joined
11 Dec 2001
Messages
1,243
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 !
 
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); 
?>
 
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)
 
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 :)
 
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 ! :)
 
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)
 
I use awstats myself, I dont like hit counters :)
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back