<?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>Thronic.com &#187; Development</title>
	<atom:link href="http://thronic.com/category/development/feed/" rel="self" type="application/rss+xml" />
	<link>http://thronic.com</link>
	<description>Personal Computing and Development by Dag J. Nedrelid</description>
	<lastBuildDate>Wed, 28 Apr 2010 12:21:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Google-like search suggestion tool</title>
		<link>http://thronic.com/2010/development/devlog/google-like-search-suggestion-tool/</link>
		<comments>http://thronic.com/2010/development/devlog/google-like-search-suggestion-tool/#comments</comments>
		<pubDate>Wed, 28 Apr 2010 11:53:57 +0000</pubDate>
		<dc:creator>Dag Jonny Nedrelid</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Devlog]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://thronic.com/?p=202</guid>
		<description><![CDATA[A search tool that resembles the way google shows suggestions when typing keywords. Basically a simple javascript application that I wrote from scratch to see if I could create something similar.

&#160;
ReviewSpindle
A gateway between articles and affiliate links.
Key features:
* It dynamically updates the suggestion list with bold character recognition.
* The suggestion list can be navigated using [...]]]></description>
			<content:encoded><![CDATA[<p>A search tool that resembles the way google shows suggestions when typing keywords. Basically a simple javascript application that I wrote from scratch to see if I could create something similar.<br />
<span id="more-202"></span>
<p>&nbsp;</p>
<p><a href="http://reviewspindle.com/" target="_blank">ReviewSpindle</a><br />
A gateway between articles and affiliate links.</p>
<p>Key features:<br />
* It dynamically updates the suggestion list with bold character recognition.<br />
* The suggestion list can be navigated using arrow keys and mouse.<br />
* When using arrow keys, movement takes over from mouse location.</p>
<p>The application JS code is easily available through viewing the source. The PHP used is just a 2-dimensional array with titles and urls. And a foreach-loop to check for form submits, along with populating the JS version of the array.</p>
]]></content:encoded>
			<wfw:commentRss>http://thronic.com/2010/development/devlog/google-like-search-suggestion-tool/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Resize a div layer with javascript</title>
		<link>http://thronic.com/2010/development/resize-a-div-layer-with-javascript/</link>
		<comments>http://thronic.com/2010/development/resize-a-div-layer-with-javascript/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 14:19:34 +0000</pubDate>
		<dc:creator>Dag Jonny Nedrelid</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[WebDev]]></category>

		<guid isPermaLink="false">http://thronic.com/?p=199</guid>
		<description><![CDATA[Formula and example included.
&#160;
&#160;

Pseudo formula:

// Acronyms used to save space.
New style.left = PCW+((E.X-PCX)-(MDX-PCX))
New style.top = PCH+((E.Y-PCY)-(MDY-PCY))
// PCW/PCH = Previous Container Width/Height.
// E.X/E.Y = Event X/Y.
// PCX/PCY = Previous Container Left/Top.
// MDX/MDY = Mouse Down X/Y (where you clicked).


With this formula you will resize the layer relative to where you clicked on it. An extended version [...]]]></description>
			<content:encoded><![CDATA[<p>Formula and example included.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><span id="more-199"></span></p>
<p>Pseudo formula:</p>
<blockquote><p style="margin:0px">
// Acronyms used to save space.<br />
New style.left = PCW+((E.X-PCX)-(MDX-PCX))<br />
New style.top = PCH+((E.Y-PCY)-(MDY-PCY))</p>
<p>// PCW/PCH = Previous Container Width/Height.<br />
// E.X/E.Y = Event X/Y.<br />
// PCX/PCY = Previous Container Left/Top.<br />
// MDX/MDY = Mouse Down X/Y (where you clicked).
</p>
</blockquote>
<p>With this formula you will resize the layer relative to where you clicked on it. An extended version of the movement formula. In effect it will take the existing width/height and add the amount of pixel movement as a positive or negative number.</p>
<p><a href="http://thronic.com/pubfiles/divresize.html" target="_blank">My example</a>, just view the source to see my code.</p>
]]></content:encoded>
			<wfw:commentRss>http://thronic.com/2010/development/resize-a-div-layer-with-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Moving a div layer with javascript</title>
		<link>http://thronic.com/2010/development/moving-a-div-layer-with-javascript/</link>
		<comments>http://thronic.com/2010/development/moving-a-div-layer-with-javascript/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 14:11:14 +0000</pubDate>
		<dc:creator>Dag Jonny Nedrelid</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[WebDev]]></category>

		<guid isPermaLink="false">http://thronic.com/?p=198</guid>
		<description><![CDATA[Formula and example included.
&#160;
&#160;

Pseudo formula:

New style.left = MovingMouseX-(PushedMouseX-PreviousStyleLeft)
New style.top = MovingMouseY-(PushedMouseY-PreviousStyleTop)


With this formula you will move the layer relative to where you clicked on it.
My example, just view the source to see my code.
]]></description>
			<content:encoded><![CDATA[<p>Formula and example included.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><span id="more-198"></span></p>
<p>Pseudo formula:</p>
<blockquote><p style="margin:0px">
New style.left = MovingMouseX-(PushedMouseX-PreviousStyleLeft)<br />
New style.top = MovingMouseY-(PushedMouseY-PreviousStyleTop)
</p>
</blockquote>
<p>With this formula you will move the layer relative to where you clicked on it.</p>
<p><a href="http://thronic.com/pubfiles/divmove.html" target="_blank">My example</a>, just view the source to see my code.</p>
]]></content:encoded>
			<wfw:commentRss>http://thronic.com/2010/development/moving-a-div-layer-with-javascript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Web Galaxy &#187; A sci-fi browser game</title>
		<link>http://thronic.com/2010/development/devlog/web-galaxy-a-sci-fi-browser-game/</link>
		<comments>http://thronic.com/2010/development/devlog/web-galaxy-a-sci-fi-browser-game/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 17:06:29 +0000</pubDate>
		<dc:creator>Dag Jonny Nedrelid</dc:creator>
				<category><![CDATA[Devlog]]></category>
		<category><![CDATA[Gaming]]></category>

		<guid isPermaLink="false">http://thronic.com/?p=189</guid>
		<description><![CDATA[Web Galaxy is a simple multiplayer browser game. It was made spontaneously without any specific game design in mind, I pretty much winged the whole thing as I went along based on awesome randomness. All codes and algorithms is original work. I borrowed graphics from the web and processed most of it through photoshop to [...]]]></description>
			<content:encoded><![CDATA[<p>Web Galaxy is a simple multiplayer browser game. It was made spontaneously without any specific game design in mind, I pretty much winged the whole thing as I went along based on awesome randomness. All codes and algorithms is original work. I borrowed graphics from the web and processed most of it through photoshop to get what I needed.</p>
<p><span id="more-189"></span></p>
<p>Web Galaxy currently has 10 solar systems with 30 asteroids in every system by default. Web browsers may lag when running more than 30-50 asteroids in the solar system view through the asteroid movement engine, I want to avoid that. If I were to add support for more player activity I would probably want to build more solar systems instead.</p>
<p><strong>Technical</strong><br />
Mainly Javascript functionality. DHTML for effects, AJAX for real-time database updates, PHP and MySQL for back-end. The whole project consists of 15 images and 6 web files, 903KiB in total size. Game is wrapped in a very simple and basic user login system, just enough to keep players separated. The game should work without cookies as well.</p>
<p>I started making the game Feb. 9, 2010. Game was considered completed enough for testing on the Feb. 15, 2010. And that is where it is right now until I decide to put more work into it. The next step would be to create a messaging and/or chat system between players.</p>
<p><strong>Gameplay illustrations:</strong><br />

<a href='http://thronic.com/2010/development/devlog/web-galaxy-a-sci-fi-browser-game/attachment/wgg1/' title='Establish your home base.'><img width="150" height="150" src="http://thronic.com/wp-content/uploads/2010/02/WGG1-150x150.jpg" class="attachment-thumbnail" alt="Step1: Build your command center, ship factory and ship tech center. Build a spaceship and upgrade it with a laser weapon. Upgrade your command center with mineral operations to earn more minerals. You can do this on every asteroid you claim." title="Establish your home base." /></a>
<a href='http://thronic.com/2010/development/devlog/web-galaxy-a-sci-fi-browser-game/attachment/wgg2/' title='Travel the galaxy.'><img width="150" height="150" src="http://thronic.com/wp-content/uploads/2010/02/WGG2-150x150.jpg" class="attachment-thumbnail" alt="Step2: Once you have built a spaceship, you can travel the web galaxy to increase your empire and build more Mineral Operations. Mineral Operations will increase your economy." title="Travel the galaxy." /></a>
<a href='http://thronic.com/2010/development/devlog/web-galaxy-a-sci-fi-browser-game/attachment/wgg3/' title='Travel to solar systems in search for other asteroids.'><img width="150" height="150" src="http://thronic.com/wp-content/uploads/2010/02/WGG3-150x150.jpg" class="attachment-thumbnail" alt="Step3: Travel to solar systems in search for other asteroids." title="Travel to solar systems in search for other asteroids." /></a>
<a href='http://thronic.com/2010/development/devlog/web-galaxy-a-sci-fi-browser-game/attachment/wgg4/' title='Attack other players with your spaceship.'><img width="150" height="150" src="http://thronic.com/wp-content/uploads/2010/02/WGG4-150x150.jpg" class="attachment-thumbnail" alt="Step4: If you attack another player&#039;s home base, they will be struck hard, loose their spaceship and will either have to use a nuclear defense or wait for help from other players." title="Attack other players with your spaceship." /></a>
<a href='http://thronic.com/2010/development/devlog/web-galaxy-a-sci-fi-browser-game/attachment/wgg5/' title='Structure struck by laser weapon.'><img width="150" height="150" src="http://thronic.com/wp-content/uploads/2010/02/WGG5-150x150.jpg" class="attachment-thumbnail" alt="The spaceship laser weapon is a powerful thing, but it also cost you 500 minerals every time you fire it, so consider your targets!" title="Structure struck by laser weapon." /></a>
<a href='http://thronic.com/2010/development/devlog/web-galaxy-a-sci-fi-browser-game/attachment/wgg6/' title='Expand your empire for survival and glory.'><img width="150" height="150" src="http://thronic.com/wp-content/uploads/2010/02/WGG6-150x150.jpg" class="attachment-thumbnail" alt="Step5: After wiping another player from an asteroid surface, you can claim the asteroid for yourself. More mineral operations means more minerals! And more survival as you will generate a lot of minerals, and be able to use more nuclear defenses." title="Expand your empire for survival and glory." /></a>
</p>
<p>You can try out the game yourself <a href="http://webgalaxygame.thronic.com/" target="_blank">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://thronic.com/2010/development/devlog/web-galaxy-a-sci-fi-browser-game/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>IE and PHP sessions</title>
		<link>http://thronic.com/2010/development/ie-and-php-sessions/</link>
		<comments>http://thronic.com/2010/development/ie-and-php-sessions/#comments</comments>
		<pubDate>Tue, 12 Jan 2010 12:46:35 +0000</pubDate>
		<dc:creator>Dag Jonny Nedrelid</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[WebDev]]></category>

		<guid isPermaLink="false">http://thronic.com/?p=180</guid>
		<description><![CDATA[I ran into a strange encounter after testing out a session based login system through an IE browser. I had been working mostly with it in other browsers, mainly Firefox where it was working fine.

IE would not recognize the session at all. IE did not block the cookies either, and I tested the SID constant [...]]]></description>
			<content:encoded><![CDATA[<p>I ran into a strange encounter after testing out a session based login system through an IE browser. I had been working mostly with it in other browsers, mainly Firefox where it was working fine.</p>
<p><span id="more-180"></span></p>
<p>IE would not recognize the session at all. IE did not block the cookies either, and I tested the SID constant thoroughly, it was not getting populated either.</p>
<p><strong>The problem</strong><br />
In the same browser application I had open &#8211; just in another tab &#8211; I was logged into a HTTP AUTH session on another site, though under the same domain.</p>
<p>Of some reason IE seems to mix up the processes when opening up different tabs or windows through the same opened application. This way it doesn&#8217;t seem to properly keep track of which sessions belong to which processes.</p>
<p><strong>The solution</strong><br />
Exit the IE application and start a new one. If I open up my session based website in the first tab, it works. It also works fine after opening the HTTP AUTH website in a second tab. But I don&#8217;t know how stable this is.</p>
<p>Just another IE annoyance&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://thronic.com/2010/development/ie-and-php-sessions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wordpress 2.8.6 Spell Check Languages</title>
		<link>http://thronic.com/2009/development/webdev/wordpress-2-8-6-spell-check-languages/</link>
		<comments>http://thronic.com/2009/development/webdev/wordpress-2-8-6-spell-check-languages/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 16:33:54 +0000</pubDate>
		<dc:creator>Dag Jonny Nedrelid</dc:creator>
				<category><![CDATA[WebDev]]></category>

		<guid isPermaLink="false">http://thronic.com/?p=177</guid>
		<description><![CDATA[When trying to add norwegian as the default language for one of my wordpress sites, I had problems finding documentation for where the language list actually is located. Tracking it down myself become a long task as well. Here is how you can edit the languages for your wordpress system.

The most logical place to look, [...]]]></description>
			<content:encoded><![CDATA[<p>When trying to add norwegian as the default language for one of my wordpress sites, I had problems finding documentation for where the language list actually is located. Tracking it down myself become a long task as well. Here is how you can edit the languages for your wordpress system.</p>
<p><span id="more-177"></span></p>
<p>The most logical place to look, is in:<br />
<code>wp-includes/js/tinymce/plugins/spellchecker/editor_plugin.js</code></p>
<p>Changes made there however doesn&#8217;t reflect in the administration editor. You also need to make changes in:<br />
<code>/wp-admin/includes/post.php</code></p>
<p>By default, spell checker uses the google gmail engine. This does not support the norwegian language in my case. So I changed over to the pspell engine. This is what I did to make that work for me:</p>
<blockquote><p><code>(on the server, a debian box)<br />
// Check first if you already have your language available.<br />
#aspell dicts<br />
<em>-bash: aspell: command not found</em></p>
<p>// I did not, so I had to install it and make PHP support it as well.<br />
#apt-get install aspell-no php5-pspell</p>
<p>// Reload the apache web server to reflect PHP update.<br />
#/etc/init.d/apache2 reload</p>
<p>// Verify installation<br />
#aspell dicts<br />
<em>nb<br />
nn<br />
no</em></p>
<p>... Looks good.</code></p></blockquote>
<p>Next you change the engine in this file:<br />
/wp-includes/js/tinymce/plugins/spellchecker/config.php</p>
<p>from: $config['general.engine'] = &#8216;GoogleSpell&#8217;;<br />
to:	$config['general.engine'] = &#8216;PSpell&#8217;;</p>
<p>That&#8217;s it.</p>
]]></content:encoded>
			<wfw:commentRss>http://thronic.com/2009/development/webdev/wordpress-2-8-6-spell-check-languages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reset MySQL Root Password</title>
		<link>http://thronic.com/2009/development/sql/reset-mysql-root-password/</link>
		<comments>http://thronic.com/2009/development/sql/reset-mysql-root-password/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 13:11:46 +0000</pubDate>
		<dc:creator>Dag Jonny Nedrelid</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://thronic.com/?p=175</guid>
		<description><![CDATA[If you have mysql- or root user shell access to the linux host where your mysql is running, you can reset the password like this:

(Debian based example).
First, stop the daemon:
#/etc/init.d/mysqld stop

Second, start mysql without privileges:
#mysqld --skip-grant-tables &#38;

Third, connect to the daemon and make changes:
#mysql
#UPDATE mysql.user SET Password=PASSWORD('NewPassword') WHERE User='root';
#FLUSH PRIVILEGES;
#quit

Now stop the daemon and start [...]]]></description>
			<content:encoded><![CDATA[<p>If you have mysql- or root user shell access to the linux host where your mysql is running, you can reset the password like this:</p>
<p><span id="more-175"></span></p>
<p>(Debian based example).</p>
<p>First, stop the daemon:</p>
<blockquote><p style="margin:0px"><code>#/etc/init.d/mysqld stop</code></p>
</blockquote>
<p>Second, start mysql without privileges:</p>
<blockquote><p style="margin:0px"><code>#mysqld --skip-grant-tables &amp;</code></p>
</blockquote>
<p>Third, connect to the daemon and make changes:</p>
<blockquote><p style="margin:0px"><code>#mysql<br />
#UPDATE mysql.user SET Password=PASSWORD('NewPassword') WHERE User='root';<br />
#FLUSH PRIVILEGES;<br />
#quit</code></p>
</blockquote>
<p>Now stop the daemon and start it normally:</p>
<blockquote><p style="margin:0px"><code>#/etc/init.d/mysql stop<br />
#/etc/init.d/mysql start</code></p>
</blockquote>
<p>It might be sufficient with a simple restart, but I like it the explicit way to be sure.</p>
<p>For more information on this subject, visit the official MySQL reference:<br />
<a href="http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html#resetting-permissions-unix" target="_blank">http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html#resetting-permissions-unix</a></p>
]]></content:encoded>
			<wfw:commentRss>http://thronic.com/2009/development/sql/reset-mysql-root-password/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>IdleGuard</title>
		<link>http://thronic.com/2009/development/devlog/idleguard/</link>
		<comments>http://thronic.com/2009/development/devlog/idleguard/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 20:59:45 +0000</pubDate>
		<dc:creator>Dag Jonny Nedrelid</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Devlog]]></category>
		<category><![CDATA[Gaming]]></category>

		<guid isPermaLink="false">http://thronic.com/?p=165</guid>
		<description><![CDATA[Some MMO games have been criticized for having up to 5 hour &#8211; or more &#8211; queues before you can actually enter the game and play. This inspired me to create a mini-program that might help you with this by reducing the amount of times you need to actually log in. Any software that kicks [...]]]></description>
			<content:encoded><![CDATA[<p>Some MMO games have been criticized for having up to 5 hour &#8211; or more &#8211; queues before you can actually enter the game and play. This inspired me to create a mini-program that might help you with this by reducing the amount of times you need to actually log in. Any software that kicks you for being idle might have a use for this little program.</p>
<p><span id="more-165"></span></p>
<p><strong>IdleGuard</strong><br />
A small program (72kb) that &#8211; so far &#8211; every 5 minutes &#8220;pushes&#8221; the space key for you which should reset the ingame logout timer. It is extremely CPU cycle friendly (no loop stress/block/hang), and will require practically zero resources to keep running.</p>
<p><strong>Is it safe to use?</strong><br />
Yes. The program does not hack or manipulate any memory space of any software. It simply sends a virtual key code into the windows keyboard stream.</p>
<p><strong>Can I only use this with games?</strong><br />
It <em>should</em> work on any applications and systems that would otherwise kick you for being idle for too long.</p>
<p><strong>Vista note</strong><br />
The Windows Vista User Interface Privilege Isolation (UIPI) might prevent this program to run properly or even crash as I have not bothered to error-handle such an event. You should be able to solve this by either turning off UAC or simply run it as administrator.</p>
<p>You can download the program <a href="http://thronic.com/pubfiles/IdleGuard.zip">here</a> (win32 executable, no dependencies).</p>
<p><strong>How do I use it?</strong><br />
Just start it, it will open a small window that you can close when you don&#8217;t need it running anymore. As long as you run it, it will &#8220;push&#8221; the space key for you every 5 minutes. Remember to have your game or program in focus while it&#8217;s running.</p>
]]></content:encoded>
			<wfw:commentRss>http://thronic.com/2009/development/devlog/idleguard/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Column count in SQL</title>
		<link>http://thronic.com/2009/development/column-count-in-sql/</link>
		<comments>http://thronic.com/2009/development/column-count-in-sql/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 11:41:14 +0000</pubDate>
		<dc:creator>Dag Jonny Nedrelid</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://thronic.com/?p=152</guid>
		<description><![CDATA[At rare occasions you might need to know the number of columns in a database table. This isn&#8217;t necessarily as easy as counting rows.

-
SELECT COUNT(1) AS RowCount FROM table
Unless you know where to look:
SELECT COUNT(1) AS ColumnCount
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='tablename';
This should work from SQL server 2000 and MySQL 5 and upwards.
Differences between MSSQL and MySQL.
In MySQL [...]]]></description>
			<content:encoded><![CDATA[<p>At rare occasions you might need to know the number of columns in a database table. This isn&#8217;t necessarily as easy as counting rows.</p>
<p><span id="more-152"></span><br />
<span style="color: #ffffff;">-</span></p>
<blockquote><p><code>SELECT COUNT(1) AS RowCount FROM table</code></p></blockquote>
<p>Unless you know where to look:</p>
<blockquote><p><code>SELECT COUNT(1) AS ColumnCount<br />
FROM INFORMATION_SCHEMA.COLUMNS<br />
WHERE TABLE_NAME='tablename';</code></p></blockquote>
<p>This should work from SQL server 2000 and MySQL 5 and upwards.</p>
<p><strong>Differences between MSSQL and MySQL.</strong><br />
In MySQL you can add the TABLE_SCHEMA to the WHERE clause in order to define what database contains the table you are looking for, to count columns. In MSSQL it seems to only contain &#8220;dbo&#8221; which doesn&#8217;t help much.</p>
<p>There are most likely other places to look for that information, I just haven&#8217;t had any need for it yet. And if your user only has access to 1 database, you shouldn&#8217;t need it anyway.</p>
]]></content:encoded>
			<wfw:commentRss>http://thronic.com/2009/development/column-count-in-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>String encryption in PHP</title>
		<link>http://thronic.com/2009/development/string-encryption-in-php/</link>
		<comments>http://thronic.com/2009/development/string-encryption-in-php/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 23:05:50 +0000</pubDate>
		<dc:creator>Dag Jonny Nedrelid</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WebDev]]></category>

		<guid isPermaLink="false">http://thronic.com/?p=150</guid>
		<description><![CDATA[I&#8217;ve been working on 2 PHP functions that can be used to encrypt and decrypt alphanumeric strings between web pages. This is used for e.g. Captcha codes that you can generate yourself and encrypt / decrypt with these functions.

How does it work?
A key is generated on the first page that also encrypts the string (or [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on 2 PHP functions that can be used to encrypt and decrypt alphanumeric strings between web pages. This is used for e.g. Captcha codes that you can generate yourself and encrypt / decrypt with these functions.</p>
<p><span id="more-150"></span></p>
<p><strong>How does it work?</strong><br />
A key is generated on the first page that also encrypts the string (or code if you want). The same key is used on the second page to decrypt it. Consider the functions below for more information. On the bottom you&#8217;ll find a test script to see the effect of the functions.<br />
<strong><br />
Generating a random string value in PHP (e.g a Captcha code):</strong></p>
<blockquote>
<pre><code>function CreateRandomStringPHP() {
  $code = substr(str_shuffle('abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789'),0,5);
  return $code;
}</code></pre>
</blockquote>
<p>If you are using another language, consider using a function like this:</p>
<blockquote>
<pre><code>function CreateRandomString() {
  $Characters = array(
  'a','b','c','d','e','f','g','h','i','j','k','m','n','p','q','r','s','t','u','v','w','x','y','z',
  'A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z',
  '2','3','4','5','6','7','8','9');

  $Code = '';
  for( $loop_b = 1; $loop_b &lt;= 5; $loop_b++ ) {
    $Code .= $Characters[mt_rand(0,count($Characters)-1)];
  }

  return $Code;
}</code></pre>
</blockquote>
<p>Both of the above functions will generate a 5-length value. This will provide one value out of 550731776 possible combinations which should be enough for most needs.</p>
<p><strong>The lock (or encrypt) function:</strong></p>
<blockquote>
<pre><code>/*
  str LockCode(str $code, str $key)

  A codelock function (or encryption if you want).
  I wrote because I wanted a nice solution for keeping codes
  such as Captcha codes safe and secure for transferring between
  sites.

  It takes a $code of random length and locking $key as parameters.
  You'll need to use the same key to unlock it.

  The value this function returns, is simply a set of location
  coordinates in the key where the characters of the $code is found.
  This is again merged together with the lengths of every coordinate,
  since every coordinate can be 1 or 2 in length as long as the $key
  is under 100 characters in length (0-99).

  In the end, it should provide a safely locked $code, based on that
  you keep the $key hidden. Do NOT show it in your HTML. The key
  used in this example, with 56 alphanumeric characters where I have
  stripped out the following chars: 1, I, l, o, O and 0 (they are
  unpractical) has 7,9164324866862966607842406018063e+97
  combinations. This should be enough to keep the $code safe...
*/
function LockCode($code,$key) {
  $key_chars = str_split($key);
  $code_chars = str_split($code);
  $LockedKeyCoords = '';
  $LockedCoordLengths = '';

  foreach($code_chars as $c_char) {
    for($loop_a=0; $loop_a&lt;count($key_chars); $loop_a++) {
      if($c_char==$key_chars[$loop_a]) {
        $LockedKeyCoords .= (string)$loop_a;
        $LockedCoordLengths .= (string)strlen($loop_a);
      }
    }
  }

  return $LockedKeyCoords . $LockedCoordLengths;
}</code></pre>
</blockquote>
<p><strong>The unlock (or decrypt) function:</strong></p>
<blockquote>
<pre><code>/*
  str UnlockCode(str $coords, str $key, int $keylength)

  The unlock function introduces a new parameter: $Keylength.
  This defines how many characters your $code was before you locked it.
*/
function UnlockCode($coords,$key,$keylength) {
  $key_chars = str_split($key);
  $keylengths = str_split(substr($coords,-$keylength));
  $UnlockedCode = '';

  foreach($keylengths as $length) {
    $UnlockedCode .= $key_chars[(int)substr($coords,0,(int)$length)];
    $coords = substr($coords,(int)$length);
  }

  return $UnlockedCode;
}</code></pre>
</blockquote>
<p>PHP code to test with:</p>
<blockquote>
<pre><code>/*
  The $Key MUST contain the characters you use to create the
  code with. Using the same string as you did to make the $Code
  with is a good choice. Generate a key on a standalone page,
  then use the same key on the page that locks the string, and
  on the page that unlocks the string.

  If you want a new random key every time you use the locking
  functions, you'll have to use a database system to store and
  retrieve them. And use sessions for identifying the keys. This
  would make a separate article, so I won't explain how to do it
  here.
*/
$Key = str_shuffle('abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789');

// Test it out.
$Code = CreateRandomStringPHP();
$LockedCode = LockCode($Code,$Key);
echo 'Key used: '. $Key .'&lt;br&gt;&lt;br&gt;';
echo 'New code: '. $Code .'&lt;br&gt;';
echo 'Locked code: '. $LockedCode .'&lt;br&gt;';
echo 'Unlocked code: '. UnlockCode($LockedCode,$Key,5) .'&lt;br&gt;&lt;br&gt;';
echo 'Reload this page to generate new results.';</code></pre>
</blockquote>
<p>The above testing code would produce something like this:</p>
<blockquote>
<pre><code>Key used: F7AYMQTscfHEPvSBXw5C3eWhj6kUxbn2ayrtGzNiqDV4m9ugJLKZpR8d

New code: c42yV
Locked code: 84331334212222
Unlocked code: c42yV</code></pre>
</blockquote>
<p>You can try it out yourself <a href="/pubfiles/string_encryption_example.php" target="_blank">here</a> (opens in a new window).</p>
]]></content:encoded>
			<wfw:commentRss>http://thronic.com/2009/development/string-encryption-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
