<?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>Lost Whisper &#187; Tutorials</title>
	<atom:link href="http://lost-whisper.com/category/tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://lost-whisper.com</link>
	<description>Yet another whisper lost.</description>
	<lastBuildDate>Thu, 28 Apr 2011 18:33:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>MongoDB and PHP on Webfaction</title>
		<link>http://lost-whisper.com/2010/06/16/mongodb-and-php-on-webfaction/</link>
		<comments>http://lost-whisper.com/2010/06/16/mongodb-and-php-on-webfaction/#comments</comments>
		<pubDate>Wed, 16 Jun 2010 23:20:45 +0000</pubDate>
		<dc:creator>TwiRp</dc:creator>
				<category><![CDATA[Hosting]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[mongodb]]></category>
		<category><![CDATA[webfaction]]></category>

		<guid isPermaLink="false">http://lost-whisper.com/?p=447</guid>
		<description><![CDATA[So, this summer I decided I was going to try to learn some new things and update myself to some of the newer stuff out there. So PHP 5 added a whole slew of new features to PHP, but I&#8217;ve been stuck in PHP 4, so I figured I&#8217;d learn some of that.  I&#8217;ve also [...]]]></description>
			<content:encoded><![CDATA[<p>So, this summer I decided I was going to try to learn some new things and update myself to some of the newer stuff out there.</p>
<p>So PHP 5 added a whole slew of new features to PHP, but I&#8217;ve been stuck in PHP 4, so I figured I&#8217;d learn some of that.  I&#8217;ve also been stuck to relational databases (and that&#8217;s usually what they teach at school), but I&#8217;ve started to see some of the sites I know and love switching to non-relational databases.  So that was also something I wanted to learn how to work with.</p>
<p>I went online to search, and the three major contenders seem to be <a href="http://cassandra.apache.org/">Cassandra</a>, <a href="http://couchdb.apache.org/">CouchDB</a>, and <a href="http://www.mongodb.org/">MongoDB</a>.  I decided to go with MongoDB since it seemed like the easiest to use and setup, and I liked the idea of document-oriented storage.  CouchDB can be ran on <a href="http://www.webfaction.com?affiliate=emokid">webFaction</a> as well, but I&#8217;m not too sure about Cassandra (I&#8217;m guessing you can with effort).</p>
<p>So here&#8217;s my guide on getting it setup and ready to run.</p>
<p><span id="more-447"></span></p>
<p>So installing <a href="http://www.mongodb.org">MongoDB</a> on <a href="http://www.webfaction.com?affiliate=emokid">Webfaction</a>:</p>
<ol>
<li>Create a new custom application listening on port called mongodb</li>
<li>Create it and take note of the port</li>
<li>Login to your account via SSH</li>
<li>Switch to the directory of the mongodb app you just created<br />
<em>cd ~/webapps/mongodb</em></li>
<li>Download the latest version of the 32-bit version of MongoDB (1.4.3 as of writing)<br />
<em>wget http://downloads.mongodb.org/linux/mongodb-linux-i686-1.4.3.tgz</em></li>
<li>Extract the files from the archive<br />
<em>tar -xf mongodb-linux-i686-1.4.3.tgz</em></li>
<li>Create a data directory for your MongoDB installation<br />
<em>mkdir data</em></li>
<li><em> </em>Start up MongoDB<br />
<em>/home/[user]/webapps/mongodb/mongodb-linux-i686-1.4.3/bin/mongod &#8210;&#8210;dbpath /home/[user]/webapps/mongodb/data/ &#8210;&#8210;port [port]</em></li>
</ol>
<p>Doing that will launch MongoDB in the terminal.  To have MongoDB run as a background process and restart should it quit can be done with a few scripts.  My way isn&#8217;t probably the most elegant way to do it.  If mongoDB doesn&#8217;t exit cleanly, then you have to delete the lock, run repair, and restart the daemon, so there are 3 parts to my way and a dummy file, but you can probably easily merge it all into one file (I&#8217;m just too interested in playing with PHP and MongoDB to do so yet).</p>
<p>This is <strong>mongo.sh</strong>, it checks if the mongo daemon (mongod) is running, and it calls the appropriate action.</p>
<p style="padding-left: 30px;">
<div id="_mcePaste" style="padding-left: 30px;"><em>#!/bin/bash</em></div>
<div id="_mcePaste" style="padding-left: 30px;"><em># check if mongod is running</em></div>
<div id="_mcePaste" style="padding-left: 30px;"><em>ps -u [user] | grep -v grep | grep mongod</em></div>
<div id="_mcePaste" style="padding-left: 30px;"><em># if not found &#8211; equals to 1, start it</em></div>
<div id="_mcePaste" style="padding-left: 30px;"><em>if [ $? -eq 1 ]</em></div>
<div id="_mcePaste" style="padding-left: 30px;"><em>then</em></div>
<div id="_mcePaste" style="padding-left: 30px;"><em>/home/[user]/webapps/mongodb/startmongo.sh</em></div>
<div id="_mcePaste" style="padding-left: 30px;"><em>else</em></div>
<div id="_mcePaste" style="padding-left: 30px;"><em>/home/[user]/webapps/mongodb/runningmongo.sh</em></div>
<div id="_mcePaste" style="padding-left: 30px;"><em>fi</em></div>
<p>This is <strong>startmongo.sh</strong>, there&#8217;s a dummy file called &#8220;isnt_running.txt&#8221;  If it exists when startmongo.sh is ran, that means that mongod didn&#8217;t exit cleanly.  If it didn&#8217;t exit cleanly, we have to delete mongod.lock and run repair on the database.  Otherwise, we just try to start up mongod.</p>
<p style="padding-left: 30px;"><em>#!/usr/bin/env php<br />
&lt;?php<br />
// Check if MongoDB did not exit cleanly<br />
if(file_exists(&#8220;/home/[user]/webapps/mongodb/isnt_running.txt&#8221;)){<br />
unlink(&#8220;/home/[user]/webapps/mongodb/data/mongod.lock&#8221;);<br />
exec(&#8220;/home/[user]/webapps/mongodb/mongodb-1.4.3/bin/mongod &#8210;&#8210;dbpath /home/[user]/webapps/mongodb/data &#8210;&#8210;port [port] &#8210;&#8210;auth &#8210;&#8210;repair&#8221;);<br />
exec(&#8220;nohup /home/[user]/webapps/mongodb/mongodb-1.4.3/bin/mongod &#8210;&#8210;dbpath /home/[user]/webapps/mongodb/data &#8210;&#8210;port [port] &#8210;&#8210;auth&#8221;);<br />
}else{<br />
exec(&#8220;nohup /home/[user]/webapps/mongodb/mongodb-1.4.3/bin/mongod &#8210;&#8210;dbpath /home/emokid/webapps/mongodb/data &#8210;&#8210;port [port] &#8210;&#8210;auth&#8221;);<br />
$fp = fopen(&#8220;/home/[user]/webapps/mongodb/isnt_running.txt&#8221;, &#8220;w+&#8221;);<br />
fwrite($fp, &#8220;Starting up&#8221;);<br />
fclose($fp);<br />
}<br />
?&gt;</em></p>
<p>This is <strong>runningmongo.sh</strong>, it just deletes our dummy file letting us know that mongod is running.</p>
<p style="padding-left: 30px;"><em>#!/usr/bin/env php<br />
&lt;?php<br />
// Check if MongoDB did not exit cleanly<br />
if(file_exists(&#8220;/home/emokid/webapps/mongodb/isnt_running.txt&#8221;)){<br />
unlink(&#8220;/home/emokid/webapps/mongodb/isnt_running.txt&#8221;);<br />
}<br />
?&gt;</em></p>
<p>I just add mongo.sh to the crontab.  Mine runs every 20 minutes, but you can make it run sooner because if it does run every 20 minutes, that mean that the database could be down for 40 minutes.  Make sure to change [user] and [port] to your username and the port number you were given.</p>
<p><strong>crontab</strong></p>
<p style="padding-left: 30px;"><em>*/20 * * * * /home/[user]/webapps/mongodb/mongo.sh &gt; /dev/null 2&gt;&amp;1</em></p>
<h2>The PHP Stack</h2>
<p>In order to run PHP with the mongo drivers, you need your own installation of Apache and PHP.  WebFaction makes this somewhat easy to do.</p>
<ol>
<li>Create a new custom application listening on port and call it phpstack</li>
<li>Take note of the port.</li>
<li>SSH onto the server and go to the phpstack webapp directory<br />
<em>cd ~/webapps/phpstack</em></li>
<li>Download the PHPStack setup script (this is the one for PHP 5.3.1).<br />
<em>wget http://wiki.webfaction.com/attachment/wiki/MiscellaneousFiles/build_php_worker_531.sh?format=raw</em></li>
<li><em> </em> Rename the script to build_php_worker_531.sh<br />
<em>mv build_php_worker_531.sh\?format\=raw build_php_worker_531.sh</em></li>
<li><em> </em>Make sure the script is executable.<br />
<em>chmod +x build_php_worker_531.sh</em></li>
<li><em></em>Run the install script and specify the path to the phpstack, and the port number<br />
<em>./build_php_worker_531.sh /home/[user]/webapps/phpstack [port]</em></li>
<li>We have to install <a href="http://pear.php.net/">Pear</a> twice in order to get <a href="http://pecl.php.net/">PECL</a>.
<ol>
<li>Download the go-pear.php installer script<br />
<em>wget http://pear.php.net/go-pear </em></li>
<li>Rename it to go-pear.php<br />
<em>mv go-pear go-pear.php</em></li>
<li><em></em>Run it with the php_fcgi version of php<br />
<em>/home/[user]/webapps/phpstack/php_fcgi/bin/php go-pear.php</em></li>
<li><em></em>When it asks you about the paths, set $prefix to:<br />
<em>/home/[user]/webapps/phpstack/php_fcgi</em></li>
<li>Then run the installer.  Repeat the process for <em>php_rawcgi</em></li>
<li><em></em>For each installation (php_fcgi and php_rawcgi) run<br />
<em>/home/[user]/webapps/phpstack/php_fcgi/bin/pecl install mongo</em></li>
<li>Then in each php.ini (php_fcgi/lib/php.ini and php_rawcgi/lib/php.ini) add<br />
<em>extension=&#8221;mongo.so&#8221;</em></li>
</ol>
</li>
</ol>
<p>To keep things easy, you can place all of your website stuff inside of the phpstack/htdocs, or you can edit the httpd.conf to point towards one of your other webapps.</p>
<p>Now you might want to <a href="http://www.mongodb.org/display/DOCS/Security+and+Authentication">secure your MongoDB a bit</a>, and read up on <a href="http://php.net/manual/en/book.mongo.php">how to use it with PHP</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://lost-whisper.com/2010/06/16/mongodb-and-php-on-webfaction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Graphical Counter</title>
		<link>http://lost-whisper.com/2006/06/26/graphical-counter/</link>
		<comments>http://lost-whisper.com/2006/06/26/graphical-counter/#comments</comments>
		<pubDate>Mon, 26 Jun 2006 19:37:40 +0000</pubDate>
		<dc:creator>TwiRp</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://lost-whisper.info/2006/06/26/graphical-counter/</guid>
		<description><![CDATA[Want to make your own Graphical Counter like OKCounter?  Look at this tutorial, and will show you how to make your own graphical counter showing the number of unique hits today, and the total number of hits.]]></description>
			<content:encoded><![CDATA[<p><strong>What's Needed</strong></p>
<p>All we need is PHP and MySQL.<br />
<strong>Files We Will Make</strong></p>
<ul>
<li>count.php</li>
<li>image.php</li>
</ul>
<p>We could do it with one file, but I think it's easier to understand with 2 different files.  Plus, if you want to have different images, the count.php will log the stuff, and image.php will display the information.</p>
<p><strong>The SQL Query</strong></p>
<p>Creating the table.  We are going to try to keep it as simple as posible.</p>
<div class="igBar"><span id="lsql-1"><a href="#" onclick="javascript:showCodeTxt('sql-1'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">SQL:</span>
<div id="sql-1">
<div class="sql">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> counter<span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">logging VARCHAR<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #cc66cc;color:#800000;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">number INT<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #cc66cc;color:#800000;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ipaddr TEXT <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">daynum INT<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #cc66cc;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color:#006600; font-weight:bold;">&#40;</span>logging<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p><span class="syntax_punct syntax_punct_queryend"><br />
</span></p>
<p>This creates the table we will use.  logging will store if it's storing information for today or total hits, and the ipaddr will store the IP of the visitors, number is the number of hits, and daynum is the number of the day.<br />
Now we need to insert some more information in the database.</p>
<div class="igBar"><span id="lsql-2"><a href="#" onclick="javascript:showCodeTxt('sql-2'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">SQL:</span>
<div id="sql-2">
<div class="sql">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> counter <span style="color:#006600; font-weight:bold;">&#40;</span>logging,number<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #ff0000;">'today'</span>,<span style="color: #ff0000;">'0'</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<div class="igBar"><span id="lsql-3"><a href="#" onclick="javascript:showCodeTxt('sql-3'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">SQL:</span>
<div id="sql-3">
<div class="sql">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> counter <span style="color:#006600; font-weight:bold;">&#40;</span>logging,number<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #ff0000;">'total'</span>,<span style="color: #ff0000;">'0'</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
This just creates the 2 storage location in the table we will use.</p>
<p><strong>Count.php</strong></p>
<p>Now we need the file that will store the information.  Hopefully you understand the internal documentation.</p>
<div class="igBar"><span id="lphp-4"><a href="#" onclick="javascript:showCodeTxt('php-4'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-4">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Database Information, Host, User, Password, and Database</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$DBHost</span> = <span style="color:#FF0000;">'localhost'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$DBUser</span> = <span style="color:#FF0000;">'user'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$DBPass</span> = <span style="color:#FF0000;">'pass'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$DB</span> = <span style="color:#FF0000;">'database'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Now to connect</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/mysql_connect"><span style="color:#000066;">mysql_connect</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$DBHost</span>, <span style="color:#0000FF;">$DBUser</span>, <span style="color:#0000FF;">$DBPass</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/mysql_select_db"><span style="color:#000066;">mysql_select_db</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$DB</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Select today's information</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$q</span> = <a href="http://www.php.net/mysql_query"><span style="color:#000066;">mysql_query</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"SELECT * FROM counter WHERE logging='today'"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$i</span> = <a href="http://www.php.net/mysql_fetch_array"><span style="color:#000066;">mysql_fetch_array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$q</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Get the total inforamtion</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$tq</span> = <a href="http://www.php.net/mysql_query"><span style="color:#000066;">mysql_query</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"SELECT * FROM coutner WHERE logging='total'"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$ti</span> = <a href="http://www.php.net/mysql_fetch_array"><span style="color:#000066;">mysql_fetch_array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$tq</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$total</span> = <span style="color:#0000FF;">$ti</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'number'</span><span style="color:#006600; font-weight:bold;">&#93;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Check to see if it's storing todays information</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$date</span> = <a href="http://www.php.net/date"><span style="color:#000066;">date</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"d"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$i</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'daynum'</span><span style="color:#006600; font-weight:bold;">&#93;</span>==<span style="color:#0000FF;">$date</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Since it is, we get the current information</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$today</span> = <span style="color:#0000FF;">$i</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'number'</span><span style="color:#006600; font-weight:bold;">&#93;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$ips</span> = <a href="http://www.php.net/explode"><span style="color:#000066;">explode</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"|"</span>,<span style="color:#0000FF;">$i</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'ipaddr'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Now to see if we need to increment the counter.</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span>!<a href="http://www.php.net/in_array"><span style="color:#000066;">in_array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_SERVER</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'REMOTE_ADDR'</span><span style="color:#006600; font-weight:bold;">&#93;</span>,<span style="color:#0000FF;">$ips</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$today</span>++;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$total</span>++;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Add the IP to the IP List</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$nip</span> = <span style="color:#0000FF;">$i</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'ipaddr'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#FF0000;">'|'</span>.<span style="color:#0000FF;">$_SERVER</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'REMOTE_ADDR'</span><span style="color:#006600; font-weight:bold;">&#93;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Update the Table</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$up</span> = <a href="http://www.php.net/mysql_query"><span style="color:#000066;">mysql_query</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"UPDATE counter SET number='"</span>.<span style="color:#0000FF;">$today</span>.<span style="color:#FF0000;">"', ipaddr='"</span>.<span style="color:#0000FF;">$nip</span>.<span style="color:#FF0000;">"' WHERE logging='today'"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$up2</span> = <a href="http://www.php.net/mysql_query"><span style="color:#000066;">mysql_query</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"UPDATE counter SET number='"</span>.<span style="color:#0000FF;">$total</span>.<span style="color:#FF0000;">"' WHERE logging='total'"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#616100;">else</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// It's a new DAY!...?</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// So update the list, how many hits today, and the total.</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$nip</span> = <span style="color:#0000FF;">$_SERVER</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'REMOTE_ADDR'</span><span style="color:#006600; font-weight:bold;">&#93;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$today</span> = <span style="color:#CC66CC;color:#800000;">1</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$total</span>++;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$up</span> = <a href="http://www.php.net/mysql_query"><span style="color:#000066;">mysql_query</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"UPDATE counter SET number='"</span>.<span style="color:#0000FF;">$today</span>.<span style="color:#FF0000;">"', ipaddr='"</span>.<span style="color:#0000FF;">$nip</span>.<span style="color:#FF0000;">"', daynum='"</span>.<span style="color:#0000FF;">$date</span>.<span style="color:#FF0000;">"' WHERE logging='today'"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$up2</span> = <a href="http://www.php.net/mysql_query"><span style="color:#000066;">mysql_query</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"UPDATE counter SET number='"</span>.<span style="color:#0000FF;">$total</span>.<span style="color:#FF0000;">"' WHERE logging='total'"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// End the count.php</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Today and Total are set and done. </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>So now the $today and $total variables are set.  We will need those when we include count.php in image.php</p>
<p><strong>The Image</strong></p>
<p>So, first off, we need to pick an image.</p>
<p>I chose <img src="http://lost-whisper.info/blue_orange.png" /> not just because I didn't feel like making a new one for the tutorial, but becuase of it's coloration...?</p>
<p>So anyways, we open it up in our favorite image editor, I picked MS Paint...</p>
<p>Put the cursor where you want the text to be (the numbers or whatever).  When I did it, the location was 30, 5.  But then after coding it, you may have to change the number a bit.  I finished with 30, 3.</p>
<p><strong>Image.php</strong></p>
<p>Image.php is rather easy to make.  We have count.php and the numbers for positioning the text.</p>
<div class="igBar"><span id="lphp-5"><a href="#" onclick="javascript:showCodeTxt('php-5'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-5">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Include count.php to log and set the variables</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">include</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"count.php"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Set our header, mine was a png, so i set image/png</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// jpeg/jpg - image/jpeg</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// gif - image/gif</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/header"><span style="color:#000066;">header</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"Content-type: image/png"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Create our image from our image...</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// change png to whatever the image type is.</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$image</span> = imagecreatefrompng<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"blue_orange.png"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Pick a color, 225 is the red, 154 is green, and 10 is blue</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$text</span> = imagecolorallocate<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$image</span>,<span style="color:#CC66CC;color:#800000;">255</span>, <span style="color:#CC66CC;color:#800000;">154</span>, <span style="color:#CC66CC;color:#800000;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Put a string, notice the 30, 3.</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$write</span> = imagestring<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$image</span>, <span style="color:#CC66CC;color:#800000;">1</span>, <span style="color:#CC66CC;color:#800000;">30</span>,<span style="color:#CC66CC;color:#800000;">3</span>, <span style="color:#0000FF;">$today</span>.<span style="color:#FF0000;">"/"</span>.<span style="color:#0000FF;">$total</span>,<span style="color:#0000FF;">$text</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Output the image, mine is png so yeah...</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">imagepng<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$image</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>And we wind up with:<br />
<img src="http://lost-whisper.info/blue_orange.php" /></p>
]]></content:encoded>
			<wfw:commentRss>http://lost-whisper.com/2006/06/26/graphical-counter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

