Newbie: Simple Counter
Moderator: General Moderators
-
d3ad1ysp0rk
- Forum Donator
- Posts: 1661
- Joined: Mon Oct 20, 2003 8:31 pm
- Location: Maine, USA
If the text file has "blah" as it's contents, I don't think thats gonna make much of a difference (ie. Our site has blah hits since march 20th, 2004, OR Our site has blai hits since march 20th, 2004).nigma wrote:Yea, you don't need to add anything. But, say somehow this counter.txt file has the string "blah" in it and not a number, then you increment "blah" and get "blai" and then you output that to the screen saying that is how many hits your site has. Now chances are that this might not ever happen, but still, some people may want to consider adding some ways to check for such an abnormality.
Point is, if the file has content that shouldnt be there, then you have to change it either way (whether it increments or not), so why add extra code?
Actually, a file is really inferior. Think about concurrent visitors.LiLpunkSkateR wrote:For a simple counter, this ISNT the inferior way. mysql would be a bad decision. Creating the db, creating the table/vars, connecting to the db, selecting the table, creating the query, getting the values, updating the value(s), saving the data, disconnecting from the db.. OR.. open file, read file, update data, write to file, close file.Sami wrote:I'd much rather prefer they learned it the best way first, instead of learning an inferior way first and then realizing there is a better method weeks/months later.
Once you get started on more than one thing (ips, refferer, etc.) dbs become a good idea. tracking JUST hits, files seem better IMHO..
You would need to have file locking, as opposed to mysql where you can simply update foo set count=count+1;
-
d3ad1ysp0rk
- Forum Donator
- Posts: 1661
- Joined: Mon Oct 20, 2003 8:31 pm
- Location: Maine, USA
-
d3ad1ysp0rk
- Forum Donator
- Posts: 1661
- Joined: Mon Oct 20, 2003 8:31 pm
- Location: Maine, USA
If it can happen, it will happen. And even a newbie wants bugfree software. Even if this means, he'll have to do some "annoying" setup of a database.LiLpunkSkateR wrote:and the likelyhood of that happening vs the annoyance of setting up a db/user/table/vars vs just adding a new file is?
anyways, not everyone has mysql.
Not everybody has access to the filesystem either. I've seen more hosts with safe_mode enabledb than without access to a database.
Here you go, both tutorials written by me.
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.
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.
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 - 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.
If you have any problems, please let me know.
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 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.
-
ldomingues
- Forum Commoner
- Posts: 41
- Joined: Fri Aug 06, 2004 1:15 pm
- Location: Portugal
Nice code!
I'd change the order of the sql operations, so that the visitor doesn't have to wait for the update to complete.
Also adding flush() is a good ideia for servers that cache output.
I'd change the order of the sql operations, so that the visitor doesn't have to wait for the update to complete.
Also adding flush() is a good ideia for servers that cache output.
Code: Select all
// 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");
// 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);
// add one before showing the counter
print $counter+1;
// added flush to output data before updating counter
flush();
// 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);
?>