<?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>WLScripting.com &#187; Counters</title>
	<atom:link href="http://www.wlscripting.com/tutorial/category/counters/feed" rel="self" type="application/rss+xml" />
	<link>http://www.wlscripting.com</link>
	<description>PHP coding tutorials</description>
	<lastBuildDate>Wed, 10 Mar 2010 23:02:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9-beta-1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Users Online</title>
		<link>http://www.wlscripting.com/tutorial/24</link>
		<comments>http://www.wlscripting.com/tutorial/24#comments</comments>
		<pubDate>Thu, 27 Jul 2006 02:05:19 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[Basics]]></category>
		<category><![CDATA[Counters]]></category>

		<guid isPermaLink="false">http://wlscripting.com/tutorial/24</guid>
		<description><![CDATA[Count the number of users currently on your site.]]></description>
			<content:encoded><![CDATA[<p>Keeping track of how many users have been on your website is very easy. This can be done with some basic MySQL code and the date() function. All you have to do is keep track of the users IP address and time they were last on the page. Lets get to some code:</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?
</span><span class="hl-var">$server</span><span class="hl-code"> = </span><span class="hl-quotes">'</span><span class="hl-string">localhost</span><span class="hl-quotes">'</span><span class="hl-code">;
</span><span class="hl-var">$user</span><span class="hl-code"> = </span><span class="hl-quotes">'</span><span class="hl-string">root</span><span class="hl-quotes">'</span><span class="hl-code">;
</span><span class="hl-var">$pass</span><span class="hl-code"> = </span><span class="hl-quotes">''</span><span class="hl-code">;
</span><span class="hl-var">$db2</span><span class="hl-code"> = </span><span class="hl-quotes">'</span><span class="hl-string">online</span><span class="hl-quotes">'</span><span class="hl-code">;

</span><span class="hl-var">$db</span><span class="hl-code"> = </span><span class="hl-identifier">mysql_connect</span><span class="hl-brackets">(</span><span class="hl-var">$server</span><span class="hl-code">, </span><span class="hl-var">$user</span><span class="hl-code">, </span><span class="hl-var">$pass</span><span class="hl-brackets">) </span><span class="hl-reserved">or die</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Could not connect.</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-var">$db</span><span class="hl-brackets">) 
	</span><span class="hl-reserved">die</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">no db</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-identifier">mysql_select_db</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-var">$db2</span><span class="hl-quotes">&quot;</span><span class="hl-code">,</span><span class="hl-var">$db</span><span class="hl-brackets">))
 	</span><span class="hl-reserved">die</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">No database selected.</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$server_time</span><span class="hl-code">=</span><span class="hl-identifier">date</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">U</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$client_ip</span><span class="hl-code">=</span><span class="hl-var">$_SERVER</span><span class="hl-brackets">[</span><span class="hl-quotes">'</span><span class="hl-string">REMOTE_ADDR</span><span class="hl-quotes">'</span><span class="hl-brackets">]</span><span class="hl-code">;

</span><span class="hl-var">$query</span><span class="hl-code"> = </span><span class="hl-identifier">mysql_query</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">SELECT * FROM current_users WHERE ip='</span><span class="hl-var">$client_ip</span><span class="hl-string">'</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$check</span><span class="hl-code"> = </span><span class="hl-identifier">mysql_fetch_array</span><span class="hl-brackets">(</span><span class="hl-var">$query</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-var">$check</span><span class="hl-brackets">) {
	</span><span class="hl-var">$update</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">UPDATE current_users set time='</span><span class="hl-var">$server_time</span><span class="hl-string">' where ip='</span><span class="hl-var">$check</span><span class="hl-string">[ip]'</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
	</span><span class="hl-identifier">mysql_query</span><span class="hl-brackets">(</span><span class="hl-var">$update</span><span class="hl-brackets">) </span><span class="hl-reserved">or die</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Unable to process update: </span><span class="hl-quotes">&quot;</span><span class="hl-code"> . </span><span class="hl-identifier">mysql_error</span><span class="hl-brackets">())</span><span class="hl-code">;
</span><span class="hl-brackets">} </span><span class="hl-reserved">else </span><span class="hl-brackets">{
  </span><span class="hl-var">$sql</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">INSERT INTO `current_users` (`ip`, `time`) VALUES ('</span><span class="hl-var">$client_ip</span><span class="hl-string">', '</span><span class="hl-var">$server_time</span><span class="hl-string">')</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
  </span><span class="hl-var">$result</span><span class="hl-code"> = </span><span class="hl-identifier">mysql_query</span><span class="hl-brackets">(</span><span class="hl-var">$sql</span><span class="hl-brackets">) </span><span class="hl-reserved">or die</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Unable to process insert: </span><span class="hl-quotes">&quot;</span><span class="hl-code"> . </span><span class="hl-identifier">mysql_error</span><span class="hl-brackets">())</span><span class="hl-code">;
