<?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>WEB-NES-BAY &#187; Environment Variables</title>
	<atom:link href="http://webnesbay.com/tag/environment-variables/feed/" rel="self" type="application/rss+xml" />
	<link>http://webnesbay.com</link>
	<description>Learn Tips and tricks on Linux, Hacking, linux, PHP, Perl, Web, Hardware</description>
	<lastBuildDate>Sun, 11 Apr 2010 05:12:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Track website visitors using PHP</title>
		<link>http://webnesbay.com/track-website-visitors-using-php/</link>
		<comments>http://webnesbay.com/track-website-visitors-using-php/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 08:55:59 +0000</pubDate>
		<dc:creator>WEBNESBAY</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Blogs]]></category>
		<category><![CDATA[Datetime]]></category>
		<category><![CDATA[Environment Variables]]></category>
		<category><![CDATA[Free Ones]]></category>
		<category><![CDATA[Graphs And Charts]]></category>
		<category><![CDATA[Host Server]]></category>
		<category><![CDATA[Ipaddress]]></category>
		<category><![CDATA[Logline]]></category>
		<category><![CDATA[Php Fopen]]></category>
		<category><![CDATA[Php Function]]></category>
		<category><![CDATA[Php Functions]]></category>
		<category><![CDATA[Php Manual]]></category>
		<category><![CDATA[Php Self]]></category>
		<category><![CDATA[php site tracker]]></category>
		<category><![CDATA[Price Tag]]></category>
		<category><![CDATA[Query String]]></category>
		<category><![CDATA[Scripting Language]]></category>
		<category><![CDATA[Server Php]]></category>
		<category><![CDATA[Server Query]]></category>
		<category><![CDATA[String Server]]></category>
		<category><![CDATA[track website]]></category>
		<category><![CDATA[tracker howto]]></category>
		<category><![CDATA[Wesites]]></category>

		<guid isPermaLink="false">http://webnesbay.com/?p=897</guid>
		<description><![CDATA[We have so many tools available to track the blogs or wesites. Although there are some free ones, most of them come with a price tag. Why not do it yourself? With PHP, you can easily create a log file within minutes. This article shows you the complete tutorial on how to make the tracker
Getting the information
The most important part is getting the information from your visitor. Thankfully, this is extremely easy to do in PHP (or any other scripting [...]


Related posts:<ol><li><a href='http://webnesbay.com/passing-a-variable-from-javascript-to-php/' rel='bookmark' title='Permanent Link: Passing a variable from Javascript to PHP'>Passing a variable from Javascript to PHP</a></li>
<li><a href='http://webnesbay.com/special-shortcuts-in-perl/' rel='bookmark' title='Permanent Link: Special shortcuts in Perl'>Special shortcuts in Perl</a></li>
<li><a href='http://webnesbay.com/captalization-of-strings-in-php/' rel='bookmark' title='Permanent Link: Captalization of strings in PHP'>Captalization of strings in PHP</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>We have so many tools available to track the blogs or wesites. Although there are some free ones, most of them come with a price tag. Why not do it yourself? With PHP, you can easily create a log file within minutes. This article shows you the complete tutorial on how to make the tracker</p>
<p><strong>Getting the information</strong><br />
The most important part is getting the information from your visitor. Thankfully, this is extremely easy to do in PHP (or any other scripting language for that matter). PHP has a special global variable called $_SERVER which contains several <a id="KonaLink1" style="text-decoration: underline ! important; position: static;" href="http://www.articlesfactory.com/articles/cgi/track-your-visitors-using-php.html#" target="undefined"><span style="color: #990000 ! important; font-weight: 400; font-size: 12px; position: static;"><span style="color: #990000 ! important; font-family: Verdana,Arial; font-weight: 400; font-size: 12px; position: static;">environment </span><span style="color: #990000 ! important; font-family: Verdana,Arial; font-weight: 400; font-size: 12px; position: static;">variables</span></span></a>, including information about your visitor. To get all the information you want, simply use the following code:</p>
<pre><span style="color: #0000ff;">// Getting the information
$ipaddress = $_SERVER['REMOTE_ADDR'];
$page = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}";
$page .= iif(!empty($_SERVER['QUERY_STRING']), "?{$_SERVER['QUERY_STRING']}", "");
$referrer = $_SERVER['HTTP_REFERER'];
$datetime = mktime();
$useragent = $_SERVER['HTTP_USER_AGENT'];
$remotehost = @getHostByAddr($ipaddress);</span></pre>
<p>As you can see the majority of information comes from the $_SERVER variable. The mktime() (http://nl2.php.net/mktime) and getHostByAddr() (http://nl2.php.net/manual/en/function.gethostbyaddr.php) functions are used to get additional information about the visitor.</p>
<p>Note: I used a function in the above example called iif(). You can get this function at <a href="http://www.phpit.net/code/iif-function">http://www.phpit.net/code/iif-function</a>.</p>
<p><strong>Logging the information</strong><br />
Now that you have all the information you need, it must be written to a log file so you can later look at it, and create useful graphs and charts. To do this you need a few simple PHP function, like fopen (http://www.php.net/fopen) and fwrite (http://www.php.net/fwrite).</p>
<p>The below code will first create a complete line out of all the information. Then it will open the log file in &#8220;Append&#8221; mode, and if it doesn&#8217;t exist yet, create it.</p>
<p>If no errors have occurred, it will write the new logline to the log file, at the bottom, and finally close the log file again.</p>
<pre><span style="color: #0000ff;">
// Create log line
$logline = $ipaddress . '|' . $referrer . '|' . $datetime . '|' . $useragent . '|' . $remotehost . '|' . $page . "
";

// Write to log file:
$logfile = '/some/path/to/your/logfile.txt';

// Open the log file in "Append" mode
if (!$handle = fopen($logfile, 'a+')) {
die("Failed to open log file");
}

// Write $logline to our logfile.
if (fwrite($handle, $logline) === FALSE) {
die("Failed to write to log file");
}

fclose($handle);
</span></pre>
<p>Now you&#8217;ve got a fully function logging module. To start tracking visitors on your <a id="KonaLink2" style="text-decoration: underline ! important; position: static;" href="http://www.articlesfactory.com/articles/cgi/track-your-visitors-using-php.html#" target="undefined"><span style="color: #990000 ! important; font-weight: 400; font-size: 12px; position: static;"><span style="color: #990000 ! important; font-family: Verdana,Arial; font-weight: 400; font-size: 12px; position: static;">website</span></span></a> simply include the logging module into your pages with the include() function (http://www.php.net/include):</p>
<pre><span style="color: #0000ff;">include ('log.php');</span></pre>
<p><strong>Okay, now I want to view my log file</strong><br />
After a while you&#8217;ll probably want to view your log file. You can easily do so by simply using a standard <a id="KonaLink3" style="text-decoration: underline ! important; position: static;" href="http://www.articlesfactory.com/articles/cgi/track-your-visitors-using-php.html#" target="undefined"><span style="color: #990000 ! important; font-weight: 400; font-size: 12px; position: static;"><span style="color: #990000 ! important; font-family: Verdana,Arial; font-weight: 400; font-size: 12px; position: static;">text </span><span style="color: #990000 ! important; font-family: Verdana,Arial; font-weight: 400; font-size: 12px; position: static;">editor</span></span></a> (like Notepad on Windows) to open the log file, but this is far from desired, because it&#8217;s in a hard-to-read format.</p>
<p>Let&#8217;s use PHP to generate useful overviews for is. The first thing that needs to be done is get the contents from the log file in a variable, like so:</p>
<pre><span style="color: #0000ff;">
// Open log file
$logfile = "G:projectsphpitcontentrack your visitors using phplog.txt";

if (file_exists($logfile)) {

$handle = fopen($logfile, "r");
$log = fread($handle, filesize($logfile));
fclose($handle);
} else {
die ("The log file doesn't exist!");
}</span></pre>
<p>Now that the log file is in a variable, it&#8217;s best if each logline is in a separate variable. We can do this using the explode() function (http://www.php.net/explode), like so:</p>
<pre><span style="color: #0000ff;">
// Seperate each logline
$log = explode("
", trim($log));
</span></pre>
<p>After that it may be useful to get each part of each logline in a separate variable. This can be done by looping through each logline, and using explode again:</p>
<pre><span style="color: #0000ff;">
// Seperate each part in each logline
for ($i = 0; $i &lt; count($log); $i++) {
$log[$i] = trim($log[$i]);
$log[$i] = explode('|', $log[$i]);
}
</span></pre>
<p>Now the complete log file has been parsed, and we&#8217;re ready to start generating some interesting stuff. The first thing that is very easy to do is getting the number of pageviews. Simply use count() (http://www.phpit.net/count) on the $log array, and there you have it;</p>
<pre><span style="color: #0000ff;">echo count($log) . " people have visited this website.";</span></pre>
<p>You can also generate a complete overview of your log file, using a simple foreach loop and tables. For example:</p>
<pre><span style="color: #0000ff;">// Show a table of the logfile
echo '&lt;table&gt;';
echo '&lt;th&gt;IP Address&lt;/th&gt;';
echo '&lt;th&gt;Referrer&lt;/th&gt;';
echo '&lt;th&gt;Date&lt;/th&gt;';
echo '&lt;th&gt;Useragent&lt;/th&gt;';
echo '&lt;th&gt;Remote Host&lt;/th&gt;';

foreach ($log as $logline) {
echo '&lt;tr&gt;';

echo '&lt;td&gt;' . $logline['0'] . '&lt;/td&gt;';
echo '&lt;td&gt;' . urldecode($logline['1']) . '&lt;/td&gt;';
echo '&lt;td&gt;' . date('d/m/Y', $logline['2']) . '&lt;/td&gt;';
echo '&lt;td&gt;' . $logline['3'] . '&lt;/td&gt;';
echo '&lt;td&gt;' . $logline['4'] . '&lt;/td&gt;';

echo '&lt;/tr&gt;';

}

echo '&lt;/table&gt;';
</span></pre>
<p>You can also use custom functions to filter out <a id="KonaLink4" style="text-decoration: underline ! important; position: static;" href="http://www.articlesfactory.com/articles/cgi/track-your-visitors-using-php.html#" target="undefined"><span style="color: #990000 ! important; font-weight: 400; font-size: 12px; position: static;"><span style="color: #990000 ! important; font-family: Verdana,Arial; font-weight: 400; font-size: 12px; position: static;">search </span><span style="color: #990000 ! important; font-family: Verdana,Arial; font-weight: 400; font-size: 12px; position: static;">engines</span></span></a> and crawlers. Or create graphs using PHP/SWF Charts (http://www.maani.us/charts/index.php). The possibilities are endless, and you can do all kinds of things!</p>
<p><strong>In Conclusion&#8230;</strong><br />
In this article I have shown you have to create a logging module for your own PHP website, using nothing more than PHP and its built-in functions. To view the log file you need to parse it using PHP, and then display it in whatever way you like. It is up to you to create a kick-ass traffic analyzer.</p>
<p>If you still prefer to use a pre-built traffic analyzer<a href="http://www.articlesfactory.com/"><img src="http://www.articlesfactory.com/pic/x.gif" border="0" alt="Article Search" /></a>, have a look at http://www.hotscripts.com.</p>
<p>Article Tags:          <a href="http://www.articlesfactory.com/search/Visitors%20Using/">Visitors Using</a>,        <a href="http://www.articlesfactory.com/search/Logging%20Module/">Logging Module</a>,        <a href="http://www.articlesfactory.com/search/Each%20Logline/">Each Logline</a>,        <a href="http://www.articlesfactory.com/search/Lt;/td%3E%27;echo%20Lt;td%3E/">Lt;/td&gt;&#8217;;echo Lt;td&gt;</a></p>
<p><!-- google_ad_section_end -->Source: <a title="Free Articles" href="http://www.articlesfactory.com/articles/cgi/track-your-visitors-using-php.html">Free Articles</a> from ArticlesFactory.com</p>


<p>Related posts:<ol><li><a href='http://webnesbay.com/passing-a-variable-from-javascript-to-php/' rel='bookmark' title='Permanent Link: Passing a variable from Javascript to PHP'>Passing a variable from Javascript to PHP</a></li>
<li><a href='http://webnesbay.com/special-shortcuts-in-perl/' rel='bookmark' title='Permanent Link: Special shortcuts in Perl'>Special shortcuts in Perl</a></li>
<li><a href='http://webnesbay.com/captalization-of-strings-in-php/' rel='bookmark' title='Permanent Link: Captalization of strings in PHP'>Captalization of strings in PHP</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://webnesbay.com/track-website-visitors-using-php/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
