Flat file-based, MySQL-based counter

Tutorials on PHP, databases and other aspects of web development. Before posting a question, check in here to see whether there's a tutorial that covers your problem.

Moderator: General Moderators

Post Reply
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Flat file-based, MySQL-based counter

Post by phice »

Creating a File-based Counter
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 Three - Upload PHP file into your server
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.
CREATE TABLE `counter` (`key` VARCHAR(255) NOT NULL, `value` INT(12) NOT NULL);
INSERT INTO `counter` (`key`, `value`) VALUES ('counter', '0');
Step Three - Create a file called 'counter.php' and place the following PHP code into it.

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 Three - Upload PHP file into your server
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
Image Image
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Very nice work on the text formatting. You've got style 8)
Post Reply