</span><span class="hl-brackets">}
</span><span class="hl-var">$time</span><span class="hl-code">=</span><span class="hl-var">$server_time</span><span class="hl-code">-</span><span class="hl-number">1800</span><span class="hl-code">;
</span><span class="hl-var">$remove</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">DELETE from current_users WHERE time&lt;'</span><span class="hl-var">$time</span><span class="hl-string">'</span><span class="hl-quotes">&quot;</span><span class="hl-code">; </span><span class="hl-comment">// Remove users outside time limit
</span><span class="hl-identifier">mysql_query</span><span class="hl-brackets">(</span><span class="hl-var">$remove</span><span class="hl-brackets">) </span><span class="hl-reserved">or die</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Unable to delete: </span><span class="hl-quotes">&quot;</span><span class="hl-code"> . </span><span class="hl-identifier">mysql_error</span><span class="hl-brackets">())</span><span class="hl-code">;

</span><span class="hl-comment">// Show the count
</span><span class="hl-var">$sql2</span><span class="hl-code"> = </span><span class="hl-identifier">mysql_query</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">SELECT ip FROM current_users</span><span class="hl-quotes">&quot; </span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$currentVisitors</span><span class="hl-code"> = </span><span class="hl-identifier">mysql_num_rows</span><span class="hl-brackets">(</span><span class="hl-var">$sql2</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-reserved">echo </span><span class="hl-quotes">'</span><span class="hl-string">There have been </span><span class="hl-quotes">'</span><span class="hl-code">.</span><span class="hl-var">$currentVisitors</span><span class="hl-code">.</span><span class="hl-quotes">'</span><span class="hl-string"> users online in the last 30 minuets.</span><span class="hl-quotes">'</span><span class="hl-code">;
</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
<p>The first query checks to see if your IP address is in the database. If your IP address is in the database it updates the time. If your IP address is not in the database, we then insert the time and your IP address.</p>
<p>To eliminate old users we automatically remove users who have been online after 30 minutes(1800 Seconds). If you want to change the time delay just multiply 60 times the number of minutes.<br />
10 Minutes = 600<br />
15 Minutes = 900<br />
20 Minutes = 1200<br />
30 Minutes = 1800<br />
1 Hour = 3600</p>
<p>Make sure you create a database and apply your settings to the variables at the top.<br />
Insert the following SQL query into your database:</p>
<blockquote><p>CREATE TABLE `current_users` (<br />
  `ip` varchar(30) NOT NULL default &#8221;,<br />
  `time` varchar(30) NOT NULL default &#8221;<br />
) TYPE=MyISAM;</p></blockquote>
<p>Just save this as online.php and include this in your page.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wlscripting.com/tutorial/24/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Flat file hit counter with image display</title>
		<link>http://www.wlscripting.com/tutorial/19</link>
		<comments>http://www.wlscripting.com/tutorial/19#comments</comments>
		<pubDate>Fri, 14 Jul 2006 13:44:57 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[Counters]]></category>

		<guid isPermaLink="false">http://wlscripting.com/?p=19</guid>
		<description><![CDATA[Create a flat file hit counter that displays hits with images.]]></description>
			<content:encoded><![CDATA[<p>If you liked the flat file hit counter tutorial, this is a little more advanced. This uses the same basic concept for counting and writing the hits, but is different in showing hits. Use the following code:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
</span><span class="hl-var">$file</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">hits.dat</span><span class="hl-quotes">&quot;</span><span class="hl-code">; </span><span class="hl-comment">// Hit count file
</span><span class="hl-var">$fp</span><span class="hl-code"> = @</span><span class="hl-identifier">fopen</span><span class="hl-brackets">(</span><span class="hl-var">$file</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">r</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">// Open hits in readonly
</span><span class="hl-var">$count</span><span class="hl-code"> = @</span><span class="hl-identifier">fread</span><span class="hl-brackets">(</span><span class="hl-var">$fp</span><span class="hl-code">, </span><span class="hl-identifier">filesize</span><span class="hl-brackets">(</span><span class="hl-var">$file</span><span class="hl-brackets">))</span><span class="hl-code">; </span><span class="hl-comment">// get hits</span><span class="hl-code">
@</span><span class="hl-identifier">fclose</span><span class="hl-brackets">(</span><span class="hl-var">$fp</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">// close file
</span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-var">$count</span><span class="hl-brackets">) {
  </span><span class="hl-var">$count</span><span class="hl-code">++; </span><span class="hl-comment">// Add 1 to hits if hits exist
</span><span class="hl-brackets">} </span><span class="hl-reserved">else </span><span class="hl-brackets">{
  </span><span class="hl-var">$count</span><span class="hl-code"> = </span><span class="hl-number">1</span><span class="hl-code">; </span><span class="hl-comment">// If no hits make hits1
</span><span class="hl-brackets">}
</span><span class="hl-var">$fp</span><span class="hl-code"> = </span><span class="hl-identifier">fopen</span><span class="hl-brackets">(</span><span class="hl-var">$file</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">w</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">// Open file for writing</span><span class="hl-code">
@</span><span class="hl-identifier">fputs</span><span class="hl-brackets">(</span><span class="hl-var">$fp</span><span class="hl-code">, </span><span class="hl-var">$count</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">// Write hits</span><span class="hl-code">
@</span><span class="hl-identifier">fclose</span><span class="hl-brackets">(</span><span class="hl-var">$fp</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">// close file
</span><span class="hl-reserved">for</span><span class="hl-brackets">(</span><span class="hl-var">$i</span><span class="hl-code">=</span><span class="hl-number">0</span><span class="hl-code">;</span><span class="hl-var">$i</span><span class="hl-code">&lt;</span><span class="hl-identifier">strlen</span><span class="hl-brackets">(</span><span class="hl-var">$count</span><span class="hl-brackets">)</span><span class="hl-code">;</span><span class="hl-var">$i</span><span class="hl-code">++</span><span class="hl-brackets">) {
  </span><span class="hl-var">$img</span><span class="hl-code"> = </span><span class="hl-identifier">substr</span><span class="hl-brackets">(</span><span class="hl-var">$count</span><span class="hl-code">,</span><span class="hl-var">$i</span><span class="hl-code">,</span><span class="hl-number">1</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">// Get next image
  </span><span class="hl-var">$hits</span><span class="hl-code"> .= </span><span class="hl-quotes">&quot;</span><span class="hl-string">&lt;img src='images/</span><span class="hl-var">$img</span><span class="hl-string">.gif'&gt;</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
</span><span class="hl-brackets">}
</span><span class="hl-reserved">echo </span><span class="hl-var">$hits</span><span class="hl-code">;
</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
<p>If you want to just display the hits without counting them use the following code:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
</span><span class="hl-var">$file</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">hits.dat</span><span class="hl-quotes">&quot;</span><span class="hl-code">; </span><span class="hl-comment">// Hit count file
</span><span class="hl-var">$fp</span><span class="hl-code"> = @</span><span class="hl-identifier">fopen</span><span class="hl-brackets">(</span><span class="hl-var">$file</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">r</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">// Open hits in readonly
</span><span class="hl-var">$count</span><span class="hl-code"> = @</span><span class="hl-identifier">fread</span><span class="hl-brackets">(</span><span class="hl-var">$fp</span><span class="hl-code">, </span><span class="hl-identifier">filesize</span><span class="hl-brackets">(</span><span class="hl-var">$file</span><span class="hl-brackets">))</span><span class="hl-code">; </span><span class="hl-comment">// get hits</span><span class="hl-code">
@</span><span class="hl-identifier">fclose</span><span class="hl-brackets">(</span><span class="hl-var">$fp</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">// close file
</span><span class="hl-reserved">for</span><span class="hl-brackets">(</span><span class="hl-var">$i</span><span class="hl-code">=</span><span class="hl-number">0</span><span class="hl-code">;</span><span class="hl-var">$i</span><span class="hl-code">&lt;</span><span class="hl-identifier">strlen</span><span class="hl-brackets">(</span><span class="hl-var">$count</span><span class="hl-brackets">)</span><span class="hl-code">;</span><span class="hl-var">$i</span><span class="hl-code">++</span><span class="hl-brackets">) {
  </span><span class="hl-var">$img</span><span class="hl-code"> = </span><span class="hl-identifier">substr</span><span class="hl-brackets">(</span><span class="hl-var">$count</span><span class="hl-code">,</span><span class="hl-var">$i</span><span class="hl-code">,</span><span class="hl-number">1</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">// Get next image
  </span><span class="hl-var">$hits</span><span class="hl-code"> .= </span><span class="hl-quotes">&quot;</span><span class="hl-string">&lt;img src='images/</span><span class="hl-var">$img</span><span class="hl-string">.gif'&gt;</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
</span><span class="hl-brackets">}
</span><span class="hl-reserved">echo </span><span class="hl-var">$hits</span><span class="hl-code">;
</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
<p>Please make sure that your hits.dat file is chmoded to 666, which is all read and write permissions.</p>
<p>You can get as creative as you like with the images used in showing your hits. If you need some basic images you can use the following images:<br />
<img id="image8" src="http://wlscripting.com/wp-content/uploads/2006/07/0.gif" alt="0.gif" /><img id="image9" src="http://wlscripting.com/wp-content/uploads/2006/07/1.gif" alt="1.gif" /><img id="image10" src="http://wlscripting.com/wp-content/uploads/2006/07/2.gif" alt="2.gif" /><img id="image11" src="http://wlscripting.com/wp-content/uploads/2006/07/3.gif" alt="3.gif" /><img id="image13" src="http://wlscripting.com/wp-content/uploads/2006/07/4.gif" alt="4.gif" /><img id="image14" src="http://wlscripting.com/wp-content/uploads/2006/07/5.gif" alt="5.gif" /><img id="image15" src="http://wlscripting.com/wp-content/uploads/2006/07/6.gif" alt="6.gif" /><img id="image16" src="http://wlscripting.com/wp-content/uploads/2006/07/7.gif" alt="7.gif" /><img id="image17" src="http://wlscripting.com/wp-content/uploads/2006/07/8.gif" alt="8.gif" /><img id="image18" src="http://wlscripting.com/wp-content/uploads/2006/07/9.gif" alt="9.gif" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wlscripting.com/tutorial/19/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Flat file hit counter</title>
		<link>http://www.wlscripting.com/tutorial/7</link>
		<comments>http://www.wlscripting.com/tutorial/7#comments</comments>
		<pubDate>Fri, 14 Jul 2006 13:38:37 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[Counters]]></category>

		<guid isPermaLink="false">http://wlscripting.com/?p=7</guid>
		<description><![CDATA[Create a simple hit counter with a few lines of code and a flat file.]]></description>
			<content:encoded><![CDATA[<p>So  you need to track hits to your website but don&#8217;t want to use a database or don&#8217;t have access to a database server. You can still do this by using a flat file on your server. Copy the following code to your website where you want to show/count the hits.</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
</span><span class="hl-var">$file</span><span class="hl-code"> = </span><span class="hl-quotes">'</span><span class="hl-string">hits.dat</span><span class="hl-quotes">'</span><span class="hl-code">; </span><span class="hl-comment">// File where data is stored
</span><span class="hl-var">$fp</span><span class="hl-code"> = </span><span class="hl-identifier">fopen</span><span class="hl-brackets">(</span><span class="hl-var">$file</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">r</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;  </span><span class="hl-comment">// Open file as read only
</span><span class="hl-var">$count</span><span class="hl-code"> = </span><span class="hl-identifier">fread </span><span class="hl-brackets">(</span><span class="hl-var">$fp</span><span class="hl-code">, </span><span class="hl-identifier">filesize</span><span class="hl-brackets">(</span><span class="hl-var">$file</span><span class="hl-brackets">))</span><span class="hl-code">; </span><span class="hl-comment">// Read hits
</span><span class="hl-identifier">fclose</span><span class="hl-brackets">(</span><span class="hl-var">$fp</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">// Close file
</span><span class="hl-var">$count</span><span class="hl-code">++; </span><span class="hl-comment">// Add 1 to the count
</span><span class="hl-var">$fp</span><span class="hl-code"> = </span><span class="hl-identifier">fopen</span><span class="hl-brackets">(</span><span class="hl-var">$file</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">w</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">// Open file for writing
</span><span class="hl-identifier">fwrite</span><span class="hl-brackets">(</span><span class="hl-var">$fp</span><span class="hl-code">, </span><span class="hl-var">$count</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">// Write the count
</span><span class="hl-identifier">fclose</span><span class="hl-brackets">(</span><span class="hl-var">$fp</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">// Close file
</span><span class="hl-reserved">echo </span><span class="hl-quotes">&quot;</span><span class="hl-string">Load </span><span class="hl-var">$count</span><span class="hl-string"> times</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
<p>If you wanted to only show the code without updating the hits use the following code:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
</span><span class="hl-var">$file</span><span class="hl-code"> = </span><span class="hl-quotes">'</span><span class="hl-string">hits.dat</span><span class="hl-quotes">'</span><span class="hl-code">; </span><span class="hl-comment">// File where data is stored
</span><span class="hl-var">$fp</span><span class="hl-code"> = </span><span class="hl-identifier">fopen</span><span class="hl-brackets">(</span><span class="hl-var">$file</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">r</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;  </span><span class="hl-comment">// Open file as read only
</span><span class="hl-var">$count</span><span class="hl-code"> = </span><span class="hl-identifier">fread </span><span class="hl-brackets">(</span><span class="hl-var">$fp</span><span class="hl-code">, </span><span class="hl-identifier">filesize</span><span class="hl-brackets">(</span><span class="hl-var">$file</span><span class="hl-brackets">))</span><span class="hl-code">; </span><span class="hl-comment">// Read hits
</span><span class="hl-identifier">fclose</span><span class="hl-brackets">(</span><span class="hl-var">$fp</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">// Close file
</span><span class="hl-reserved">echo </span><span class="hl-quotes">&quot;</span><span class="hl-string">Load </span><span class="hl-var">$count</span><span class="hl-string"> times</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
<p>Please note that the hits.dat or the file in the $file variable must be chmoded to atleast 666 or be read and writable.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wlscripting.com/tutorial/7/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Create a MySQL hit counter</title>
		<link>http://www.wlscripting.com/tutorial/5</link>
		<comments>http://www.wlscripting.com/tutorial/5#comments</comments>
		<pubDate>Fri, 14 Jul 2006 13:36:57 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[Counters]]></category>

		<guid isPermaLink="false">http://wlscripting.com/?p=5</guid>
		<description><![CDATA[Create a simple PHP and MySQL hit counter.]]></description>
			<content:encoded><![CDATA[<p>So you want to keep track of your website hits in a database. Using the following code this is possible. Lets start.</p>
<p>Create a blank database on  your server. Now using a database manager like phpmyadmin, add the following sql into the database:</p>
<blockquote><p>CREATE TABLE `hit_count` (<br />
  `hits` int(50) NOT NULL default &#8216;0&#8242;<br />
) TYPE=MyISAM;</p>
<p>INSERT INTO `hit_count` VALUES (0);</p></blockquote>
<p>Next create a blank document on your computer using your favorite editor and past the following into that file:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
</span><span class="hl-comment">// DB Settings
</span><span class="hl-var">$server</span><span class="hl-code"> = </span><span class="hl-quotes">'</span><span class="hl-string">localhost</span><span class="hl-quotes">'</span><span class="hl-code">;
</span><span class="hl-var">$user</span><span class="hl-code"> = </span><span class="hl-quotes">''</span><span class="hl-code">;
</span><span class="hl-var">$pass</span><span class="hl-code"> = </span><span class="hl-quotes">''</span><span class="hl-code">;
</span><span class="hl-var">$db2</span><span class="hl-code"> = </span><span class="hl-quotes">''</span><span class="hl-code">;

</span><span class="hl-var">$db</span><span class="hl-code"> = </span><span class="hl-identifier">mysql_connect</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-var">$server</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-var">$user</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-var">$pass</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">) </span><span class="hl-reserved">or die</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Could not connect.</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-var">$db</span><span class="hl-brackets">)
</span><span class="hl-reserved">die</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">no db</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-identifier">mysql_select_db</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-var">$db2</span><span class="hl-quotes">&quot;</span><span class="hl-code">,</span><span class="hl-var">$db</span><span class="hl-brackets">))
</span><span class="hl-reserved">die</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">No database selected.</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;

</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
<p>Add your username to MySQL between the &#8216; &#8216; in the $user = &#8221;;.</p>
<p>Add your password to MySQL to the $pass and your database name to the $db2.</p>
<p>Now after:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-default">die(&quot;No database selected.&quot;);</span></pre></div></div>
<p>But before:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-default">?&gt;</span></pre></div></div>
<p>Add the following code:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-default">$query1 = mysql_query(&quot;SELECT hits FROM hit_count&quot;);// Get hit count
$total = mysql_fetch_array($query1);

$hitCount = $total['hits'] + 1;// Add 1 to the count

$sql = &quot;UPDATE `hit_count` SET `hits` = '$hitCount'&quot;; // Update Hits
mysql_query($sql) or die(&quot;Unable to process query: &quot; . mysql_error());

echo 'Hits: '.$hitCount; // Show hits</span></pre></div></div>
<p>Thats all there is to creating a hit counter and showing the results on your page.</p>
<p>If you want to show the hits on a page without updating them you can use the following code:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
</span><span class="hl-var">$total</span><span class="hl-code"> = </span><span class="hl-identifier">mysql_fetch_array</span><span class="hl-brackets">(</span><span class="hl-var">$query</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-reserved">echo </span><span class="hl-quotes">'</span><span class="hl-string">Total Hits: </span><span class="hl-quotes">'</span><span class="hl-code">.</span><span class="hl-var">$total</span><span class="hl-brackets">[</span><span class="hl-quotes">'</span><span class="hl-string">hits</span><span class="hl-quotes">'</span><span class="hl-brackets">]</span><span class="hl-code">;
</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
<p>Make sure that you connect to the database before you call the above code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wlscripting.com/tutorial/5/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
