WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this Article to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES SUBMIT AN ARTICLE PRINT
Title : A simple textfile based counter
Categories : PHP, File System Picture not available
Emanuel Nacht
Date : 2000-10-04
Grade : 0 of 5 (graded 0 times)
Viewed : 2533
Search : More Articles by Emanuel Nacht
Action : Grade This Article
Tools : My Favotite Articles


  Submit your own code examples 
 


This is the first article in quite a series I want to write. I've thought a while before starting to write to create some kind of plan for me, so I can cover allot of different aspects of PHP. Simultaneously to this series, about PHP, I'm working on a rather large article about setting up Apache with PHP support. PHP with MySQL, Sybase-CT, GD, BCMath, IMAP and more support. Apache with mod_perl, mod_SSL, mod_jserv and more support. As you can see, this is going to be a rather long article once it's finished and I've written all my comments to everything, but I hope it'll be worth the waiting!

Anyways, here's the first article of my series. This one is intended for beginners to PHP, so I'll comment about every line of code there is, for the more advanced user, this may or may not be useful. I'll be glad if at least one person reads this article and finds it useful :)

One more thing to add, all the articles I'm writing are based upon Linux with apache as web server, the articles
themselves are written under windows. If you have a windows-based web server, I can't guarantee for anything. Furthermore, everything is provided on an as-is basis, so don't come cry to me if you crashed your box which was up for 1000days and then crashed simply because you used one of my snippets... what I want to say, use the snippets if you can use them, send me some email if you think they're useful, or if you find a mistake or something, but don't blame me if they don't work on your box. I've tested all of them before I published them, and they worked for me.



Topic: A simple text file based counter
Skill level: newbies

Counters are always nice to have on your web page, so you have an approximate value of how many persons visited your web page. So what does a counter do? It simply increments a value each time someone enters your homepage, nothing more, nothing less (well, there are more advanced counters with statistic usages and and and, but more about this in another article). For this to work, the counter needs to store the number of page views somewhere, e.g.. in a text file. In PHP there are allot of file functions, that's why this is going to be rather easy.

What the script will do: First it will check if the file where the visits are stored already exists. If it does, it reads the number from the file, and writes it back. If the file on the other hand doesn't exist, it will be created with an initial value of 1 page view. But this doesn't display anything yet, so we need to tell the page display the value it stored to the file. Achieving this is pretty simple, as we already have the amount of page views stored in a variable (remember, we read the number of page views from the text file and increment it. This requires that we store it to a variable).

Enough theoretical stuff, let's start with the action!


1 <?php
2 $counterfile
="./counter.txt";
3 if (file_exists($counterfile)) {
4 $fp = fopen("$counterfile","r+");
5 $pageviews = fread ($fp,7);
6 $pageviews++;


line by line explanation of the first part of the script. The first line tells the web server that this is a PHP script, and not HTML or something else. On the second line, we tell the PHP interpreter which file to use to store the number of page views in. In this case, it's the file counter.txt in the current directory, e.g.. the directory where the script resides. On line number 3 we check whether or not the file exists. If the file exists we open the file on line 4. $fp is the file-handle we'll need for the further operations with the file, r+ opens the file read and writeable. Line 5 then reads the content of the file. Here we need the file handle we used on line 4 to open the file. 7 says that we read 7 bytes from the file, this means your counter may have a maximum value of 9999999. Which should be more than enough. On line 6 we then increment the umber of page views read from the text file.


7 rewind($fp);
8 fputs($fp, $pageviews, 7);
9 fclose($fp);
10 } else {




Now that we have the incremented value, we reset the pointer to point to the beginning of the file on line 7. when we're back on the beginning of the file, we write the incremented value to the file handle, again 7 bytes long. we then close the file, e.g.. write it back to the file system et voila the counter is incremented!


11 $fp = fopen("$counterfile","w");
12 $pageviews = 1;
13 fputs($fp, $pageviews, 7);
14 fclose($fp);
15 }
16 echo "this site has been visited $pageviews times!";


in the lines here, the file doesn't exist (else on line 10 tells us that it doesn't). So we re-create the file, the parameter "w" opens the file for writing only, places the file pointer at the beginning, and creates the file if it doesn't exist. The next lines do the same as above, but we set the number of page views to 1, as it's the first time the script gets started (unless you erased it academically). With the last line we write to the browser the number of page views... pretty simple huh?

Problems that may occur: If you get an error message that the web server is unable to write to the text file, or read it, then you have a permissions problem. You have to make sure that the user as which the web server runs as has enough privileges to write to the dir where you have your text file. A Most common mistake is also that you forget the semicolon to terminate the commands at the end of a line.

I hope that was useful to some new PHP coders, and send me some feedback about it to enacht@horus.ch!

Emanuel Nacht for weberdev.com









Simple Form-Based File Uploads
Categories : PHP, File System
Jump Start to Easy URLs
Categories : PHP, Beginner Guides, MySQL, File System, To PHP
Uploading files to the server with PHP
Categories : PHP, File System, HTML and PHP, HTTP
Working with Dates and Times in PHP
Categories : PHP, Date Time
10 PHP Functions I Bet You Didn't Know About!
Categories : PHP, PHP Functions, Filesystem, Arrays, Errors and Logging
Using the .NET Assembly in PHP
Categories : PHP, .NET
Date Arithmetic With MySQL
Categories : PHP, Databases, MySQL, Date Time
Saving Images in MySQL
Categories : MySQL, PHP, Graphics, Databases
PHP References Explained
Categories : PHP References, PHP
Aspect-Oriented Programming and PHP
Categories : PHP, Aspect Oriented Programming
Beginners guide to PHP and MySQL
Categories : PHP, Beginner Guides, Databases, MySQL, Installation
Logging with PHP
Categories : PHP, Log Files
Who's Linking?
Categories : PHP, Beginner Guides, To PHP
Building A Persistent Shopping Cart With PHP and MySQL
Categories : PHP, MySQL, Databases, Ecommerce
Generating One-Time URLs with PHP
Categories : PHP, URLs