<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>C#est</title>
	<atom:link href="http://csharpest.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://csharpest.wordpress.com</link>
	<description>Just another C# and ASP.NET blog</description>
	<lastBuildDate>Tue, 03 Mar 2009 18:46:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='csharpest.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>C#est</title>
		<link>http://csharpest.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://csharpest.wordpress.com/osd.xml" title="C#est" />
	<atom:link rel='hub' href='http://csharpest.wordpress.com/?pushpress=hub'/>
		<item>
		<title>ASP.NET C# Page Counter using IP Address</title>
		<link>http://csharpest.wordpress.com/2009/03/03/aspnet-c-page-counter-using-ip-address/</link>
		<comments>http://csharpest.wordpress.com/2009/03/03/aspnet-c-page-counter-using-ip-address/#comments</comments>
		<pubDate>Tue, 03 Mar 2009 03:02:00 +0000</pubDate>
		<dc:creator>vanbruiser</dc:creator>
				<category><![CDATA[C# ASP.NET Tips]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C# and ASP.NET]]></category>
		<category><![CDATA[C# Tips]]></category>
		<category><![CDATA[C#est]]></category>
		<category><![CDATA[CSharpest]]></category>
		<category><![CDATA[IP Address]]></category>
		<category><![CDATA[Page Counter]]></category>

		<guid isPermaLink="false">http://csharpest.wordpress.com/?p=3</guid>
		<description><![CDATA[Building a page counter using the users ipAddress and a database table<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharpest.wordpress.com&amp;blog=6800278&amp;post=3&amp;subd=csharpest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>On my site, <a title="BriefingWire.com" href="http://www.briefingwire.com" target="_blank">BriefingWire.com</a>, I list user submitted press releases and I needed to count each time a press release was viewed, uniquely. Instead of using Cookies which can be manipulated by turning them off and then refreshing the page over and over, I insert a record in the DB associating the press release ID (prID) with IP Address (ipAddress). This ensures that noone can manipulate the number of times a page is viewed (numViews).</p>
<p>I use two pages to do this: <br />
1) listPR &#8211; which lists headlines of all press releases<br />
2) viewPR &#8211; which displays all the data for the selected press releases</p>
<p><strong><span style="color:#800000;">listPR Logic:<br />
</span></strong>When a visitor clicks a press release in listPR.aspx, she is directed to viewPR.aspx</p>
<p><strong><span style="color:#800000;">viewPR Logic:</span><br />
</strong>1) Make a call to DB and SELECT all data from tblPressReleases for selected press release, including numViews<br />
2) getIpAddress() of user:</p>
<p><span style="font-size:x-small;color:#0000ff;"><span style="color:#008000;">//BEGIN</span><br />
public</span><span style="font-size:x-small;"> </span><span style="font-size:x-small;color:#0000ff;">static</span><span style="font-size:x-small;"> </span><span style="font-size:x-small;color:#0000ff;">string</span><span style="font-size:x-small;"> getIPAddress()<br />
{<br />
</span><span style="font-size:x-small;color:#0000ff;">    string</span><span style="font-size:x-small;"> strIPAddress = </span><span style="font-size:x-small;">Request.ServerVariables[</span><span style="font-size:x-small;color:#a31515;">"HTTP_X_FORWARDED_FOR"</span><span style="font-size:x-small;">];<br />
    </span><span style="font-size:x-small;color:#0000ff;">if</span><span style="font-size:x-small;"> (strIPAddress == </span><span style="font-size:x-small;color:#0000ff;">null</span><span style="font-size:x-small;">)<br />
    {<br />
        <span style="font-size:x-small;">strIPAddress = </span></span><span style="font-size:x-small;">Request.ServerVariables[</span><span style="font-size:x-small;color:#a31515;">"REMOTE_ADDR"</span><span style="font-size:x-small;">];<br />
    }<br />
</span><span style="font-size:x-small;color:#0000ff;">    return<br />
    </span><span style="font-size:x-small;">strIPAddress;<br />
}<br />
<span style="color:#008000;">//END</span></span></p>
<p>3) SELECT FROM tblIPTracker (which has two fields: prID and ipAddress) to see if current user&#8217;s IPAddress is associated with this pressReleaseID<br />
4) If it is then do nothing; no increment<br />
5) If it is not then INSERT prID and ipAddress into ipTracker<br />
6) UPDATE PressRelease table with numViews++</p>
<p><span style="color:#800000;"><strong>Note:</strong></span><br />
If you get an error with the Request.ServerVariables["..."] then use the full path:<br />
<span style="font-size:x-small;">System.Web.<span style="font-size:x-small;color:#2b91af;">HttpContext</span></span><span style="font-size:x-small;">.Current.Request.ServerVariables["..."]</span></p>
<p>Hope this is helpful,<br />
Dennis<br />
<a title="BriefingWire.com" href="http://www.briefingwire.com" target="_blank">BriefingWire.com</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/csharpest.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/csharpest.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/csharpest.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/csharpest.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/csharpest.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/csharpest.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/csharpest.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/csharpest.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/csharpest.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/csharpest.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/csharpest.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/csharpest.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/csharpest.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/csharpest.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharpest.wordpress.com&amp;blog=6800278&amp;post=3&amp;subd=csharpest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://csharpest.wordpress.com/2009/03/03/aspnet-c-page-counter-using-ip-address/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f1d0a429fa888e4ac6ddad3bcee42d22?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vanbruiser</media:title>
		</media:content>
	</item>
	</channel>
</rss>
