<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Burn A Brain &#187; counter</title>
	<atom:link href="http://www.burnabrain.com/tag/counter/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.burnabrain.com</link>
	<description>Burn With Style!</description>
	<lastBuildDate>Wed, 25 Jan 2012 23:32:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>A Simple PHP Visitor Counter</title>
		<link>http://www.burnabrain.com/simple-php-visitor-counter/</link>
		<comments>http://www.burnabrain.com/simple-php-visitor-counter/#comments</comments>
		<pubDate>Wed, 01 Oct 2008 04:55:34 +0000</pubDate>
		<dc:creator>Brainiac</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[counter]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.burnabrain.com/?p=17</guid>
		<description><![CDATA[This article is designed to help you make a very simple hit counter for your php enabled website. I will talk you through each stage and explain both what is happening and why. There are many ways that we can achieve our goal of creating a visitor counter using PHP. In this article I will [...]
Related posts:<ol>
<li><a href='http://www.burnabrain.com/simple-facts/' rel='bookmark' title='Simple Facts'>Simple Facts</a></li>
<li><a href='http://www.burnabrain.com/create-flat-tv/' rel='bookmark' title='Simple guide on how to create a flat TV'>Simple guide on how to create a flat TV</a></li>
<li><a href='http://www.burnabrain.com/horsepower/' rel='bookmark' title='HorsePower'>HorsePower</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><img class="size-full wp-image-53 alignleft" title="brainer_sciencestein" src="http://burnabrain.s3.amazonaws.com/wp-content/uploads/2008/10/brainer_sciencestein.jpg" alt="" width="150" height="110" />This article is designed to help you make a very simple hit counter for your php enabled website.  I will talk you through each stage and explain both what is happening and why. There are many ways that we can achieve our goal of creating a visitor counter using PHP.  In this article I will show you how to create one using a text file and how to incorporate it into any page using a simple code snippet.</p>
<p class="componentheading">Requirements</p>
<ul>
<li>A strong grasp of HTML  is reccomended before attempting to tackle PHP.</li>
</ul>
<ul>
<li>A PHP enabled web server running PHP 4.x.x or higher on either a unix or Microsoft Windows platform.</li>
<li>This is a basic article, however some knowledge of PHP is desired, such as what functions are, what variables are and the basics of the syntax in general.</li>
</ul>
<p><span id="more-17"></span></p>
<p class="componentheading">The Basics</p>
<p>The very 1st thing we need to do is decide where the text file we are going to store our visitor count is going to be on the server.  You can choose to place it anywhere you like, in the example we use the path <strong>c:\htdocs</strong> and the file is called <strong>counter.txt.</strong> This means we have a full path for the file that will store our visitor count of<strong> c:\htdocs\counter.txt</strong>.</p>
<p>Open this file up, type in the number &#8217;0&#8242; &#8211; without the quotes &#8211; save it and exit.  We have now set our counter to 0, or zero.  Our script is going to use this file as a reference to see how many people have visited our site and then increment it for every new visitor.</p>
<p>Our second decision is what are we going to call our file.  The file is seperate from the pages you wish to monitor, and as discussed briefly in the Introduction we will cover how to include it in your other pages a bit later.  For the purposes of the tutorial I have named our file <strong>visitorcount.php</strong>.  You need to create this file, again, wherever you like, it does NOT have to be in a directory your webserver can access.</p>
<p>So by now we should have two files:  counter.txt and visitorcount.php</p>
<p class="componentheading">The Code</p>
<p>The first thing we will do is define the name of the file that will record the number of visitors and reference this to a variable. This is the file counter.txt that we created earlier.</p>
<p><!-- PARSE RESULT  --> <span style="color: #000000;"> <span style="color: #ff8000;">//define the file name and its path<br />
</span><span style="color: #0000bb;">$filename </span><span style="color: #007700;">= </span><span style="color: #dd0000;">&#8220;c:\\htdocs\\counter.txt&#8221;</span><span style="color: #007700;">;</span></span><br />
<span style="color: #000000;"><span style="color: #ff8000;">//note we escape the backslashes in the file system path </span></span><br />
Then we need to open the file for reading so we can check the current value, and have this referenced to a variable. <span style="color: #000000;"><span style="color: #ff8000;">//open the file<br />
</span><span style="color: #0000bb;">$handle </span><span style="color: #007700;">= </span><span style="color: #0000bb;">fopen</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$filename</span><span style="color: #007700;">, </span><span style="color: #dd0000;">&#8220;rb&#8221;</span><span style="color: #007700;">) or die (</span><span style="color: #dd0000;">&#8220;Failed to open the file&#8221;</span><span style="color: #007700;">);</span></span><br />
<span style="color: #000000;"><span style="color: #ff8000;">//note we are using this on a windows system and</span></span><br />
<span style="color: #000000;"><span style="color: #ff8000;">//thus use the &#8220;b&#8221; parameter as it is a Binary file system. </span></span><br />
Next we need to read the current value in the file, so we can then add 1 to it to represent our latest visitor. <span style="color: #000000;"><span style="color: #ff8000;">//read the contents of the file, in our case it will be an integer<br />
</span></span><span style="color: #000000;"><span style="color: #0000bb;">$data </span><span style="color: #007700;">= </span><span style="color: #0000bb;">fread</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$handle</span><span style="color: #007700;">, </span><span style="color: #0000bb;">filesize</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$filename</span><span style="color: #007700;">)) or die (</span><span style="color: #dd0000;">&#8220;Failed to read the file&#8221;</span><span style="color: #007700;">); </span></span></p>
<p><span style="color: #000000;"><span style="color: #ff8000;">//lets now add 1 to the number we read, therefore recording our new visitor<br />
</span><span style="color: #0000bb;">$data </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$data </span><span style="color: #007700;">+ </span><span style="color: #0000bb;">1</span><span style="color: #007700;">;</span></span><br />
<span style="color: #000000;"><span style="color: #ff8000;">//note we take the variable $data and simply add one to it, reusing</span></span><br />
<span style="color: #000000;"><span style="color: #ff8000;">//the previously defined variable for ease. </span></span><br />
Then we close the file for now. <span style="color: #000000;"><span style="color: #0000bb;">fclose</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$handle</span><span style="color: #007700;">);</span></span><br />
<span style="color: #000000;"><span style="color: #ff8000;">//and close the file as we dont need it any more </span></span><br />
The next part uses slightly different methods, the reason for this is just to show there are many ways to identical actions using PHP. It is time to write our new value to the file. We check that the file is writeable (i.e has the correct permissions set on the server). The code below is commented and speaks for itself.</p>
<p><span style="color: #000000;"><span style="color: #ff8000;">//lets now write to our file.<br />
//make sure the file exists and is writable first.<br />
</span><span style="color: #007700;">if (</span><span style="color: #0000bb;">is_writable</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$filename</span><span style="color: #007700;">)) {</span></span></p>
<p><span style="color: #ff8000;">//if it isnt then throw up an error and exit the script.<br />
</span><span style="color: #007700;">if (!</span><span style="color: #0000bb;">$handle </span><span style="color: #007700;">= </span><span style="color: #0000bb;">fopen</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$filename</span><span style="color: #007700;">, </span><span style="color: #dd0000;">&#8216;wb&#8217;</span><span style="color: #007700;">)) {</span><br />
<span style="color: #000000;"><span style="color: #ff8000;">//note the useage of &#8220;wb&#8221; in fopen() &#8216;s mode parameter</span></span><br />
<span style="color: #000000;"><span style="color: #ff8000;">//as we are Writing(w) to a binary file system(b).<br />
</span><span style="color: #007700;">echo </span><span style="color: #dd0000;">&#8220;Failed to open the file&#8221;</span><span style="color: #007700;">;</span></span><br />
<span style="color: #ff8000;">//this is the error message it will show if the file is not writeable </span><span style="color: #000000;"><span style="color: #007700;"><br />
exit;<br />
}</span></span></p>
<p><span style="color: #ff8000;">// If it is writeable lets write our new number to our opened file.<br />
</span><span style="color: #007700;">if (</span><span style="color: #0000bb;">fwrite</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$handle</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$data</span><span style="color: #007700;">) === </span><span style="color: #0000bb;">FALSE</span><span style="color: #007700;">) {<br />
echo </span><span style="color: #dd0000;">&#8220;Failed to write to the file&#8221;</span><span style="color: #007700;">; </span><span style="color: #ff8000;">//error and exit if failed.<br />
</span><span style="color: #007700;">exit;<br />
} </span></p>
<p>If all was succesful and we updated the file with the new value, we can then echo out the visitor number, or hit count. If you want to keep this data private just comment the line below out. <span style="color: #000000;"><span style="color: #007700;"><br />
echo </span><span style="color: #dd0000;">&#8220;Welcome visitor number $data&#8221;</span><span style="color: #007700;">; </span></span><br />
Then we close the file. <span style="color: #000000;"><span style="color: #007700;"><br />
</span><span style="color: #0000bb;">fclose</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$handle</span><span style="color: #007700;">); </span><span style="color: #ff8000;">//and close the file as we dont need it any more</span></span></p>
<p><span style="color: #007700;">} else { </span><br />
This is the error we show if we can not access the file. <span style="color: #000000;"><span style="color: #007700;"> echo </span><span style="color: #dd0000;">&#8220;The file is not accessable.&#8221;</span><span style="color: #007700;">; </span><span style="color: #ff8000;">//error and exit if failed<br />
</span><span style="color: #007700;">exit();<br />
} </span></span><br />
Now we need a way to make this work on our website. To include this in any php page on your website simply include the page at the point you wish the counter to be displayed. You can format the number with HTML as normal.</p>
<p><span style="color: #0000bb;">&lt;?php </span><span style="color: #007700;">include_once(</span><span style="color: #dd0000;">&#8220;visitorcount.php&#8221;</span><span style="color: #007700;">);</span><span style="color: #0000bb;">?&gt;<br />
</span><br />
And thats it. A very simple hit counter using php and a text file. The full interupted code is below.</p>
<p>&lt;?php</p>
<p><span style="color: #000000;"><span style="color: #0000bb;"> </span><span style="color: #ff8000;">//define the file name and its path<br />
</span><span style="color: #0000bb;">$filename </span><span style="color: #007700;">= </span><span style="color: #dd0000;">&#8220;c:\\htdocs\\counter.txt&#8221;</span><span style="color: #007700;">;</span></span><br />
<span style="color: #000000;"><span style="color: #ff8000;">//note we escape the backslashes in the file system path</span></span></p>
<p>//open the file<br />
<span style="color: #0000bb;">$handle </span><span style="color: #007700;">= </span><span style="color: #0000bb;">fopen</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$filename</span><span style="color: #007700;">, </span><span style="color: #dd0000;">&#8220;rb&#8221;</span><span style="color: #007700;">) or die (</span><span style="color: #dd0000;">&#8220;Failed to open the file&#8221;</span><span style="color: #007700;">);</span><span style="color: #000000;"><span style="color: #ff8000;"><br />
//note we are using this on a windows system and thus use the</span></span><br />
<span style="color: #000000;"><span style="color: #ff8000;">//&#8221;b&#8221; parameter as it is a Binary file system.</span></span></p>
<p>//read the contents of the file, in our case it will be an integer<br />
<span style="color: #0000bb;">$data </span><span style="color: #007700;">= </span><span style="color: #0000bb;">fread</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$handle</span><span style="color: #007700;">, </span><span style="color: #0000bb;">filesize</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$filename</span><span style="color: #007700;">)) or die (</span><span style="color: #dd0000;">&#8220;Failed to read the file&#8221;</span><span style="color: #007700;">);</span></p>
<p><span style="color: #ff8000;">//lets now add 1 to the number we read, therefore recording our new visitor<br />
</span><span style="color: #0000bb;">$data </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$data </span><span style="color: #007700;">+ </span><span style="color: #0000bb;">1</span><span style="color: #007700;">;</span><br />
<span style="color: #000000;"><span style="color: #ff8000;">//note we take the variable $data and simply add one to it,</span></span><br />
<span style="color: #000000;"><span style="color: #ff8000;">//reusing the previously defined variable for ease.</span></span></p>
<p><span style="color: #0000bb;">fclose</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$handle</span><span style="color: #007700;">); </span><span style="color: #ff8000;">//and close the file as we dont need it any more</span></p>
<p>//lets now write to our file.<br />
//make sure the file exists and is writable first.<br />
<span style="color: #007700;">if (</span><span style="color: #0000bb;">is_writable</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$filename</span><span style="color: #007700;">)) {</span></p>
<p><span style="color: #ff8000;">//if it isnt then throw up an error and exit the script.<br />
</span><span style="color: #007700;">if (!</span><span style="color: #0000bb;">$handle </span><span style="color: #007700;">= </span><span style="color: #0000bb;">fopen</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$filename</span><span style="color: #007700;">, </span><span style="color: #dd0000;">&#8216;wb&#8217;</span><span style="color: #007700;">)) { </span><br />
<span style="color: #000000;"><span style="color: #ff8000;">//note the useage of &#8220;wb&#8221; in fopen() &#8216;s mode parameter as</span></span><br />
<span style="color: #000000;"><span style="color: #ff8000;">//we are Writing(w) to a binary file system(b).<br />
</span><span style="color: #007700;">echo </span><span style="color: #dd0000;">&#8220;Failed to open the file&#8221;</span><span style="color: #007700;">;<br />
exit;<br />
}</span></span></p>
<p><span style="color: #ff8000;">// Write our new number to our opened file.<br />
</span><span style="color: #007700;">if (</span><span style="color: #0000bb;">fwrite</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$handle</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$data</span><span style="color: #007700;">) === </span><span style="color: #0000bb;">FALSE</span><span style="color: #007700;">) {<br />
echo </span><span style="color: #dd0000;">&#8220;Failed to write to the file&#8221;</span><span style="color: #007700;">;</span><br />
<span style="color: #000000;"><span style="color: #ff8000;">//error and exit if failed.<br />
</span><span style="color: #007700;">exit;<br />
}</span></span></p>
<p>echo <span style="color: #dd0000;">&#8220;Welcome visitor number $data&#8221;</span><span style="color: #007700;">;</span></p>
<p><span style="color: #0000bb;">fclose</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$handle</span><span style="color: #007700;">); </span><span style="color: #ff8000;">//and close the file as we dont need it any more</span></p>
<p><span style="color: #007700;">} else {<br />
echo </span><span style="color: #dd0000;">&#8220;The file is not accessable.&#8221;</span><span style="color: #007700;">; </span><span style="color: #ff8000;">//error and exit if failed<br />
</span><span style="color: #007700;">exit();<br />
}</span></p>
<p><span style="color: #0000bb;">?&gt;<br />
</span><br />
<span style="color: #0000bb;">&lt;?php<br />
</span><span style="color: #ff8000;">//THIS IS TO BE USED ON THE PAGE THAT YOU</span><br />
<span style="color: #000000;"><span style="color: #ff8000;">//WANT THE COUNTER TO WORK ON &#8211; NOT AS PART OF THE MAIN CODE!<br />
</span><span style="color: #007700;">include_once(</span><span style="color: #dd0000;">&#8220;visitorcount.php&#8221;</span><span style="color: #007700;">);<br />
</span><span style="color: #0000bb;">?&gt;<br />
</span> </span></p>
<p>Related posts:<ol>
<li><a href='http://www.burnabrain.com/simple-facts/' rel='bookmark' title='Simple Facts'>Simple Facts</a></li>
<li><a href='http://www.burnabrain.com/create-flat-tv/' rel='bookmark' title='Simple guide on how to create a flat TV'>Simple guide on how to create a flat TV</a></li>
<li><a href='http://www.burnabrain.com/horsepower/' rel='bookmark' title='HorsePower'>HorsePower</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.burnabrain.com/simple-php-visitor-counter/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Database Caching 3/20 queries in 0.004 seconds using apc
Object Caching 568/584 objects using apc
Content Delivery Network via Amazon Web Services: S3: burnabrain.s3.amazonaws.com

Served from: burnabrain.com @ 2012-02-04 19:57:59 -->
