FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Flat file-based, MySQL-based counter





shinji_x19
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.

php:
<?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.
php:
<?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.
Quote:
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.
php:
<?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.
php:
<?php include("http://location.to.your/new/counter.php"); ?>
Animal
Moved to Tutorials forum
Related topics
need help in a simple php register script based on flat file
Flat file question
Any flat file DBMS that is suitable for portable application
PM Script
New CMS - ToendaCMS - No database necessary!
Want a news script which doesnt use MySQL?
Phoenix free PHP scripts
150 frih to make this SIMPLE script
best cms for newbies?
Can someone please help me with my website?
data extraction from dat flat file
Logic MySql
PHP-Fusion | An Open Source CMS | PHP & MySQL Based
[GUIDE]Testing Out Your Site Without Uploading It
Reply to topic    Frihost Forum Index -> Miscellaneous -> Tutorials

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.