<?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>MaggsWeb:7 &#187; log</title>
	<atom:link href="http://www.maggsweb.com/tag/log/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.maggsweb.com</link>
	<description>www.maggsweb.com</description>
	<lastBuildDate>Sat, 21 Aug 2010 20:50:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Logging user activity</title>
		<link>http://www.maggsweb.com/php/logging-user-activity/</link>
		<comments>http://www.maggsweb.com/php/logging-user-activity/#comments</comments>
		<pubDate>Wed, 22 Aug 2007 09:33:15 +0000</pubDate>
		<dc:creator>chris</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[log]]></category>

		<guid isPermaLink="false">http://www.cmaggs.co.uk/?p=25</guid>
		<description><![CDATA[Logging user activity is vital when there are multiple authors for a site. It also helps with debugging user activity when there is problems. You certainly don&#8217;t want to be putting this into a database, so the next best thing is a nice little text file. I have used this to log user activity in [...]]]></description>
			<content:encoded><![CDATA[<p>Logging user activity is vital when there are multiple authors for a site.  It also helps with debugging user activity when there is problems.  You certainly don&#8217;t want to be putting this into a database, so the next best thing is a nice little text file.<br />
I have used this to log user activity in a number of sites, as it can easily be added after development.  In this example, $PAGE and $pageTitle are set in each page as they are used to determine menu selections, etc.. but these can easily be set manually.<br />
<span id="more-25"></span></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" onclick="pageTracker._trackPageview('/outgoing/www.ericbess.com/ericblog/2008/03/03/wp-codebox/_examples?referer=');"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p25code2'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p252"><td class="code" id="p25code2"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> add2log<span style="color: #009900;">&#40;</span><span style="color: #000088;">$var1</span><span style="color: #339933;">,</span><span style="color: #000088;">$var2</span><span style="color: #339933;">,</span><span style="color: #000088;">$var3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$PAGE</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$PageTitle</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;"># create array
</span>    <span style="color: #000088;">$logArray</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array" onclick="pageTracker._trackPageview('/outgoing/www.php.net/array?referer=');"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$logArray</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'loginname'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$logArray</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'accesslevel'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$logArray</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REMOTE_ADDR'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$logArray</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/date" onclick="pageTracker._trackPageview('/outgoing/www.php.net/date?referer=');"><span style="color: #990000;">date</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Y-m-d H:i:s&quot;</span><span style="color: #339933;">,</span><a href="http://www.php.net/time" onclick="pageTracker._trackPageview('/outgoing/www.php.net/time?referer=');"><span style="color: #990000;">time</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">3600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$logArray</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$PAGE</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$logArray</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$PageTitle</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$logArray</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$var1</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$logArray</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$var2</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$logArray</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$var3</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;"># create csv from array
</span>    <span style="color: #000088;">$logDetails</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">.</span><a href="http://www.php.net/implode" onclick="pageTracker._trackPageview('/outgoing/www.php.net/implode?referer=');"><span style="color: #990000;">implode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$logArray</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;"># sort dir
</span>    <span style="color: #000088;">$dir</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'DOCUMENT_ROOT'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/../_logs/&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">//if (!is_dir($dir)) mkdir($dir,0777);</span>
    <span style="color: #666666; font-style: italic;">#make filename
</span>    <span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dir</span><span style="color: #339933;">.</span><a href="http://www.php.net/date" onclick="pageTracker._trackPageview('/outgoing/www.php.net/date?referer=');"><span style="color: #990000;">date</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Y-m-d&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/time" onclick="pageTracker._trackPageview('/outgoing/www.php.net/time?referer=');"><span style="color: #990000;">time</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">3600</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.htm&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">#write to file
</span>    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$file_handle</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/fopen" onclick="pageTracker._trackPageview('/outgoing/www.php.net/fopen?referer=');"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;a&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 	<span style="color: #000088;">$ERROR</span><span style="color: #339933;">;</span> <a href="http://www.php.net/exit" onclick="pageTracker._trackPageview('/outgoing/www.php.net/exit?referer=');"><span style="color: #990000;">exit</span></a><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/fwrite" onclick="pageTracker._trackPageview('/outgoing/www.php.net/fwrite?referer=');"><span style="color: #990000;">fwrite</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file_handle</span><span style="color: #339933;">,</span> <span style="color: #000088;">$logDetails</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 	<span style="color: #000088;">$ERROR</span><span style="color: #339933;">;</span> <a href="http://www.php.net/exit" onclick="pageTracker._trackPageview('/outgoing/www.php.net/exit?referer=');"><span style="color: #990000;">exit</span></a><span style="color: #339933;">;</span>
    <a href="http://www.php.net/fclose" onclick="pageTracker._trackPageview('/outgoing/www.php.net/fclose?referer=');"><span style="color: #990000;">fclose</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file_handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This creates nice little log files for each day, that can be displayed, exported, imported, searched, etc&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.maggsweb.com/php/logging-user-activity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
