<?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>Building Browsergames &#187; tutorial</title>
	<atom:link href="http://buildingbrowsergames.com/category/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://buildingbrowsergames.com</link>
	<description>Ever wanted to build a browsergame?</description>
	<lastBuildDate>Mon, 29 Mar 2010 14:00:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Getting Started With MongoDB (Python)</title>
		<link>http://buildingbrowsergames.com/2010/02/08/getting-started-with-mongodb-python/</link>
		<comments>http://buildingbrowsergames.com/2010/02/08/getting-started-with-mongodb-python/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 14:00:23 +0000</pubDate>
		<dc:creator>Luke</dc:creator>
				<category><![CDATA[tutorial]]></category>
		<category><![CDATA[mongodb]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/2010/02/08/getting-started-with-mongodb-python/</guid>
		<description><![CDATA[Now that I&#8217;ve extolled the virtues of using a NoSQL database, it&#8217;s time to put my money where my mouth is and show you how to use one.
To start things off, we&#8217;re going to build a super-simple site using the excellent Bottle Python framework, which is built for building simple sites like this.
This tutorial assumes [...]]]></description>
			<content:encoded><![CDATA[<p>Now that I&#8217;ve <a href='http://buildingbrowsergames.com/2010/02/01/you-should-try-a-nosql-database-today/'>extolled the virtues</a> of using a NoSQL database, it&#8217;s time to put my money where my mouth is and show you how to <strong>use</strong> one.</p>
<p>To start things off, we&#8217;re going to build a super-simple site using the excellent <a href='http://bottle.paws.de/'>Bottle</a> Python framework, which is built for building simple sites like this.</p>
<p>This tutorial assumes that you already have a mongoDB instance up and running somewhere; if you haven&#8217;t done that already, take a look at <a href='http://www.mongodb.org/display/DOCS/Quickstart'>the quickstart on mongodb.org</a>.</p>
<p>First, <a href='http://bottle.paws.de/#download-install'>download and install Bottle</a> (it&#8217;s pretty easy). Once that&#8217;s done, we can start working on building our mini project.</p>
<p>Today we&#8217;re going to build a small &#8216;army manager&#8217; site; users will be able to add, edit, and delete members of their army, and everything we need to track for the user will be stored in our mongodb database.</p>
<p>To start off, we&#8217;ll just make the index page return a template:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> bottle, <span style="color: #dc143c;">random</span>
<span style="color: #ff7700;font-weight:bold;">from</span> bottle <span style="color: #ff7700;font-weight:bold;">import</span> route, jinja2_view <span style="color: #ff7700;font-weight:bold;">as</span> view, debug, run
&nbsp;
DATABASE_HOST = <span style="color: #483d8b;">'localhost'</span>
DATABASE_NAME = <span style="color: #483d8b;">'army'</span>
DATABASE_PORT = <span style="color: #ff4500;">27017</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> pymongo
<span style="color: #ff7700;font-weight:bold;">from</span> pymongo <span style="color: #ff7700;font-weight:bold;">import</span> Connection
connection = Connection<span style="color: black;">&#40;</span>DATABASE_HOST, DATABASE_PORT<span style="color: black;">&#41;</span>
db = connection<span style="color: black;">&#91;</span>DATABASE_NAME<span style="color: black;">&#93;</span>
users = db.<span style="color: black;">users</span>
&nbsp;
SOLDIER_NAMES = <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;Moe&quot;</span>, <span style="color: #483d8b;">&quot;Curly&quot;</span>, <span style="color: #483d8b;">&quot;Janne&quot;</span>, <span style="color: #483d8b;">&quot;Peter&quot;</span>, <span style="color: #483d8b;">&quot;Paul&quot;</span>, <span style="color: #483d8b;">&quot;Kyle&quot;</span>, <span style="color: #483d8b;">&quot;Chris&quot;</span>, <span style="color: #483d8b;">&quot;Jude&quot;</span>, <span style="color: #483d8b;">&quot;Larry&quot;</span>, <span style="color: #483d8b;">&quot;Gus&quot;</span><span style="color: black;">&#93;</span>
&nbsp;
@route<span style="color: black;">&#40;</span><span style="color: #483d8b;">'/'</span><span style="color: black;">&#41;</span>
@view<span style="color: black;">&#40;</span><span style="color: #483d8b;">'templates/index.html'</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">def</span> index<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    current_user = get_current_user<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#123;</span>
        <span style="color: #483d8b;">'user'</span>: current_user,
    <span style="color: black;">&#125;</span>
<span style="color: #ff7700;font-weight:bold;">def</span> get_current_user<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    ip = request.<span style="color: black;">environ</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'REMOTE_ADDR'</span><span style="color: black;">&#93;</span>
    current_user = users.<span style="color: black;">find_one</span><span style="color: black;">&#40;</span><span style="color: black;">&#123;</span><span style="color: #483d8b;">'ip'</span>: ip<span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> current_user:
        users.<span style="color: black;">insert</span><span style="color: black;">&#40;</span><span style="color: black;">&#123;</span>
            <span style="color: #483d8b;">'ip'</span>: ip,
            <span style="color: #483d8b;">'army'</span>: <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>,
        <span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>
        current_user = users.<span style="color: black;">find_one</span><span style="color: black;">&#40;</span><span style="color: black;">&#123;</span><span style="color: #483d8b;">'ip'</span>: ip<span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> current_user    
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    debug<span style="color: black;">&#40;</span><span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    run<span style="color: black;">&#40;</span>host=<span style="color: #483d8b;">'localhost'</span>, port=<span style="color: #ff4500;">8000</span>, reloader=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>You may have noticed that instead of using the default template renderer, I used the <a href='http://jinja.pocoo.org/2/'>Jinja</a> renderer instead &#8211; which templating engine you use is up to you, but I prefer Jinja.</p>
<p>There&#8217;s a lot going on here &#8211; and if you&#8217;re new to Python, this code probably looks pretty scary. However, it&#8217;s pretty simple once you get the hang of it &#8211; here&#8217;s what we&#8217;re doing:</p>
<ul>
<li>
<p>First, we&#8217;re importing all of the libraries we need, and setting up some of our configuration. This could be split out into another file easily enough &#8211; but for the sake of this tutorial, we&#8217;re going to keep everything self-contained for now. The libraries and helpers we&#8217;ve imported are <em>random</em>, <em>bottle.route</em>, <em>bottle.run</em>, <em>bottle.request</em>, <em>bottle.jinja2_view as view</em>, <em>bottle.response</em>, <em>bottle.debug</em>, <em>bottle.redirect</em>, <em>pymongo</em>, and <em>pymongo.Connection</em></p>
<p>Most of these come from the <a href='http://bottle.paws.de/'>Bottle</a> library, and are designed to help us get our app off the ground faster &#8211; we imported <em>random</em> to be able to choose names for our soldiers, and <em>pymongo</em> to help us work with our MongoDB database.</p>
</li>
<li>
<p>The next thing we did was create our index view &#8211; by saying that Bottle should route the url &#8216;/&#8217; to our &#8216;index&#8217; function. As you can see, all our index function does is retrieve the current user, and return it so that we can render it with our template &#8211; which we specified using our view() decorator.</p>
</li>
<li>
<p>The next thing we do is define a function to retrieve the current user, based on their IP address &#8211; this is our first dive into working with MongoDB. First, we retrieve the IP address of the computer that&#8217;s requesting the page, so that we can track the visitor&#8217;s information. Once we have that, we query our <em>users</em> database, to see if a user exists with our IP &#8211; if there is no user with the IP address we&#8217;re testing for, we create a new user with an empty army, and then return that one.</p>
</li>
<li>
<p>Finally, we have a little bit of utility code to let us test our Bottle app. This piece of code allows us to run our app by navigating to it in the terminal, and then just running &#8220;python app.py&#8221; &#8211; assuming your MongoDB instance is up and running and your dependencies are okay, you should be able to check out your app by navigating to <a href='http://localhost:8000/'>http://localhost:8000/</a>.</p>
</li>
</ul>
<p>If you navigate to your app right now though, you might notice that there&#8217;s a problem &#8211; we don&#8217;t have an index template! We&#8217;ll fix that by creating a template for our app, under templates/index.html:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot;</span>
<span style="color: #00bbdd;">       &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;</span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html</span> <span style="color: #000066;">lang</span>=<span style="color: #ff0000;">&quot;en&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Python MongoDB Tutorial<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        Your Army: {{ user.army }}
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">'/add-soldier/'</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">'post'</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Add Soldier&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">'/remove-soldier/'</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">'post'</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Remove Soldier&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">'/rename-army/'</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">'post'</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Rename All Soldiers&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>As you can see, we&#8217;re not shattering any barriers with this template &#8211; but it lets us test our app, so it will do for now. Navigate again to <a href='http://localhost:8000/'>http://localhost:8000/</a>, and you should see something like this:</p>
<p><img src='http://buildingbrowsergames.com/blog/wp-content/images/mongodb-python/first.png' alt="Your Army: [], Add Soldier button, Remove Soldier button, Rename All Soldiers button" /></p>
<p>With our template all set up, it&#8217;s time to write our views &#8211; starting with the view that lets us add soldiers:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">@route<span style="color: black;">&#40;</span><span style="color: #483d8b;">'/add-soldier/'</span>, method=<span style="color: #483d8b;">'POST'</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">def</span> add_soldier<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    current_user = get_current_user<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    army = current_user.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'army'</span>, <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    army.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">random</span>.<span style="color: black;">choice</span><span style="color: black;">&#40;</span>SOLDIER_NAMES<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    users.<span style="color: black;">update</span><span style="color: black;">&#40;</span>
        <span style="color: black;">&#123;</span><span style="color: #483d8b;">'ip'</span>: current_user<span style="color: black;">&#91;</span><span style="color: #483d8b;">'ip'</span><span style="color: black;">&#93;</span><span style="color: black;">&#125;</span>,
        <span style="color: black;">&#123;</span><span style="color: #483d8b;">'$set'</span>: <span style="color: black;">&#123;</span>
            <span style="color: #483d8b;">'army'</span>: army,
        <span style="color: black;">&#125;</span><span style="color: black;">&#125;</span>
    <span style="color: black;">&#41;</span>
    redirect<span style="color: black;">&#40;</span><span style="color: #483d8b;">'/'</span><span style="color: black;">&#41;</span></pre></div></div>

<p>For the most part, this view&#8217;s pretty simple &#8211; we want it to be at the url &#8216;/add-soldier/&#8217;, and only responding when users POST to it (by clicking on the &#8216;add soldier&#8217; button in our template). When a user clicks on the button and POSTs to this view, we retrieve their current army (making sure to default to [] if it doesn&#8217;t exist &#8211; this is important!), add a new soldier to it (by appending a random name from our SOLDIER_NAMES variable), and then update the user&#8217;s information in our MongoDB database. Finally, we redirect back to the frontpage so that the user can see their new army.</p>
<p>It&#8217;s important to keep in mind that when you&#8217;re using a schema-less database like MongoDB, you may not actually <strong>have</strong> the properties you&#8217;re expecting to, especially if you haven&#8217;t set them. With that in mind, always be sure to write your code in such a way that it will still work if a an object doesn&#8217;t have the property that you were expecting it to.</p>
<p>If you visit your testing page at <a href='http://localhost:8000/'>http://localhost:8000/</a> now and click the &#8216;Add Soldier&#8217; button, you should notice that your army has increased &#8211; there&#8217;s a new name there!</p>
<p><img src='http://buildingbrowsergames.com/blog/wp-content/images/mongodb-python/second.png' alt="Index page with a new army member" /></p>
<p>Now that we can <strong>add</strong> soldiers to our army, we need to be able to remove them as well. Let&#8217;s write a quick view to do that:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">@route<span style="color: black;">&#40;</span><span style="color: #483d8b;">'/remove-soldier/'</span>, method=<span style="color: #483d8b;">'POST'</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">def</span> remove_soldier<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    current_user = get_current_user<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    army = current_user.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'army'</span>, <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>army<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span>:   <span style="color: #808080; font-style: italic;"># make sure we don't pop from an empty list</span>
        army.<span style="color: black;">pop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    users.<span style="color: black;">update</span><span style="color: black;">&#40;</span>
        <span style="color: black;">&#123;</span><span style="color: #483d8b;">'ip'</span>: current_user<span style="color: black;">&#91;</span><span style="color: #483d8b;">'ip'</span><span style="color: black;">&#93;</span><span style="color: black;">&#125;</span>,
        <span style="color: black;">&#123;</span><span style="color: #483d8b;">'$set'</span>: <span style="color: black;">&#123;</span>
            <span style="color: #483d8b;">'army'</span>: army,
        <span style="color: black;">&#125;</span><span style="color: black;">&#125;</span>
    <span style="color: black;">&#41;</span>
    redirect<span style="color: black;">&#40;</span><span style="color: #483d8b;">'/'</span><span style="color: black;">&#41;</span></pre></div></div>

<p>This view is just about the same as our <em>add_soldier</em> view from earlier, except that we&#8217;re removing soldiers instead of adding them before we write the user&#8217;s army into our database. We make sure they actually have soldiers in their &#8216;army&#8217; array, and then we remove one if they do before writing their army back to the database, and redirecting back to the front page.</p>
<p><img src='http://buildingbrowsergames.com/blog/wp-content/images/mongodb-python/first.png' alt="Index page, with an empty army" /></p>
<p>With that view written, there&#8217;s only one more to write &#8211; this one will live at &#8216;/rename-army/&#8217;, and update the names of all the soldiers in the user&#8217;s army:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">@route<span style="color: black;">&#40;</span><span style="color: #483d8b;">'/rename-army/'</span>, method=<span style="color: #483d8b;">'POST'</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">def</span> rename_army<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    current_user = get_current_user<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    army = current_user.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'army'</span>, <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    army = <span style="color: black;">&#91;</span><span style="color: #dc143c;">random</span>.<span style="color: black;">choice</span><span style="color: black;">&#40;</span>SOLDIER_NAMES<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> army<span style="color: black;">&#93;</span>
    users.<span style="color: black;">update</span><span style="color: black;">&#40;</span>
        <span style="color: black;">&#123;</span><span style="color: #483d8b;">'ip'</span>: current_user<span style="color: black;">&#91;</span><span style="color: #483d8b;">'ip'</span><span style="color: black;">&#93;</span><span style="color: black;">&#125;</span>,
        <span style="color: black;">&#123;</span><span style="color: #483d8b;">'$set'</span>: <span style="color: black;">&#123;</span>
            <span style="color: #483d8b;">'army'</span>: army,
        <span style="color: black;">&#125;</span><span style="color: black;">&#125;</span>
    <span style="color: black;">&#41;</span>
    redirect<span style="color: black;">&#40;</span><span style="color: #483d8b;">'/'</span><span style="color: black;">&#41;</span></pre></div></div>

<p>As you can see, there isn&#8217;t much changed in this view either &#8211; we retrieve the user and their army, update the value of their &#8216;army&#8217; array, write it to the database, and then redirect them. We use a <a href='http://docs.python.org/tutorial/datastructures.html#list-comprehensions'>list comprehension</a> to update the name of every one of our army members, and that&#8217;s all that&#8217;s special here.</p>
<p>With that, we&#8217;re done! If you visit <a href="http://localhost:8000/">http://localhost:8000/</a>, you should be able to add soldiers, remove soldiers, and rename soldiers &#8211; with all the data being stored in your MongoDB database.</p>
<p>If you&#8217;re stuck, or can&#8217;t seem to get it going, you can download the code for this tutorial at <a href='http://buildingbrowsergames.com/blog/wp-content/downloads/mongodb-python-1.zip'>http://buildingbrowsergames.com/blog/wp-content/downloads/mongodb-python-1.zip</a>.</p>
<h2>Extra Homework</h2>
<ul>
<li>
<p>Our views involved writing a lot of boilerplate code, which can probably be refactored. Write a custom User object that handles storage and retrieval, along with making it easier to interact with the user&#8217;s army.</p>
</li>
</li>
<p>Modify the &#8216;army&#8217; array to store custom Soldier objects, which have a name <strong>and</strong> an attack value &#8211; when a user adds a soldier to their army, randomize both values (some of the work for this has been done for you &#8211; see the download above).</p>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2010/02/08/getting-started-with-mongodb-python/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Adding a Reward System to your Game</title>
		<link>http://buildingbrowsergames.com/2010/01/25/adding-a-reward-system-to-your-game/</link>
		<comments>http://buildingbrowsergames.com/2010/01/25/adding-a-reward-system-to-your-game/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 14:00:06 +0000</pubDate>
		<dc:creator>Luke</dc:creator>
				<category><![CDATA[tutorial]]></category>
		<category><![CDATA[rewards]]></category>
		<category><![CDATA[topsite]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/2010/01/25/adding-a-reward-system-to-your-game/</guid>
		<description><![CDATA[I was recently contacted by Sunchaser from the BBGameZone forums about membership within the PBBG network for his topsite, top-pbbg.com. As you can see, his membership was accepted &#8211; and I also managed to get him to write a quick tutorial about how to integrate his site into your PBBG &#8211; so that you can [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently contacted by <strong>Sunchaser</strong> from <a href='http://bbgamezone.net'>the BBGameZone forums</a> about membership within <a href='http://pbbgnetwork.com'>the PBBG network</a> for his topsite, <a href='http://top-pbbg.com'>top-pbbg.com</a>. As you can see, his membership was accepted &#8211; and I also managed to get him to write a quick tutorial about how to integrate <strong>his</strong> site into <strong>your</strong> PBBG &#8211; so that you can drive traffic to your game by providing incentives for your players, using a reward system. Here&#8217;s the tutorial in full:</p>
<p><p>It&#8217;s possible to find on the internet a large number of sites that shows a list of browser-based games (these are normally called Topsites, or Toplists). A game owner can register their game into these sites &#8211; and by inserting a voting link in their game, their players can cast their vote for the game. With enough votes, your game can be pushed into the top positions on the topsite &#8211; increasing visibility, <strong>and</strong> driving more traffic to your game.</p>
<p>Some of these sites allow you, the game owner, to configure a <strong>reward system</strong> for your game, so that you can give your players an incentive to vote for your game.</p>
<p><img src='http://buildingbrowsergames.com/blog/wp-content/images/pic_1.png' style='width:600px;' /></p>
<p>Once you have configured the reward URL, an example of a voting link would be <strong>http://exampletoplist.com/in.php?u=789&#038;key=12345667</strong>.</p>
<p>The parameter &#8216;u&#8217; will contain the ID of the player that is voting, and the key is a random number. After the topsite has verified the vote, it will call back your configured link (for example, <strong>http://yourgame.com/reward.php?u=789&#038;key=12345667&#038;listname=top_pbbg</strong>). The reward script should check for a corresponding couple (userid + key), and then give the player the reward.</p>
<p>This tutorial will walk you through setting up a system that:</p>
<ul>
<li>Sends a player vote to a toplist, recording the vote</li>
<li>Processes a callback from the toplist</li>
</ul>
<h2>Sending a player vote to a toplist</h2>
<p>In order to trace the votes and prevent cheating, the votes sent to the toplist will need to be recorded within a database table. The first thing we&#8217;ll do is create a database table where we store different toplist sites, along with a table for recording user votes:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`cfg_ranking_sites`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span> <span style="color: #808080; font-style: italic;">--primary key</span>
  <span style="color: #ff0000;">`username`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>      <span style="color: #808080; font-style: italic;">--the username you registered with in the toplist</span>
  <span style="color: #ff0000;">`name`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>          <span style="color: #808080; font-style: italic;">--the toplist name</span>
  <span style="color: #ff0000;">`url`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>          <span style="color: #808080; font-style: italic;">--the toplist url (voting links of the toplist)</span>
  <span style="color: #ff0000;">`refererurl`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>   <span style="color: #808080; font-style: italic;">--the toplist root url</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>MyISAM;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`voting_log`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`user_id`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`list_id`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`vkey`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`status`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`ip`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">15</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`urlcalled`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`timestamp`</span> timestamp <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> CURRENT_TIMESTAMP <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">UPDATE</span> CURRENT_TIMESTAMP<span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>MyISAM;</pre></div></div>

<p>Next, we&#8217;ll add any topsites we wanted to track into our <em>cf_ranking_sites</em> table (in this case, <a href='http://top-pbbg.com'>top-pbbg.com</a>):</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`cfg_ranking_sites`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`username`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`name`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`voting_link`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`refererurl`</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'vlad'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'top_pbbg'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'http://www.top-pbbg.com/index.php?a=in&amp;u=vlad&amp;k=KEY'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'http://www.top-pbbg.com'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Next, we create a file called <em>vote.php</em>, which loads the topsite urls, generates a random key for users, tracks the vote, <strong>and</strong> sends the vote to the topsite. Here&#8217;s the code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/****************************************************************************
File:   vote.php
Desc:   traces a vote creating a random key, and sends the vote.
****************************************************************************/</span>
<span style="color: #000088;">$listname</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'listname'</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// load data of rating site</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span> 
	<span style="color: #0000ff;">&quot;select id, voting_link 
  from cfg_ranking_sites
  where name = '<span style="color: #006699; font-weight: bold;">{$listname}</span>' &quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$list_id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$voting_link</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// generate a rnd string</span>
<span style="color: #000088;">$k</span> <span style="color: #339933;">=</span>  <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">md5</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10000000</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">25</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// get the user id from session</span>
<span style="color: #666666; font-style: italic;">// (substitute your method for finding the user id)</span>
<span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> User<span style="color: #339933;">::</span><span style="color: #004000;">get_char_info</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$u</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$user</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// place the generated random key and trace the vote</span>
<span style="color: #000088;">$urlcalled</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;KEY&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$k</span><span style="color: #339933;">,</span> <span style="color: #000088;">$voting_link</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;insert into 
	voting_log
  ( user_id, vkey, list_id, ip, status, urlcalled ) 
  values
  ( '<span style="color: #006699; font-weight: bold;">{$u}</span>', '<span style="color: #006699; font-weight: bold;">{$k}</span>', <span style="color: #006699; font-weight: bold;">$list_id</span>, '&quot;</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: #0000ff;">&quot;', 'new', '<span style="color: #006699; font-weight: bold;">{$urlcalled}</span>' ) &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// send the vote</span>
<span style="color: #990000;">header</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;location: <span style="color: #006699; font-weight: bold;">$urlcalled</span>&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">exit</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Next, put your voting link inside a page that&#8217;s visible to your players, so that they can start clicking on it. Make sure the link points to your <em>vote.php</em> file:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">'vote.php?listname=top_pbbg'</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Vote for us at Top PBBG!<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Now, your players should be able to vote, and you should be able to record their votes in the table <em>voting_log</em>.</p>
<h2>Processing a callback from the toplist</h2>
<p>Let&#8217;s assume you have configured your reward URL on <a href='http://top-pbbg.com'>top-pbbg.com</a> to be the following reward URL:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">'http://yourgame.com/reward.php?listname=top_pbbg'</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Vote for us at Top PBBG!<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>To process the toplist callback and give rewards to your player(s), first create a file called <em>reward.php</em> and insert the following code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Set content type</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-type: text/xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Check input, if keys are not present return error.</span>
<span style="color: #666666; font-style: italic;">// This check and the fields needs to be changed for each toplist</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>
	<span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'key'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> or 
	<span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'success'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> or
	<span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'listname'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> or
	<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;response rewarded=&quot;0&quot; message=&quot;Invalid input.&quot; /&gt;'</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get data</span>
<span style="color: #000088;">$key</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'key'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$success</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'success'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$referer</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_REFERER'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;select v.id, v.user_id, r.name
from voting_log v, cfg_ranking_sites r
where vkey = '<span style="color: #006699; font-weight: bold;">{$key}</span>' and v.list_id = r.id
and   refererurl = '<span style="color: #006699; font-weight: bold;">{$referer}</span>'
and   status = 'new'&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$sql</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">mysql_num_rows</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// insert your debug message, return error</span>
<span style="color: #666666; font-style: italic;">// mydebug( true, &quot;match not found. ip: &quot; . $_SERVER['REMOTE_ADDR'] . &quot; key: &quot; . $key . &quot; referer: &quot; . $referer);</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;response rewarded=&quot;0&quot; message=&quot;match not found.&quot; /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #990000;">exit</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user_id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$list_name</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// give rewards to user, only if the vote was unique ($success==1)</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$success</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$r</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$r</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		<span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//example: give reward and send information to user</span>
		<span style="color: #666666; font-style: italic;">//mysql_query( &quot;update user set char_adamantium = char_adamantium + 2 where user_id = &quot; . $user_id );</span>
		<span style="color: #666666; font-style: italic;">//User::send_news( 'char', $user_id, &quot;You got 2 adamantium for your vote at &quot; . $list_name );</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">else</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//example: give reward and send information to user</span>
		<span style="color: #666666; font-style: italic;">//mysql_query( &quot;update user set char_mythril = char_mythril + 2 where user_id = &quot; . $user_id );</span>
		<span style="color: #666666; font-style: italic;">//User::send_news( 'char', $user_id, &quot;You got 2 mythril for your vote at &quot; . $list_name );</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;update voting_log set status = 'rewardgiven' where id = &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$id</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;response rewarded=&quot;1&quot; message=&quot;Reward given.&quot; /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;update voting_log set status = 'rewardnotgiven' where id = &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$id</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;response rewarded=&quot;0&quot; message=&quot;Reward not given (not unique vote).&quot; /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #990000;">exit</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now, by changing the code within the if statements above, you should be able to give rewards to your players for voting for your game on different topsites.</p>
<h2>Note</h2>
<p>The script <em>reward.php</em> has been written to process rewards for one toplist &#8211; if you want it to process results from multiple toplists, you will want to have it handle the <em>listname</em> parameter, using that to decide which piece of code to run for which topsite.</p>
<p>And that&#8217;s all there is to setting up your game with a rewards URL! If you&#8217;re interested in doing this for your game, sign up at <a href='http://top-pbbg'>top-pbbg.com</a> and get started &#8211; and then post a link to your game in the comments when you&#8217;re finished.</p>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2010/01/25/adding-a-reward-system-to-your-game/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Looking for a tutorial?</title>
		<link>http://buildingbrowsergames.com/2009/11/18/looking-for-a-tutorial/</link>
		<comments>http://buildingbrowsergames.com/2009/11/18/looking-for-a-tutorial/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 14:00:32 +0000</pubDate>
		<dc:creator>Luke</dc:creator>
				<category><![CDATA[site]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=847</guid>
		<description><![CDATA[It&#8217;s that time of year again &#8212; the time of year when Building Browsergames solicits ideas for the next batch of tutorials we should write.
What are you looking to build? What features are you getting stuck on? If there&#8217;s anything you&#8217;d like to see a tutorial on(anything at all) &#8212; post a comment below and [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s that time of year again &mdash; the time of year when Building Browsergames solicits ideas for the next batch of tutorials we should write.</p>
<p>What are <b>you</b> looking to build? What features are <b>you</b> getting stuck on? If there&#8217;s anything you&#8217;d like to see a tutorial on(anything at all) &mdash; post a comment below and I will do my very best to get to it.</p>
<p>Tutorial requests will be accepted for a week, so you have until the 25th of November &#8211; at that point, I will take the list and everyone will be allowed to vote on which ones they&#8217;d like to see first.</p>
<p>So &mdash; what do you want to see a tutorial on?</p>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2009/11/18/looking-for-a-tutorial/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Sending unknown number of variable parameters to new page in HTML</title>
		<link>http://buildingbrowsergames.com/2008/09/17/sending-unknown-number-of-variable-parameters-to-new-page-in-html/</link>
		<comments>http://buildingbrowsergames.com/2008/09/17/sending-unknown-number-of-variable-parameters-to-new-page-in-html/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 14:00:34 +0000</pubDate>
		<dc:creator>gostyloj</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[terratanks]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=386</guid>
		<description><![CDATA[Before I get started I wanted to say that I am not a web server expert so if you know of a better way to do this, post it in the comments and I will thank you as I implement your knowledge in my game.
When using PHP you eventually come to the realization that your [...]]]></description>
			<content:encoded><![CDATA[<p>Before I get started I wanted to say that I am not a web server expert so if you know of a better way to do this, post it in the comments and I will thank you as I implement your knowledge in my game.</p>
<p>When using PHP you eventually come to the realization that your gatekeepers for everything you do and display are HTTP and HTML. Â They are both great tools but were designed with a more static implementation in mind.  Values passed to new web pages are set up in key->value pairs that are accessed with an associative array using the name of the parameter as the key (ie. $_POST['myParam']). Â This post will deal with the issue of how to send an unknown number of parameters which are user set to a new page which can then use those parameters correctly.</p>
<p>My particular implementation is my Mining page in my game <a href="http://www.terratanks.com">TerraTanks</a>. Â The mining page allows the user to set the type of mining on each of their planets so that if they are deficient in a particular resource, they can mine it out faster. Â I wanted to make sure that this could be changed globally, so the mining page will show all of your planets and each planet will have a set of radio buttons allowing you to change the mining settings. Â With these constraints, you cannot know how many planets the user will have and you must write your code to account for any number of parameters.</p>
<p>This is a simplified version of what I have:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// I have the result set for all my mining planets in $result</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$element</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_object</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// code displaying what I need about planet //</span>
&nbsp;
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;INPUT TYPE=<span style="color: #000099; font-weight: bold;">\&quot;</span>radio<span style="color: #000099; font-weight: bold;">\&quot;</span> name=<span style="color: #000099; font-weight: bold;">\&quot;</span>miner<span style="color: #006699; font-weight: bold;">{$count}</span><span style="color: #000099; font-weight: bold;">\&quot;</span> value=<span style="color: #000099; font-weight: bold;">\&quot;</span>normal,<span style="color: #006699; font-weight: bold;">$element-&gt;location</span><span style="color: #000099; font-weight: bold;">\&quot;</span> checked&gt;Normal&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;INPUT TYPE=<span style="color: #000099; font-weight: bold;">\&quot;</span>radio<span style="color: #000099; font-weight: bold;">\&quot;</span> name=<span style="color: #000099; font-weight: bold;">\&quot;</span>miner<span style="color: #006699; font-weight: bold;">{$count}</span><span style="color: #000099; font-weight: bold;">\&quot;</span> value=<span style="color: #000099; font-weight: bold;">\&quot;</span>iron,<span style="color: #006699; font-weight: bold;">$element-&gt;location</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;Iron&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$count</span><span style="color: #339933;">++;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I am creating the name of the radio button group by appending the value of $count the the word miner and then incrementing count as long as there are planets.  Basically, I am hacking together my own array.  It should be noted that I am setting the first radio button to the checked state.  The web page that will read the input depends on everything that exists having a value so it is necessary to make sure that something is selected.  In your work you will want to check what the value is previously set to and make sure the correct radio button is checked when the page loads.</p>
<p>You may also notice that I am shoving a lot of data into the value sent appended by commas.  This is a decent way of shoving related data through without having to define a ton of variables.</p>
<p>Now we have to create something on the receiving page that will correctly read in the values that we are sending.  This is the code that I am using:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$indexName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;miner&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$indexName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$miner</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$indexName</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$x</span><span style="color: #339933;">++;</span>
	<span style="color: #000088;">$indexName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;miner&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span>	
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If you read the code in English it roughly says, as long as there is a POST value at the index miner{$x} where $x starts at 0 and increments by one then just keep incrementing $x and saving the value into my own array.  This is why it was so important that as long as you had a planet you had a value that was passed.  If you set a value for planet 0, 1, and 5 but not 2, 3, or 4 then the code would stop at 2, determine there was no value and would never get to 5.</p>
<p>Now instead of creating a second pseudo array with hidden inputs that pointed to the location of the planet, I packed all the information into a comma appended string.  When I get the value of $miner[$x] I can use the php function explode to separate out the sub values.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$minerParts</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$miner</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// now $minerParts[$x][0] is the mining type and $minerParts[$x][1] is the location</span></pre></div></div>

<p>So there is a way to send an unknown number of variables to a new page.  Like I said, if you know of a better way make sure you post it in the comments.  I don&#8217;t claim to be an expert on this subject, it is just how I resolved this problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2008/09/17/sending-unknown-number-of-variable-parameters-to-new-page-in-html/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Putting It All Together (Ruby on Rails)</title>
		<link>http://buildingbrowsergames.com/2008/09/05/putting-it-all-together-ruby-on-rails/</link>
		<comments>http://buildingbrowsergames.com/2008/09/05/putting-it-all-together-ruby-on-rails/#comments</comments>
		<pubDate>Fri, 05 Sep 2008 14:00:38 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[buildingbrowsergames]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[gettingstarted]]></category>
		<category><![CDATA[rubyonrails]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=309</guid>
		<description><![CDATA[Introduction
This entry is based upon this entry from the Building Browsergames blog: Putting It All Together (PHP)
A Quick Recap
In past installments we provided a way for users to authenticate who they were (login, logout, signup) and have users with stats. The original article shows the SQL that was used to create the tables but we [...]]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>This entry is based upon this entry from the Building Browsergames blog: <a href="http://buildingbrowsergames.com/2008/05/30/building-browsergames-putting-it-all-together-php/">Putting It All Together (PHP)</a></p>
<h2>A Quick Recap</h2>
<p>In past installments we provided a way for users to authenticate who they were (login, logout, signup) and have users with stats. The original article shows the SQL that was used to create the tables but we used migrations from our Rails code to do the same thing.</p>
<p>The example code we&#8217;ve already built is basically equivalent to what is built in the PHP entry so all we&#8217;re going to do is a few minor tweaks to match the version outlined in the article about the PHP version:</p>
<ul>
<li>Our pages don&#8217;t have titles &#8211; We&#8217;ll use the layout feature of Rails to fix that.</li>
<li>Our pages don&#8217;t have any style &#8211; The PHP examples don&#8217;t have much style either, but at least they&#8217;ve got messages in green and red depending upon the type of message.</li>
</ul>
<p>Note: One other difference is that restful_authentication logs you in immediately after registration. If the plan is not to require email confirmation, I prefer that we do it that way.</p>
<p>Let&#8217;s go through the changes required to make our code match the PHP version feature for feature so we&#8217;re up with the latest version.<br />
<h3>Add Titles</h3>
<p>We&#8217;re going to use another part of the templating engine in Rails to make it easy to have a title on every page. Rails has a concept called layouts. A layout takes the [something].html.erb files we&#8217;ve been creating and wraps them with another [something].html.erb file. It&#8217;s handy where you want a group of pages to all have the same headers, footers, navigation, etc. which, if you think about it, is most every website you go to. In order to wrap all the pages you create a layout called application.html.erb. A layout with that name in the layouts directory will be used as a default layout for the entire application. You can override the default easily if you need to though, one of the extra credit learning opportunities this week is about that. Here&#8217;s the app/views/layouts/application.html.erb I created:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;&lt;%<span style="color: #66cc66;">=</span> @<span style="color: #000066;">title</span> %&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
    <span style="color: #009900;">&lt;%<span style="color: #66cc66;">=</span> yield  %&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>Now we can edit the various controllers (welcome_controller.rb, users_controller.rb, and sessions_controller.rb) adding assignments to @title in the methods that get called before we go to the view. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> WelcomeController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  <span style="color:#9966CC; font-weight:bold;">def</span> index
    <span style="color:#008000; font-style:italic;"># Call the function current_user to get the user assigned to the </span>
    <span style="color:#008000; font-style:italic;"># @current_user variable (if anyone is logged in). The</span>
    <span style="color:#008000; font-style:italic;"># function comes with restful_authentication.</span>
    current_user
&nbsp;
    <span style="color:#0066ff; font-weight:bold;">@title</span> = <span style="color:#996600;">&quot;Welcome&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Note that all we added here was an assignment to the @title variable in the controller action. Once the action is finished Rails will render the app/views/welcome/index.html.erb (with our new layout surrounding it) and the @title will go into the title of the page.</p>
<h3>Add A Little Style</h3>
<p>Styling message display is pretty easy, we just put a div tag with a class around the flash[:notice] output and we&#8217;re set. The class can then be styled any way we like:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;% if flash<span style="color: #66cc66;">&#91;</span>:notice<span style="color: #66cc66;">&#93;</span> %&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;flashNotice&quot;</span>&gt;&lt;%<span style="color: #66cc66;">=</span> h flash<span style="color: #66cc66;">&#91;</span>:notice<span style="color: #66cc66;">&#93;</span> %&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;% end %&gt;</span></pre></div></div>

<p>To handle styling our entire game we&#8217;ll create a new file (public/stylesheets/pbbg.css). To get the stylesheet loaded we can add the following line within the head section of app/views/layouts/application.html.erb:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;%<span style="color: #66cc66;">=</span> stylesheet_link_tag <span style="color: #ff0000;">&quot;pbbg&quot;</span>  %&gt;</span></pre></div></div>

<p>Styling the errors that come out in the form whenever a user&#8217;s input fails validation isn&#8217;t much more complicated. If we take our registration form as an example, we can botch the password confirmation field and submit to see what we get:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;errorExplanation&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;errorExplanation&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>3 errors prohibited this user from being saved<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>There were problems with the following fields:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>Password confirmation can't be blank<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>Password can't be blank<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>Password is too short (minimum is 6 characters)<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/users&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span>&gt;</span> 
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;margin:0;padding:0&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;authenticity_token&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;hidden&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;31249bde4aede2633a4391a44eafff49ec27ae17&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&nbsp;
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;login&quot;</span>&gt;</span>Login<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span><span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;user_login&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;user[login]&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;30&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Testing&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
&nbsp;
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;email&quot;</span>&gt;</span>Email<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span><span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;user_email&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;user[email]&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;30&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sample@email.com&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
&nbsp;
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;password&quot;</span>&gt;</span>Password<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span><span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;fieldWithErrors&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;user_password&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;user[password]&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;30&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;password&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
&nbsp;
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;password_confirmation&quot;</span>&gt;</span>Confirm Password<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span><span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;fieldWithErrors&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;user_password_confirmation&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;user[password_confirmation]&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;30&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;password&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
&nbsp;
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;commit&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Sign up&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></div></div>

<p>That&#8217;s the error output and the form (with some of the error output wrapped through it) generated automatically by Rails. The thing to notice there is that Rails has inserted a couple of div tags automatically. One wraps the description of any and all errors (i.e. class=&#8221;errorExplanation&#8221;) and the other wraps individual fields which had errors (i.e. class=&#8221;fieldWithErrors&#8221;). By styling those two classes and even tags within each one (e.g. the h2 and ul tags in the explanation section) we can make the error output from Rails a little more appealing.</p>
<p>It probably wouldn&#8217;t take you a lot of effort to come up with better CSS to style the tags than what I present here, but at least it will give you a starting point (public/stylesheets/pbbg.css):</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.errorExplanation</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#ffcccc</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #993333;">red</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">vertical-align</span><span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.errorExplanation</span> h2 <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">maroon</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.errorExplanation</span> ul li <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">list-style</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">square</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.fieldWithErrors</span> input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;text&quot;</span><span style="color: #00AA00;">&#93;</span><span style="color: #00AA00;">,</span>
<span style="color: #6666ff;">.fieldWithErrors</span> input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;password&quot;</span><span style="color: #00AA00;">&#93;</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">border-color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">red</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.flashNotice</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #993333;">green</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#ccffcc</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Here&#8217;s what the error page above looks like with that crude CSS applied to it. Note that not only is the error explanation styled, but that the fields that are in error are outlined in red:</p>
<div id="attachment_308" class="wp-caption aligncenter" style="width: 297px"><a href="http://buildingbrowsergames.com/blog/wp-content/uploads/2008/08/sign-up-with-a-minimum-of-style.png"><img src="http://buildingbrowsergames.com/blog/wp-content/uploads/2008/08/sign-up-with-a-minimum-of-style-287x300.png" alt="The sign up form showing some simple CSS attributes applied to error messages and error fields." title="Sign Up (with a minimum of style)" width="287" height="300" class="size-medium wp-image-308" /></a><p class="wp-caption-text">The sign up form showing some simple CSS attributes applied to error messages and error fields.</p></div>
<h2>Extra Credit</h2>
<ol>
<li>You&#8217;ve already seen that you can easily have a default layout for the entire application. Watch <a href="http://railscasts.com/episodes/7-all-about-layouts">Railscasts #7 &#8211; All About Layouts</a> to learn about default layouts for any view in a particular controller and about how to override the default layout when you want something different for certain actions or even dynamically decide which layout to use.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2008/09/05/putting-it-all-together-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Making Your Forms Auto-Focus (Ruby on Rails)</title>
		<link>http://buildingbrowsergames.com/2008/09/03/making-your-forms-auto-focus-ruby-on-rails/</link>
		<comments>http://buildingbrowsergames.com/2008/09/03/making-your-forms-auto-focus-ruby-on-rails/#comments</comments>
		<pubDate>Wed, 03 Sep 2008 14:00:55 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[buildingbrowsergames]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[rubyonrails]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=290</guid>
		<description><![CDATA[Introduction
This covers the same material as this entry from Building Browsergames: Making Your Forms Auto-Focus
Why Do We Have To Do Anything Different?
The advice is excellent and it works perfectly, but it has to be tweaked ever so slightly for Rails. Rails actually generates your form fields for you and names them itself in order to [...]]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>This covers the same material as this entry from Building Browsergames: <a href="http://buildingbrowsergames.com/2008/05/20/making-your-forms-auto-focus/">Making Your Forms Auto-Focus</a></p>
<h2>Why Do We Have To Do Anything Different?</h2>
<p>The advice is excellent and it works perfectly, but it has to be tweaked ever so slightly for Rails. Rails actually generates your form fields for you and names them itself in order to easily copy an object into the fields when populating a form and then make a new object out of the form inputs afterward. As a result, the form_for command you see in a page like app/views/users/new.html.erb:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#5A0A0A; font-weight:bold;">form_for</span> <span style="color:#ff3333; font-weight:bold;">:user</span>, <span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> users_path <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">-%&gt;</span>
  &lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= label_tag <span style="color:#996600;">'login'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br/&gt;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:login</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
&nbsp;
  &lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= label_tag <span style="color:#996600;">'email'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br/&gt;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
&nbsp;
  &lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= label_tag <span style="color:#996600;">'password'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br/&gt;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#5A0A0A; font-weight:bold;">password_field</span> <span style="color:#ff3333; font-weight:bold;">:password</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
&nbsp;
  &lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= label_tag <span style="color:#996600;">'password_confirmation'</span>, <span style="color:#996600;">'Confirm Password'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br/&gt;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#5A0A0A; font-weight:bold;">password_field</span> <span style="color:#ff3333; font-weight:bold;">:password_confirmation</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
&nbsp;
  &lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= submit_tag <span style="color:#996600;">'Sign up'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">-%&gt;</span></pre></div></div>

<p>Generates HTML that looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/users&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;margin:0;padding:0&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;authenticity_token&quot;</span> </span>
<span style="color: #009900;"><span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;hidden&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;41adf13eec9e99506a55c427ce59052a5543cf70&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;login&quot;</span>&gt;</span>Login<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span><span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;user_login&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;user[login]&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;30&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
&nbsp;
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;email&quot;</span>&gt;</span>Email<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span><span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;user_email&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;user[email]&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;30&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
&nbsp;
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;password&quot;</span>&gt;</span>Password<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span><span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;user_password&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;user[password]&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;30&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;password&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
&nbsp;
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;password_confirmation&quot;</span>&gt;</span>Confirm Password<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span><span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;user_password_confirmation&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;user[password_confirmation]&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;30&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;password&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
&nbsp;
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;commit&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Sign up&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></div></div>

<p>So when we write our JavaScript, we need to make sure it specifies the correct element ID for the HTML that will actually be generated by Rails:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
window.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'user_login'</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>user_login is the name of the field that Rails will generate for login input in our case. If you aren&#8217;t sure of the name that will be used, just go to the page in your browser and view the page source. You can see the actual label used there.</p>
<h2>Extra Credit</h2>
<ol>
<li>Line #3 of the HTML code above features an &#8220;authenticity_token&#8221; that Rails inserted automatically for us. Find out what it does and why you want it.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2008/09/03/making-your-forms-auto-focus-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Making Your Forms Remember Their Values (Ruby on Rails)</title>
		<link>http://buildingbrowsergames.com/2008/09/02/making-your-forms-remember-their-values-ruby-on-rails/</link>
		<comments>http://buildingbrowsergames.com/2008/09/02/making-your-forms-remember-their-values-ruby-on-rails/#comments</comments>
		<pubDate>Tue, 02 Sep 2008 14:00:13 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[buildingbrowsergames]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[rubyonrails]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=295</guid>
		<description><![CDATA[Introduction
This entry is based on an entry of the same name from Building Browsergames: Making Your Forms Remember Their Values
I Think We&#8217;re Already There
You may have noticed from our existing forms that it was remembering things like the login and feeding it back to the form automatically whenever there was an error. That&#8217;s because form_for [...]]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>This entry is based on an entry of the same name from Building Browsergames: <a href="http://buildingbrowsergames.com/2008/05/21/making-your-forms-remember-their-values/">Making Your Forms Remember Their Values</a></p>
<h2>I Think We&#8217;re Already There</h2>
<p>You may have noticed from our existing forms that it was remembering things like the login and feeding it back to the form automatically whenever there was an error. That&#8217;s because form_for in your view can take an object you created in the controller and use it to fill in the various fields as it builds them for the HTML. Likewise, when a form gets submitted, it automatically takes those fields and puts them into a Ruby hash which can be directly assigned to a new object to initialize it. That natural flow from controller to view and back to another controller when the user submits makes it easy to have forms &#8220;remember&#8221; their previous values whenever an error occurs or whenever they need to edit an existing record in the database.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#5A0A0A; font-weight:bold;">form_for</span> <span style="color:#ff3333; font-weight:bold;">:user</span>, <span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> users_path <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">-%&gt;</span>
  &lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= label_tag <span style="color:#996600;">'login'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br/&gt;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:login</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
&nbsp;
  &lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= label_tag <span style="color:#996600;">'email'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br/&gt;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
&nbsp;
  &lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= label_tag <span style="color:#996600;">'password'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br/&gt;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#5A0A0A; font-weight:bold;">password_field</span> <span style="color:#ff3333; font-weight:bold;">:password</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
&nbsp;
  &lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= label_tag <span style="color:#996600;">'password_confirmation'</span>, <span style="color:#996600;">'Confirm Password'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br/&gt;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#5A0A0A; font-weight:bold;">password_field</span> <span style="color:#ff3333; font-weight:bold;">:password_confirmation</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
&nbsp;
  &lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= submit_tag <span style="color:#996600;">'Sign up'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">-%&gt;</span></pre></div></div>

<p>Simply by specifying the text field as &#8220;f.text_field :login&#8221; pulls the username from @user (which was supplied by the new function in app/controllers/users_controller.rb) and if there is any value on @user for that field then it is used to fill in the field as the page is built. You don&#8217;t have to do any of this yourself. Just learn and follow the standard Rails conventions for these things and it will handle the rest automatically.</p>
<p>If you&#8217;d like to see it in action, start entering in some intentionally wrong values into the sign up form and submit it. What you will see is how you get bounced in a loop between the new and create functions in the UsersController (app/controllers/users_controller.rb) and the app/views/users/new.html.erb. As you do so, even though it is may be erroneous, what you enter in for login and email will be preserved between one submission and the next and filled back into the form for you to correct and submit again (though Rails is noticing that the destination fields are password fields and it doesn&#8217;t fill those in for security reasons).</p>
<h2>Extra Credit</h2>
<ol>
<li><a href="http://dizzy.co.uk/cheatsheets">Dizzy</a> offers several excellent one page cheatsheets you can print out for quick reference on the topics of forms, migrations, and ActionMailer. You might want to download the PDF for the FormHelper cheatsheet and print it out for quick reference.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2008/09/02/making-your-forms-remember-their-values-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Getting Started With A Templating System (Ruby on Rails)</title>
		<link>http://buildingbrowsergames.com/2008/08/29/getting-started-with-a-templating-system-ruby-on-rails/</link>
		<comments>http://buildingbrowsergames.com/2008/08/29/getting-started-with-a-templating-system-ruby-on-rails/#comments</comments>
		<pubDate>Fri, 29 Aug 2008 14:00:42 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[buildingbrowsergames]]></category>
		<category><![CDATA[rubyonrails]]></category>
		<category><![CDATA[templates]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=277</guid>
		<description><![CDATA[Introduction
This entry is based on the Building Browsergames entry: Getting Started With A Templating System (PHP)
Since we&#8217;re using Ruby on Rails, we&#8217;re don&#8217;t have to select a templating system, there&#8217;s one already built into Rails itself. It offers many different features to make it easy to build web pages out of parts, cache parts of [...]]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>This entry is based on the Building Browsergames entry: <a href="http://buildingbrowsergames.com/2008/05/16/building-browsergames-setting-up-a-templating-system-php/">Getting Started With A Templating System (PHP)</a></p>
<p>Since we&#8217;re using Ruby on Rails, we&#8217;re don&#8217;t have to select a templating system, there&#8217;s one already built into Rails itself. It offers many different features to make it easy to build web pages out of parts, cache parts of pages or whole pages, etc. As with most (probably all) parts of Rails, alternatives are available to what&#8217;s built in but you&#8217;ll be satisfied with what&#8217;s already there for quite a while.</p>
<p>I particularly want to amplify something Luke said in the original posting. He thought a lot of developers were fans of developing their own templating systems. Wow. If that is in fact true, then you are seriously misguided if you go down that path. What&#8217;s built into Rails and the Smarty templating system Luke used for his PHP work is more than enough to get your pages done. <strong>Do not waste your time on side projects like building a templating system</strong>, use the time to improve your overall programming skills or build your game. If you do, you might be one of the few percent who actually finishes a project and others get to play it.</p>
<h2>Using Rails Views</h2>
<p>The design of Rails is already about separating the view which builds our HTML from the controller which connects the model to the view. So displaying the user&#8217;s name in the view is as simple as adding some code like the following to the already existing app/views/welcome/index.html.erb:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#5A0A0A; font-weight:bold;">render</span> <span style="color:#ff3333; font-weight:bold;">:partial</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'users/user_bar'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#5A0A0A; font-weight:bold;">flash</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= h <span style="color:#5A0A0A; font-weight:bold;">flash</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
&lt;h1&gt;Welcome To The Game&lt;/h1&gt;
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">if</span> logged_in? <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;p&gt;Hello, <span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#0066ff; font-weight:bold;">@current_user</span>.<span style="color:#9900CC;">login</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>!&lt;/p&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>It&#8217;s the same as it was before except that we&#8217;ve added the section at the end that checks to see if we&#8217;re logged in and greets us if we are. Now, as long as the controller method that is called for this page sets up a @current_user variable the view will pull the login out of it and display it within the page. The restful_authentication plugin provides us with an easy to use function to check if the user is logged in or not and put the user&#8217;s info into the @current_user variable if he/she is. We&#8217;ll call that from our controller (app/controllers/welcome_controller.rb) in the index function (remember, the flow of a user&#8217;s page request always goes to the controller first to set everything up and then to the view from there).</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> index
  current_user
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>If you recall from before, the old index method didn&#8217;t do anything. Now it calls the function which will fill in the @current_user variable before the index.html.erb file gets used to generate the view. At this point if you login you should find that everything works nicely.</p>
<p>By this point we&#8217;ve seen that the templating code (in the .html.erb files) gives us the ability to include partials pages to reuse sections of HTML, pull data from variables, and even conditionally show or not show parts of the page.</p>
<h2>Extra Credit</h2>
<ol>
<li>To learn more about the functions that restful_authentication makes available to you, look in the file lib/authenticated_system.rb. There are several functions in there to help you determine whether or not the user is logged in, restrict pages from users who aren&#8217;t yet logged in, etc.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2008/08/29/getting-started-with-a-templating-system-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Simple Cron</title>
		<link>http://buildingbrowsergames.com/2008/07/17/simple-cron/</link>
		<comments>http://buildingbrowsergames.com/2008/07/17/simple-cron/#comments</comments>
		<pubDate>Thu, 17 Jul 2008 14:00:51 +0000</pubDate>
		<dc:creator>Luke</dc:creator>
				<category><![CDATA[cron]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=86</guid>
		<description><![CDATA[One of the questions that I seem to get asked most about building a browsergame is &#8220;how do I make things happen periodically?&#8221;.
The answer is actually very simple, and lies with a utility that comes on all Linux servers called cron.
Cron exists to run periodic, scheduled tasks for you &#8211; based on a file called [...]]]></description>
			<content:encoded><![CDATA[<p>One of the questions that I seem to get asked most about building a browsergame is <strong>&#8220;how do I make things happen periodically?&#8221;</strong>.</p>
<p>The answer is actually very simple, and lies with a utility that comes on all Linux servers called <em>cron</em>.</p>
<p>Cron exists to run periodic, scheduled tasks for you &#8211; based on a file called a &#8220;crontab&#8221;. You can run any command you want at any time that you want using cron.</p>
<p>In order to edit your crontab, get access to the console of your webserver and type the following command:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">crontab -e</pre></div></div>

<p>If your server doesn&#8217;t have any cronjobs currently in place, you&#8217;ll just see an empty file. We will be editing this file to make things happen periodically.</p>
<p>The first five columns in your crontab correspond to measurements of time; the sixth column is the command to run. The time columns are for the hour, minute, day of the month, month, and day of week to run the command on. Here&#8217;s what a command that runs every day at 3:30 AM might look like:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">30 3 * * * echo &quot;ran cron at 3:30 AM&quot; &gt; /var/log/misc/crons.log</pre></div></div>

<p>If you wanted to make something happen periodically in your game, you would just need to write the code for whatever you wanted to have happen periodically, and then write a cronjob to call it with the proper parameters.</p>
<p>You can also configure cronjobs to run in shorter intervals, or even at multiple intervals:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">0 */3 * * * echo &quot;this cronjob runs every 3 hours&quot;</pre></div></div>

<p>The above cronjob will run every 3 hours &#8211; and the one below will run only once a month:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">0 0 1 * * echo &quot;this cronjob runs every 3 hours&quot;</pre></div></div>

<p>If you don&#8217;t have access to your server&#8217;s command line to play around with cron, try talking to your webhost or investigating your control panel if you have one &#8211; in most cases, webhosts that won&#8217;t allow you command-line access will still allow you to create your own crontabs.</p>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2008/07/17/simple-cron/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Building Browsergames: Putting it all together (Perl)</title>
		<link>http://buildingbrowsergames.com/2008/06/02/building-browsergames-putting-it-all-together-perl/</link>
		<comments>http://buildingbrowsergames.com/2008/06/02/building-browsergames-putting-it-all-together-perl/#comments</comments>
		<pubDate>Mon, 02 Jun 2008 14:00:13 +0000</pubDate>
		<dc:creator>Luke</dc:creator>
				<category><![CDATA[buildingbrowsergames]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[gettingstarted]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=52</guid>
		<description><![CDATA[We&#8217;ve already built a registration and page, in addition to getting started with a templating system &#8211; so let&#8217;s put it all together, and start making a game out of all our individual parts.
To begin with, a quick design decision &#8211; we will not require new users to verify their e-mail address(or even supply it [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve already built a <a href='http://buildingbrowsergames.com/2008/04/18/building-browsergames-the-registration-page-perl/'>registration</a> and <a href='http://buildingbrowsergames.com/2008/04/25/building-browsergames-the-login-page-perl/'>page</a>, in addition to <a href='http://buildingbrowsergames.com/2008/05/18/building-browsergames-setting-up-a-templating-system-perl/'>getting started with a templating system</a> &#8211; so let&#8217;s put it all together, and start making a game out of all our individual parts.</p>
<p>To begin with, a quick design decision &#8211; we will not require new users to verify their e-mail address(or even supply it when they register). This is a decision that&#8217;s up to you, really &#8211; and if you really want to make users verify their address, you can always adjust the code supplied here to use the <a href='http://buildingbrowsergames.com/2008/05/12/building-browsergames-implementing-an-e-mail-confirmation-system-perl/'>e-mail confirmation system</a> we built earlier for it.</p>
<p>To start off, we need a database structure. We&#8217;ve been sort of building one organically as we walked through each of the individual parts of our game, so let&#8217;s put it all in one place:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> users <span style="color: #66cc66;">&#40;</span>
	id int <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
	username varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">250</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	password varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	is_admin tinyint<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> stats <span style="color: #66cc66;">&#40;</span>
	id int <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
	display_name text<span style="color: #66cc66;">,</span>
	short_name varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> stats<span style="color: #66cc66;">&#40;</span>display_name<span style="color: #66cc66;">,</span>short_name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Magic'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'mag'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> stats<span style="color: #66cc66;">&#40;</span>display_name<span style="color: #66cc66;">,</span>short_name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Attack'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'atk'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> stats<span style="color: #66cc66;">&#40;</span>display_name<span style="color: #66cc66;">,</span>short_name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Defence'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'def'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> user_stats <span style="color: #66cc66;">&#40;</span>
	id int <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
	user_id int<span style="color: #66cc66;">,</span>
	stat_id int<span style="color: #66cc66;">,</span>
	value text<span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>With that done, we need to modify our registration page &#8211; so that it auto-focuses into the right textbox, remembers what was sent to it, and uses HTML::Template. Here&#8217;s what the template it will use is going to look like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">	&lt;html&gt;
	&lt;head&gt;
		&lt;title&gt;Register&lt;/title&gt;
	&lt;/head&gt;
	&lt;body&gt;
		&lt;tmpl_if name='error'&gt;
			&lt;span style='color:red'&gt;Error: &lt;!--tmpl_var name='error'--&gt;&lt;/span&gt;
		&lt;/tmpl_if&gt;
		&lt;tmpl_if name='message'&gt;
			&lt;span style='color:green'&gt;&lt;!--tmpl_var name='message'--&gt;&lt;/span&gt;
		&lt;/tmpl_if&gt;
		&lt;form method='post' action='register.cgi'&gt;
			Username: &lt;input type='text' name='username' id='username' value='&lt;!--tmpl_var name=&quot;username&quot;--&gt;' /&gt;&lt;br /&gt;
			Password: &lt;input type='password' name='password' /&gt;&lt;br /&gt;
			Confirm Password: &lt;input type='password' name='confirm' /&gt;&lt;br /&gt;
			&lt;input type='submit' value='Register!' /&gt;
		&lt;/form&gt;
		&lt;script type='text/javascript'&gt;
		document.getElementById('username').focus();
		&lt;/script&gt;
	&lt;/body&gt;
	&lt;/html&gt;</pre></td></tr></table></div>

<p>We use a &lt;tmpl_if&gt; tag to make sure that our error and success messages only appear when there&#8217;s something to display &#8211; but other than that, you&#8217;ve seen all of the code in this template before. Save the file as <strong>register.tmpl</strong>.</p>
<p>The next piece of code we need to write is for the actual page that loads in the template and handles all of the database work that we need behind it. It&#8217;s pretty easy to just modify our other registration code to work with a template:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl -w</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> CGI <span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">:</span>cgi<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> CGI<span style="color: #339933;">::</span><span style="color: #006600;">Carp</span> <span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span>fatalsToBrowser warningsToBrowser<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> DBI<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> config<span style="color: #339933;">;</span>		<span style="color: #666666; font-style: italic;"># this is our database settings</span>
<span style="color: #000000; font-weight: bold;">use</span> HTML<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$query</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CGI<span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">%arguments</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">Vars</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$template</span> <span style="color: #339933;">=</span> HTML<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span>
		filename	<span style="color: #339933;">=&gt;</span>	<span style="color: #ff0000;">'register.tmpl'</span><span style="color: #339933;">,</span>
		associate	<span style="color: #339933;">=&gt;</span>	<span style="color: #0000ff;">$query</span><span style="color: #339933;">,</span>		<span style="color: #666666; font-style: italic;"># for argument memory</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">%parameters</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">%arguments</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$arguments</span><span style="color: #009900;">&#123;</span>password<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">ne</span> <span style="color: #0000ff;">$arguments</span><span style="color: #009900;">&#123;</span>confirm<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #0000ff;">$parameters</span><span style="color: #009900;">&#123;</span>error<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'Those passwords do not match!'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dbh</span> <span style="color: #339933;">=</span> DBI<span style="color: #339933;">-&gt;</span><span style="color: #006600;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;DBI:mysql:$dbname:$dbhost&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$dbuser</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$dbpass</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>RaiseError <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;SELECT count(id) FROM users WHERE UPPER(username) = UPPER(?)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$arguments</span><span style="color: #009900;">&#123;</span>username<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$count</span><span style="color: #339933;">;</span>
		<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">bind_columns</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">\$count</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">fetch</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$count</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #0000ff;">$parameters</span><span style="color: #009900;">&#123;</span>error<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'That username is taken.'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #0000ff;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;INSERT INTO users(username,password) VALUES (?,?)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$arguments</span><span style="color: #009900;">&#123;</span>username<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #000066;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$arguments</span><span style="color: #009900;">&#123;</span>password<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$arguments</span><span style="color: #009900;">&#123;</span>username<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #0000ff;">$parameters</span><span style="color: #009900;">&#123;</span>message<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'Congratulations! You registered successfully!'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">$template</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">param</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">%parameters</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">print</span> <span style="color: #0000ff;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">header</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$template</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">output</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>So now we have a registration form&#8230;but what about logging in? And stats? Players are going to need stats to play our game. We&#8217;ll just build in our <a href='http://buildingbrowsergames.com/2008/05/07/building-browsergames-implementing-a-flexible-stats-system-perl/'>flexible stats system code</a> from earlier, as a file called <strong>stats.pm</strong> &#8211; and then any code that needs it can use it. We&#8217;ll modify it slightly so that it inserts a default value of 0 for stats that it can&#8217;t retrieve, though:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000066;">package</span> stats<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> DBI<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> getStat <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$userID</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">use</span> config<span style="color: #339933;">;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dbh</span> <span style="color: #339933;">=</span> DBI<span style="color: #339933;">-&gt;</span><span style="color: #006600;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;DBI:mysql:$dbname:$dbhost&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$dbuser</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$dbpass</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>RaiseError <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;SELECT count(value) FROM user_stats WHERE stat_id = (SELECT id FROM stats WHERE display_name = ? OR short_name = ?) AND user_id = ?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$count</span><span style="color: #339933;">;</span>
	<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">bind_columns</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">\$count</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">fetch</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$count</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;"># no entry for that stat/user combination - insert one with a value of 0</span>
		<span style="color: #0000ff;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;INSERT INTO user_stats(stat_id,user_id,value) VALUES ((SELECT id FROM stats WHERE display_name = ? OR short_name = ?),?,?)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$userID</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #0000ff;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;SELECT value FROM user_stats WHERE stat_id = (SELECT id FROM stats WHERE display_name = ? OR short_name = ?) AND user_id = ?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$value</span><span style="color: #339933;">;</span>
	<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">bind_columns</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">\$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">fetch</span><span style="color: #339933;">;</span>
	<span style="color: #000066;">return</span> <span style="color: #0000ff;">$value</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">sub</span> setStat <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$userID</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$statValue</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">use</span> config<span style="color: #339933;">;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dbh</span> <span style="color: #339933;">=</span> DBI<span style="color: #339933;">-&gt;</span><span style="color: #006600;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;DBI:mysql:$dbname:$dbhost&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$dbuser</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$dbpass</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>RaiseError <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;UPDATE user_stats SET value = ? WHERE stat_id = (SELECT id FROM stats WHERE display_name = ? OR short_name = ?) AND user_id = ?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$statValue</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now, we&#8217;ve set up our database, created our registration page, and written the code that will allow us to retrieve a user&#8217;s stats once they&#8217;ve logged in. But users <strong>can&#8217;t</strong> login yet &#8211; there isn&#8217;t a page for them to login with! So we&#8217;ll build that next, starting with the template(called <strong>login.tmpl</strong>):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">	&lt;html&gt;
	&lt;head&gt;
		&lt;title&gt;Login&lt;/title&gt;
	&lt;/head&gt;
	&lt;body&gt;
		&lt;tmpl_if name='error'&gt;
			&lt;span style='color:red'&gt;Error: &lt;!--tmpl_var name='error'--&gt;&lt;/span&gt;
		&lt;/tmpl_if&gt;
		&lt;form action='login.cgi' method='post'&gt;
			Username: &lt;input type='text' name='username' id='username' value='&lt;!--tmpl_var name=&quot;username&quot;--&gt;' /&gt;&lt;br /&gt;
			Password: &lt;input type='password' name='password' /&gt;&lt;br /&gt;
			&lt;input type='submit' value='Login' /&gt;
		&lt;/form&gt;	
		&lt;script type='text/javascript'&gt;
		document.getElementById('username').focus();
		&lt;/script&gt;
	&lt;/body&gt;
	&lt;/html&gt;</pre></td></tr></table></div>

<p>And then we&#8217;ll modify our existing login code to use the template:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl -w</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> CGI <span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">:</span>cgi<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> CGI<span style="color: #339933;">::</span><span style="color: #006600;">Carp</span> <span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span>fatalsToBrowser warningsToBrowser<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> DBI<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> config<span style="color: #339933;">;</span>		<span style="color: #666666; font-style: italic;"># this is our database settings</span>
<span style="color: #000000; font-weight: bold;">use</span> HTML<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$query</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CGI<span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">%arguments</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">Vars</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$template</span> <span style="color: #339933;">=</span> HTML<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span>
		filename	<span style="color: #339933;">=&gt;</span>	<span style="color: #ff0000;">'login.tmpl'</span><span style="color: #339933;">,</span>
		associate	<span style="color: #339933;">=&gt;</span>	<span style="color: #0000ff;">$query</span><span style="color: #339933;">,</span>		<span style="color: #666666; font-style: italic;"># for argument memory</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">%parameters</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">%arguments</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dbh</span> <span style="color: #339933;">=</span> DBI<span style="color: #339933;">-&gt;</span><span style="color: #006600;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;DBI:mysql:$dbname:$dbhost&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$dbuser</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$dbpass</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>RaiseError <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;SELECT COUNT(id) FROM users WHERE UPPER(username) = UPPER(?) AND password = ?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$count</span><span style="color: #339933;">;</span>
	<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$arguments</span><span style="color: #009900;">&#123;</span>username<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #000066;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$arguments</span><span style="color: #009900;">&#123;</span>password<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$arguments</span><span style="color: #009900;">&#123;</span>username<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">bind_columns</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">\$count</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">fetch</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$count</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #0000ff;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;UPDATE users SET last_login = NOW() WHERE UPPER(username) = UPPER(?) AND password = ?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$arguments</span><span style="color: #009900;">&#123;</span>username<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #000066;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$arguments</span><span style="color: #009900;">&#123;</span>username<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$arguments</span><span style="color: #009900;">&#123;</span>password<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #0000ff;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;SELECT is_admin FROM users WHERE UPPER(username) = UPPER(?) AND password = ?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$is_admin</span><span style="color: #339933;">;</span>
		<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$arguments</span><span style="color: #009900;">&#123;</span>username<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #000066;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$arguments</span><span style="color: #009900;">&#123;</span>password<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$arguments</span><span style="color: #009900;">&#123;</span>username<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">bind_columns</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">\$is_admin</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">fetch</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$cookie</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">cookie</span><span style="color: #009900;">&#40;</span>
				<span style="color: #339933;">-</span>name	<span style="color: #339933;">=&gt;</span>	<span style="color: #ff0000;">'username+password'</span><span style="color: #339933;">,</span>
				<span style="color: #339933;">-</span>value	<span style="color: #339933;">=&gt;</span>	<span style="color: #0000ff;">$arguments</span><span style="color: #009900;">&#123;</span>username<span style="color: #009900;">&#125;</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">'+'</span> <span style="color: #339933;">.</span> <span style="color: #000066;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$arguments</span><span style="color: #009900;">&#123;</span>password<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$arguments</span><span style="color: #009900;">&#123;</span>username<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				<span style="color: #339933;">-</span>expires	<span style="color: #339933;">=&gt;</span>	<span style="color: #ff0000;">'+3M'</span><span style="color: #339933;">,</span>
			<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$uri</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'index.cgi'</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$is_admin</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;"># redirect to admin page</span>
			<span style="color: #0000ff;">$uri</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'admin.cgi'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066;">print</span> <span style="color: #0000ff;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">header</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>cookie<span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">$cookie</span><span style="color: #339933;">,-</span>location<span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">$uri</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #0000ff;">$parameters</span><span style="color: #009900;">&#123;</span>error<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'That username and password combination does not match any currently in our database.'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">$template</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">param</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">%parameters</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">print</span> <span style="color: #0000ff;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">header</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$template</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">output</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>And, finally, we&#8217;ll create an ultra-simple index page(for now) that will show the user their username, and give them a link to log back out. Here&#8217;s our template, saved as <strong>index.tmpl</strong>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">	&lt;html&gt;
	&lt;head&gt;
		&lt;title&gt;Index Page&lt;/title&gt;
	&lt;/head&gt;
	&lt;body&gt;
		&lt;p&gt;Hello, &lt;!--tmpl_var name='username'--&gt;!&lt;/p&gt;
		&lt;p&gt;&lt;a href='logout.cgi'&gt;Logout&lt;/a&gt;&lt;/p&gt;
	&lt;/body&gt;
	&lt;/html&gt;</pre></td></tr></table></div>

<p>And this is the code to load in and display the template, inside <strong>index.cgi</strong>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl -w</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> CGI <span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">:</span>cgi<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> HTML<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$query</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CGI<span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$cookie</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">cookie</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'username+password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$username</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #000066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\+/</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$cookie</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$template</span> <span style="color: #339933;">=</span> HTML<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span>
		filename	<span style="color: #339933;">=&gt;</span>	<span style="color: #ff0000;">'index.tmpl'</span><span style="color: #339933;">,</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$template</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">param</span><span style="color: #009900;">&#40;</span>username	<span style="color: #339933;">=&gt;</span>	<span style="color: #0000ff;">$username</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">print</span> <span style="color: #0000ff;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">header</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$template</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">output</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>As the one, final thing we need to do to bring everything together, we&#8217;ll build the logout page. It&#8217;s an extremely simple page to build:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl -w</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> CGI <span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">:</span>cgi<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$query</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CGI<span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$logout</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">cookie</span><span style="color: #009900;">&#40;</span>
	<span style="color: #339933;">-</span>name		<span style="color: #339933;">=&gt;</span>	<span style="color: #ff0000;">'username+password'</span><span style="color: #339933;">,</span>
	<span style="color: #339933;">-</span>value		<span style="color: #339933;">=&gt;</span>	<span style="color: #ff0000;">''</span><span style="color: #339933;">,</span>
	<span style="color: #339933;">-</span>expires	<span style="color: #339933;">=&gt;</span>	<span style="color: #ff0000;">'-3M'</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">print</span> <span style="color: #0000ff;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>cookie<span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">$logout</span><span style="color: #339933;">,-</span>uri<span style="color: #339933;">=&gt;</span><span style="color: #ff0000;">'http://google.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>All we do in that page is set the value of the cookie to nothing, and the expiration to sometime in the past &#8211; which means it will be deleted by the user&#8217;s browser. When we print out the header to remove the cookie, we also redirect the user &#8211; in this case, to <a href='http://google.com'>http://google.com</a>(but you could change this to whatever you wanted &#8211; Wordpress, for example, redirects to its login page).</p>
<p>And that&#8217;s all there is to the basic skeleton of our game! You now have a registration page, a login page, a very sparse index page(we&#8217;ll work on that more later), and a logout page &#8211; in addition to all that, you&#8217;ve also got the code written to play with a user&#8217;s stats(which we&#8217;ll get to later). It might not seem like much, but today we&#8217;ve set up the basic framework that every single browsergame needs &#8211; including yours.</p>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2008/06/02/building-browsergames-putting-it-all-together-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

