Step One - In the same directory as you plan on putting the PHP file for this counter, create a file 'counter.txt' and CHMOD it to 0777.
Step Two - Create a file called 'counter.php' and place the following PHP code into it.
Code: Select all
<?php
// Change this if you rename your counter.txt file
$counter_file = "counter.txt";
// Get the contents of the current counter file
if($f = @fopen($counter_file, "r"))
{
@$contents = fread($f, @filesize($counter_file));
@fclose($f);
}
// File is empty
if($contents == "")
{
$contents = 0;
}
// File contents is not a number
if(!is_numeric($contents))
{
print "Counter contents is not a number.";
define('ERROR', 1);
}
// Bump the count
if(ERROR <> 1)
{
$contents++;
// Add contents back to file
if($f = @fopen($counter_file, "w"))
{
@fwrite($f, $contents);
@fclose($f);
}
// Print the contents
print $contents;
}
?>Step Four - Place the following code (setting the same url as if you were to go to it in the browser) where you want to place your counter. You call the file from the http:// location so it processes, and then you get the contents of the file without worrying about file location or any of that.
Code: Select all
<?php include("http://location.to.your/new/counter.php"); ?>Creating a MySQL-based Counter
NOTE - This is for the more advanced user. If you don't have phpMyAdmin installed, you can download it at http://www.phpmyadmin.net/.
Step One - Make sure you have MySQL installed on your server and you have the right server address, username, password and database. (If you dont have MySQL installed or dont have these settings, please contact your server administrator/host (I cant help you get these settings myself).
Step Two - Browse to your phpMyAdmin and run this query in your selected database.
Step Three - Create a file called 'counter.php' and place the following PHP code into it.CREATE TABLE `counter` (`key` VARCHAR(255) NOT NULL, `value` INT(12) NOT NULL);
INSERT INTO `counter` (`key`, `value`) VALUES ('counter', '0');
Code: Select all
<?php
// Change these settings to match yours
define('SERVER', 'localhost');
define('DATABASE', '');
define('USER', '');
define('PASS', '');
define('TABLE', 'counter');
// Connect to MySQL
@mysql_connect(SERVER, USER, PASS)
or print(mysql_error());
$db = @mysql_select_db(DATABASE)
or print(mysql_error() . "<br />\n");
// Update the field
$result = @mysql_query("UPDATE `" . TABLE . "` SET value = value + 1 WHERE `key` = 'counter'")
or print(mysql_error() . "<br />\n");
@mysql_free_result($result);
// Get the value
$result = @mysql_query("SELECT `value` FROM `" . TABLE . "` WHERE `key` = 'counter'")
or print(mysql_error() . "<br />\n");
$row = @mysql_fetch_object($result);
$counter = $row->value;
@mysql_free_result($result);
print $counter;
?>Step Four - Place the following code (setting the same url as if you were to go to it in the browser) where you want to place your counter. You call the file from the http:// location so it processes, and then you get the contents of the file without worrying about file location or any of that.
Code: Select all
<?php include("http://location.to.your/new/counter.php"); ?>If you have any problems, please let me know.
extracted from this thread
