<?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>Guides</title>
	<atom:link href="http://guides.ricehigh.dk/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://guides.ricehigh.dk</link>
	<description>Guides and toturials by Ricehigh</description>
	<lastBuildDate>Wed, 23 Sep 2009 06:40:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Adding numbers as strings</title>
		<link>http://guides.ricehigh.dk/?p=58</link>
		<comments>http://guides.ricehigh.dk/?p=58#comments</comments>
		<pubDate>Fri, 18 Sep 2009 20:25:01 +0000</pubDate>
		<dc:creator>Ricehigh</dc:creator>
				<category><![CDATA[Micro guides]]></category>
		<category><![CDATA[Micro guide]]></category>
		<category><![CDATA[Number theory]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://guides.ricehigh.dk/?p=58</guid>
		<description><![CDATA[Introduction
Today i ran into the problem of not being able to operate with numbers larger than a 1024-bit signed integer, equilavant to 2^1023. I wanted to be able to add two numbers in this size range, providing a number bigger than this barrier. So i wrote a script, that made it possible to do this, [...]]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>Today i ran into the problem of not being able to operate with numbers larger than a 1024-bit signed integer, equilavant to 2^1023. I wanted to be able to add two numbers in this size range, providing a number bigger than this barrier. So i wrote a script, that made it possible to do this, by using a method analouge to how you&#8217;d add large numbers on a piece of paper: i used strings, where I added each element in each string with each other.<br />
The code is fairly simple, and there&#8217;s plenty of comments in it, so I&#8217;ll just post it as-is.</p>
<h3>The code</h3>

<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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> add_as_string<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span><span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// Produce an error if either $a or $b is not a string.</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #990000;">is_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;At least one of the provided numbers was not a string!&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Finds the length of $a or $b, determined by which of $a and $b is the longest string.</span>
	<span style="color: #000088;">$length</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;</span>strlen<span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span>?<span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Zero-pads the strings if neccesary.</span>
	<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%0<span style="color: #006699; font-weight: bold;">{$length}</span>s&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%0<span style="color: #006699; font-weight: bold;">{$length}</span>s&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Here the actual addition takes place. Note that it does the addition &quot;backwards&quot;.</span>
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">=</span><span style="color: #000088;">$length</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><span style="color: #000088;">$x</span><span style="color: #339933;">&gt;=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$x</span><span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$c</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;">$a</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;">$b</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: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$c</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$x</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$c</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: #cc66cc;">10</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_reverse</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Reverse the array containing the addition numbers.</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Puts all the numbers in $c into a string.</span>
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$number</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$result</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">strval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #990000;">pow</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1023</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// the highest value php can give me. (corresponds to the maximum value of a 1024-bit signed integer)</span>
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%1.0f</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// makes $a a string.</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Here we let the parser give the addition a try. Note that this gives INF (meaning infinity).</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$a</span><span style="color: #339933;">+</span><span style="color: #000088;">$a</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// We try to add the number via the add_as_string function.</span>
<span style="color: #666666; font-style: italic;">// Note that when $a==$b, every loop of the for-loop is equal to multiplication with two. Meaning that if you run the loop twice, it's the same as 4*$a, and so fourth.</span>
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><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;">$x</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> add_as_string<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">// We output the value of the </span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$a</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// We try putting through two number's that are unequal in length, therefore making use of the zero-padding.</span>
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> add_as_string<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;5&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;50&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$a</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// We try putting through a number and a string, which will provide an error.</span>
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> add_as_string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$a</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<blockquote><p>The above code outputs:<br />
INF<br />
179769313486231590772930519 &#8230; 79716304835356329624224137216<br />
55<br />
At least one of the provided numbers was not a string!</p></blockquote>
<h3>Notes</h3>
<p>Be aware that the script can only be used to calculate addition of integers!<br />
It should be fairly simple to adopt the method to substraction of numbers. My next script might be multiplication of numbers larger than 2^1023. We&#8217;ll se ;D</p>
]]></content:encoded>
			<wfw:commentRss>http://guides.ricehigh.dk/?feed=rss2&amp;p=58</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Automatically count the number of appendices in LaTeX</title>
		<link>http://guides.ricehigh.dk/?p=48</link>
		<comments>http://guides.ricehigh.dk/?p=48#comments</comments>
		<pubDate>Mon, 24 Aug 2009 06:06:34 +0000</pubDate>
		<dc:creator>Ricehigh</dc:creator>
				<category><![CDATA[LaTeX guides]]></category>
		<category><![CDATA[LaTeX]]></category>

		<guid isPermaLink="false">http://guides.ricehigh.dk/?p=48</guid>
		<description><![CDATA[This guide will show you how to automatically count the number of appendices in your LaTeX document.
First of all you need to place this code in the preamble of your document:

\makeatletter
\newcounter{numapp}
\newcommand{\numberofapp}{%
    \immediate\write\@auxout%
      {\string\setcounter{numapp}{\the\c@chapter}}%
}
\AtBeginDocument{\AtEndDocument{\numberofapp}}
\makeatletter

The script simply counts the number of \chapter&#8217;s started since the environment \appendix is called. [...]]]></description>
			<content:encoded><![CDATA[<p>This guide will show you how to automatically count the number of appendices in your LaTeX document.</p>
<p>First of all you need to place this code in the preamble of your document:</p>

<div class="wp_syntax"><div class="code"><pre class="latex" style="font-family:monospace;"><span style="color: #800000; font-weight: normal;">\<span style="color: #800000; font-weight: bold;">makeatletter</span></span>
<span style="color: #800000; font-weight: normal;">\newcounter</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">numapp</span><span style="color: #E02020; ">}</span>
<span style="color: #800000; font-weight: normal;">\newcommand</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #800000; font-weight: normal;">\numberofapp</span></span><span style="color: #E02020; ">}{</span><span style="color: #2C922C; font-style: italic;">%</span>
    <span style="color: #800000; font-weight: normal;">\immediate</span><span style="color: #800000; font-weight: normal;">\write</span><span style="color: #E00000; font-weight: normal;">\@auxout</span><span style="color: #2C922C; font-style: italic;">%</span>
      <span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #800000; font-weight: normal;">\string</span><span style="color: #800000; font-weight: normal;">\setcounter</span><span style="color: #E02020; ">{</span>numapp</span><span style="color: #E02020; ">}{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #800000; font-weight: normal;">\the</span><span style="color: #800000; font-weight: normal;">\c</span>@chapter</span><span style="color: #E02020; ">}}</span><span style="color: #2C922C; font-style: italic;">%</span>
<span style="color: #E02020; ">}</span>
<span style="color: #800000; font-weight: normal;">\AtBeginDocument</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #800000; font-weight: normal;">\AtEndDocument</span><span style="color: #E02020; ">{</span><span style="color: #800000; font-weight: normal;">\numberofapp</span></span><span style="color: #E02020; ">}}</span>
<span style="color: #800000; font-weight: normal;">\<span style="color: #800000; font-weight: bold;">makeatletter</span></span></pre></div></div>

<p>The script simply counts the number of <code>\chapter</code>&#8217;s started since the environment <code>\appendix</code> is called. The number of appendiced will then be placed in the variable <code>\numapp</code>, thus accesible throughout your document using <code>\thenumapp</code>.</p>
<p>Note: The script should run at least twice, before the variable will produce the correct number of appendices.</p>
]]></content:encoded>
			<wfw:commentRss>http://guides.ricehigh.dk/?feed=rss2&amp;p=48</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to get LaTeX on Linux to coorporate with LaTeX on Windows</title>
		<link>http://guides.ricehigh.dk/?p=36</link>
		<comments>http://guides.ricehigh.dk/?p=36#comments</comments>
		<pubDate>Mon, 24 Aug 2009 05:54:17 +0000</pubDate>
		<dc:creator>Ricehigh</dc:creator>
				<category><![CDATA[LaTeX guides]]></category>
		<category><![CDATA[LaTeX]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://guides.ricehigh.dk/?p=36</guid>
		<description><![CDATA[When writing in LaTeX it is frequently done in small or larger groups. These groups will typically access the LaTeX files via an SVN-like service, thus it is important that the LaTeX files is accesible and compilable on multi-platform. This guide should help you if you&#8217;re having trouble getting LaTeX on Linux to coorporate with [...]]]></description>
			<content:encoded><![CDATA[<p>When writing in LaTeX it is frequently done in small or larger groups. These groups will typically access the LaTeX files via an SVN-like service, thus it is important that the LaTeX files is accesible and compilable on multi-platform. This guide should help you if you&#8217;re having trouble getting LaTeX on Linux to coorporate with LaTeX on Windows.</p>
<p><strong>Starting point:</strong><br />
<em>Computer1:</em><br />
This is a computer with Windows Vista installed, that uses <em>MiKTeX</em> for LaTeX package sourses and compiler, <em>TeXnicCenter</em> as LaTeX editor, and uses a SVN-folder to share files with the group.</p>
<p><em>Computer2:</em><br />
This is a computer with Ubuntu 8.04 installed, that wants to be able to share and compile files within the SVN-folder.</p>
<p><strong>The solution:</strong><br />
All the setup is done on <em>Computer2</em>, the one with Ubuntu installed, because, frankly, there isn&#8217;t much configuration available in <em>TeXnicCenter</em> regarding encoding and such.</p>
<p>First of you need to install the following packages, and their dependencies: Texlive, Kile and Okular.<br />
<code>apt-get install texlive kile okular</code><br />
Remember, you need root access to install packages. In Ubuntu, you do that by:<br />
<code>sudo apt-get install texlive kile okular</code></p>
<p>Then you need to configure kile, a LaTeX editor for Linux, to use the same character encoding as TeXnicCenter:</p>
<p><em>Settings:</em><br />
Choose <code>Editor</code>, Chose <code>Open/Save</code> ,Set <code>Encoding</code> to  <code>Western European (iso  8859-1)</code>, Set <code>Encode auto</code> to <code>disabled</code>, Set <code>End of line</code> to <code>DOS/Windows</code></p>
<p>Now you should be able to open and save documents readable to both you and users of the SVN-folder.</p>
<p><em>Compiling:</em><br />
You can compile your LaTeX document by pressing the <code>LaTeX</code> in <em>Kile</em>, thus producing a dvi-file. Say you want to produce a pdf-file instead, just press <code>DVItoPDF</code> after you&#8217;ve pressed <code>LaTeX</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://guides.ricehigh.dk/?feed=rss2&amp;p=36</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Concept unit converter</title>
		<link>http://guides.ricehigh.dk/?p=25</link>
		<comments>http://guides.ricehigh.dk/?p=25#comments</comments>
		<pubDate>Thu, 12 Feb 2009 21:18:36 +0000</pubDate>
		<dc:creator>Ricehigh</dc:creator>
				<category><![CDATA[Micro guides]]></category>
		<category><![CDATA[casual scripts]]></category>
		<category><![CDATA[Micro guide]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://guides.ricehigh.dk/?p=25</guid>
		<description><![CDATA[Introduction
This is my fourth guide and is, like my prime generator, just some code I did for fun, while I was bored in front of my computer:)
So what are we going to do today? Well, we&#8217;re going to program a unit converter &#8220;Google-style&#8221;. What I wanted to do was to get a user to input [...]]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>This is my fourth guide and is, like my prime generator, just some code I did for fun, while I was bored in front of my computer:)<br />
So what are we going to do today? Well, we&#8217;re going to program a unit converter &#8220;Google-style&#8221;. What I wanted to do was to get a user to input a unit. For instance &#8220;4 pounds&#8221; and then the script would output how much this was in several other units. And because units like &#8220;pounds&#8221; can be addresses in many ways (like &#8220;lb&#8221;, &#8220;lbs&#8221;, &#8220;pound&#8221;, and so on) I wanted to make the script understand every single of them and use them as if the user had used the input &#8220;pounds&#8221;.</p>
<h3>The code</h3>

<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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Concept unit converter</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;You can use this script to convert from/to any of the most common units in mass, distance and speed&lt;br/&gt;Just enter, for instance: 13 km, and then the system will calculate how long 13 km is in other units.&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;form action=&quot;test.php&quot; method=&quot;get&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;q&quot; size=&quot;15&quot;/&gt;&lt;input type=&quot;submit&quot; value=&quot;submit&quot;/&gt;&lt;/form&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$input</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'q'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$number</span> <span style="color: #339933;">=</span> <span style="color: #990000;">floatval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$unit</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/[\s,.1234567890]+/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$original_unit</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$unit</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//Mass</span>
<span style="color: #000088;">$allowed</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;mass&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;kilogram&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;kg&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;kilograms&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;kg&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;kgs&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;kg&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;grams&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;g&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;gram&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;g&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;lb&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;lbs&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;pounds&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;lbs&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;pound&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;lbs&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;ounces&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;ounce&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;stones&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;stone&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$conv</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;mass&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;kg&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;lbs&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">2.204623</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;g&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1000</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;stone&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">0.157473</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;ounce&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">35.27</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Distance</span>
<span style="color: #000088;">$allowed</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;distance&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;meter&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;m&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;meters&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;m&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;kilometer&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;km&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;kilometers&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;km&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;inch&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;in&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;inches&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;in&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;mile&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;mi&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;miles&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;mi&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;yard&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;yd&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;yards&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;yd&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$conv</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;distance&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;m&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;km&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">0.001</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;in&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">39.37007874</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;mi&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">0.0006213712</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;yd&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">1.093613</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Speed / velocity</span>
<span style="color: #000088;">$allowed</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;speed&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;kmh&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;km/h&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;mps&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;m/s&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;kms&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;km/s&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$conv</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;speed&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;km/h&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;mph&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">0.6213712</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;m/s&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">0.2777778</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;km/s&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">0.0002777778</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$types</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mass&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;distance&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;speed&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$types</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$type</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: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$unit</span><span style="color: #339933;">,</span><span style="color: #000088;">$allowed</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$type</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$unit</span><span style="color: #339933;">,</span><span style="color: #000088;">$conv</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$type</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</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: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$unit</span><span style="color: #339933;">,</span><span style="color: #000088;">$allowed</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$type</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #000088;">$unit</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$allowed</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$type</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$unit</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;table&gt;&lt;tr&gt;&lt;td style=<span style="color: #000099; font-weight: bold;">\&quot;</span>text-align:right;<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;b&gt;&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">number_format</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt; &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$original_unit</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$number</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$number</span><span style="color: #339933;">/</span><span style="color: #000088;">$conv</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$type</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$unit</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$conv</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$type</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$unit_conv</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$conv_ratio</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: #000088;">$unit</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$unit_conv</span><span style="color: #009900;">&#41;</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;tr&gt;&lt;td style=<span style="color: #000099; font-weight: bold;">\&quot;</span>text-align:right;<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">number_format</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span><span style="color: #339933;">*</span><span style="color: #000088;">$conv_ratio</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/td&gt; &lt;td&gt;<span style="color: #006699; font-weight: bold;">$unit_conv</span>&lt;/td&gt;&lt;/tr&gt;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;/table&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>It is clear from the code that the array $allowed[] specifies what terms are allowed in the script and it&#8217;s a translation table to the correct unit (&#8221;the correct unit&#8221; is specified by you). The array $conv[] contains the conversion ratio. As you see from the code the conversion ratio is always relative to the first unit in $conv[].</p>
<p>Good luck converting:D</p>
<p><strong>Important note:</strong> This script can only be used for units with a specific ratios between them. This means that it for instance cannot be used for converting Celsius to Fahrenheit and vice versa.</p>
]]></content:encoded>
			<wfw:commentRss>http://guides.ricehigh.dk/?feed=rss2&amp;p=25</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cookie security</title>
		<link>http://guides.ricehigh.dk/?p=7</link>
		<comments>http://guides.ricehigh.dk/?p=7#comments</comments>
		<pubDate>Mon, 02 Feb 2009 18:21:17 +0000</pubDate>
		<dc:creator>Ricehigh</dc:creator>
				<category><![CDATA[Security]]></category>
		<category><![CDATA[Cookies]]></category>
		<category><![CDATA[hashing]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://guides.ricehigh.dk/?p=7</guid>
		<description><![CDATA[Introduction
When using cookies for login systems it&#8217;s very common to store the username and a hashed password. This method is nothing near ideal, since cookie theft is a very common phenomenon, whereby a potential hacker will be able to brute force your user&#8217;s password. What you want to do instead is creating a cookie containing [...]]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>When using cookies for login systems it&#8217;s very common to store the username and a hashed password. This method is nothing near ideal, since <a href="http://en.wikipedia.org/wiki/HTTP_cookie#Cookie_hijacking">cookie theft</a> is a very common phenomenon, whereby a potential hacker will be able to brute force your user&#8217;s password. What you want to do instead is creating a cookie containing 3 values: The username, a long random string and an expirery date. By putting the cookie information in a database as well, we can determine the cookies validity.</p>
<h3>How to do it</h3>
<p><em>NOTE: This guide presumes that you&#8217;ve already got a login system and the variables <code>$username</code> and <code>$id</code> is respectively the username and user id in your userdatabase</em></p>
<p>First you need to create a MySQL database table with the following data:</p>
<ul>
<li>id, int(11), unsigned, auto_increment, primary key</li>
<li>user, varchar(50)</li>
<li>random, varchar(64)</li>
<li>expire, varchar(40)</li>
<li>uid, int(11)</li>
</ul>
<p>When you have authorized the user&#8217;s login data, you will generate the cookie information somewhat like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$random</span> <span style="color: #339933;">=</span> <span style="color: #990000;">hash</span><span style="color: #009900;">&#40;</span>sha256<span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">uniqid</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">uniqid</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$expire</span> <span style="color: #339933;">=</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: #cc66cc;">60</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">24</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">30</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// current time + expirery time (here: 30 days)</span>
<span style="color: #000088;">$name</span>   <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;login&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Name of the cookie</span>
<span style="color: #000088;">$value</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$username</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #006699; font-weight: bold;">$random</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #006699; font-weight: bold;">$expire</span>&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Value of the cookie</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO `cookie_user_db` (`id`,`user`,`random`,`expire`,`uid`) VALUES (NULL, '<span style="color: #006699; font-weight: bold;">$username</span>', '<span style="color: #006699; font-weight: bold;">$random</span>', '<span style="color: #006699; font-weight: bold;">$expire</span>', '<span style="color: #006699; font-weight: bold;">$id_db</span>')&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$path</span>   <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// The cookie will work in all folders on present domain.</span>
<span style="color: #990000;">setcookie</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #339933;">,</span> <span style="color: #000088;">$expire</span><span style="color: #339933;">,</span> <span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In the code above it&#8217;s presumed that the <code>$username</code> string has already been properly evaluated, to prevent from mysql-injection attacks. See more <a href="http://dk.php.net/manual/en/function.mysql-real-escape-string.php">here</a>.</p>
<p>Now we have a cookie with our wanted data, and a copy of those data in our database.</p>
<p>When a user, who is not logged in, comes to your site, who has a cookie, we want to determine wether the given data is valid:</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'login'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$info</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'\n'</span><span style="color: #339933;">,</span><span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'login'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Devides the cookie in it's seperable values: 1) Username, 2) A unique random number and 3) the expirery date for the cookie.</span>
	<span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$info</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// mysql_escape_string is used to prevent mysql injection attacks.</span>
	<span style="color: #000088;">$random</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$info</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$expire</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$info</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</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: #000088;">$username</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span> <span style="color: #339933;">==</span> <span style="color: #990000;">htmlentities</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$random</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$expire</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// Checks if a username is present and wether the username holds special characters.</span>
		<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM `cookie_user_db` WHERE `user` = '<span style="color: #006699; font-weight: bold;">$username</span>' AND `random` = '<span style="color: #006699; font-weight: bold;">$random</span>' AND `expire` = '<span style="color: #006699; font-weight: bold;">$expire</span>'&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</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: #990000;">mysql_num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</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: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$r</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_fetch_array</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: #000088;">$username_db</span><span style="color: #339933;">=</span> <span style="color: #990000;">mb_strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$r</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$random_db</span><span style="color: #339933;">=</span><span style="color: #000088;">$r</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;random&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$expire_db</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$r</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;expire&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$uid_db</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$r</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;uid&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">//checks wether the supplied user exists in the user database</span>
			<span style="color: #000088;">$result</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 * FROM `<span style="color: #006699; font-weight: bold;">$user_db</span>` WHERE `user` = '<span style="color: #006699; font-weight: bold;">$username</span>' AND `id` = '<span style="color: #006699; font-weight: bold;">$uid_db</span>'&quot;</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: #990000;">mysql_num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</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: #666666; font-style: italic;">// At the basis the user isn't allowed to login:</span>
				<span style="color: #000088;">$login</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$expire_db</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$expire</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$expire</span> <span style="color: #339933;">&gt;</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</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;">// Checks wether 1) the expirery date of the database matches the one supplied in the cookie and 2) wether the expirery date is still valid.</span>
					<span style="color: #666666; font-style: italic;">// Gives $login a true value</span>
					<span style="color: #000088;">$login</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
&nbsp;
				<span style="color: #666666; font-style: italic;">// Checks wether $login is true</span>
				<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$login</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #666666; font-style: italic;">// Session variable &quot;access&quot; is given a true value.</span>
					<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'access'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #666666; font-style: italic;">// The user is sent to the administration page.</span>
					<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Location: admin_website.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #990000;">exit</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #666666; font-style: italic;">//If $login is not true, the user is sent back to the login page.</span>
				<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;login.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #990000;">exit</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">// If no cookie is found, the user is sent back to the login page</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;login.php&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>

]]></content:encoded>
			<wfw:commentRss>http://guides.ricehigh.dk/?feed=rss2&amp;p=7</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>How to generate primes</title>
		<link>http://guides.ricehigh.dk/?p=14</link>
		<comments>http://guides.ricehigh.dk/?p=14#comments</comments>
		<pubDate>Mon, 02 Feb 2009 17:59:38 +0000</pubDate>
		<dc:creator>Ricehigh</dc:creator>
				<category><![CDATA[Micro guides]]></category>
		<category><![CDATA[casual scripts]]></category>
		<category><![CDATA[Micro guide]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Prime numbers]]></category>

		<guid isPermaLink="false">http://guides.ricehigh.dk/?p=14</guid>
		<description><![CDATA[Introduction
Here&#8217;s a micro guide on how to generate prime numbers in a given interval with PHP.
A prime number is a natural number (a positive integer) which has exactly two distinct natural number divisors (1 and the number itself).
The Code
What we want to do is to create a function that takes a minimum and a maximum [...]]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>Here&#8217;s a micro guide on how to generate prime numbers in a given interval with PHP.</p>
<p>A prime number is a natural number (a positive integer) which has exactly two distinct natural number divisors (1 and the number itself).</p>
<h3>The Code</h3>
<p>What we want to do is to create a function that takes a minimum and a maximum value of primes values we want to find. Then we try to divide the number by every number bigger than 1 and smaller than itself and determine if it has a remainder on any of the calculation. If it has on every caltulation we&#8217;ve got a prime, if not, we don&#8217;t.<br />
I&#8217;ve made the script to only use odd numbers, because 2 is the only non-odd prime.</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> prime_generator<span style="color: #009900;">&#40;</span><span style="color: #000088;">$from</span><span style="color: #339933;">,</span><span style="color: #000088;">$to</span><span style="color: #339933;">,</span><span style="color: #000088;">$return</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">false</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: #339933;">!</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$from</span><span style="color: #339933;">%</span><span style="color:#800080;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #000088;">$from</span><span style="color: #339933;">++;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$from</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$from</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$return</span><span style="color: #009900;">&#41;</span>
			<span style="color: #000088;">$primes</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;2, &quot;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">else</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;2, &quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">=</span><span style="color: #000088;">$from</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$to</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">+=</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$prime</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$y</span><span style="color: #339933;">=</span><span style="color: #000088;">$x</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><span style="color: #000088;">$y</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><span style="color: #000088;">$y</span><span style="color: #339933;">--</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: #339933;">!</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">%</span><span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$prime</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$prime</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: #000088;">$return</span><span style="color: #009900;">&#41;</span>
				<span style="color: #000088;">$primes</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;, &quot;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">else</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;, &quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$return</span><span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$primes</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$primes</span> <span style="color: #339933;">=</span> prime_generator<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$primes</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>You can use the function either with or without the <code>$return</code> parameter set. If it&#8217;s set to true it will return all primes in the given interval, if not, it gives the output right away.</p>
]]></content:encoded>
			<wfw:commentRss>http://guides.ricehigh.dk/?feed=rss2&amp;p=14</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Password hashing</title>
		<link>http://guides.ricehigh.dk/?p=1</link>
		<comments>http://guides.ricehigh.dk/?p=1#comments</comments>
		<pubDate>Sun, 18 Jan 2009 14:54:53 +0000</pubDate>
		<dc:creator>Ricehigh</dc:creator>
				<category><![CDATA[Security]]></category>
		<category><![CDATA[hashing]]></category>
		<category><![CDATA[MD5]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SHA]]></category>

		<guid isPermaLink="false">http://guides.ricehigh.dk/?p=1</guid>
		<description><![CDATA[Introduction
One of the most widely used password hashing methods is MD5 (using md5("password")). Although, It&#8217;s been discovered that MD5-sum hashing is insecure because of the fairly simple method of finding collisions (as described by Wang et al. in 2004) and recently discovery that MD5-sum hashing brings insecurity to https sites (as described by Sotirov et [...]]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>One of the most widely used password hashing methods is MD5 (using <code>md5("password")</code>). Although, It&#8217;s been discovered that MD5-sum hashing is insecure because of the fairly simple method of finding collisions (as described by <a href="http://web.archive.org/web/20070604205756/http://www.infosec.sdu.edu.cn/paper/md5-attack.pdf">Wang et al. in 2004</a>) and recently discovery that MD5-sum hashing brings insecurity to https sites (as described by <a href="http://www.win.tue.nl/hashclash/rogue-ca/">Sotirov et al. in 2007</a>).<br />
This MD5 insecurity made me want to create a password hashing system that uses a modern hash algorithm and a modern way of thinking. So I sat down and began to read about internet security and I came up with 3 significant improvements compared to the <code>md5("password")</code> method:</p>
<ol>
<li>SHA-256 hashing, instead of MD5.</li>
<li>Adding a random &#8217;salt&#8217; to the password.</li>
<li>Adding a fixed &#8217;salt&#8217; to the password</li>
</ol>
<h3>Salts</h3>
<p>A salt is a string that is appended to the password to make the more difficult to <a href="http://en.wikipedia.org/wiki/Brute_force_attack">brute force</a> the hash. Adding a salt also makes the use of rainbow tables impossible.<br />
When appending a random salt to the password we ensure that two equal passwords is completely different in the database, whereby a potential hacker can&#8217;t brute force more than one user at a time &#8211; he has to brute force every password by itself.<br />
When adding a fixed salt we add a significantly long string to the password, making the hash even more difficult and time demanding to brute force because:</p>
<ul>
<li>It&#8217;s highly unlikely that a hacker grants access to both the database (containing the hash) and the php-file (containing the fixed salt)</li>
<li>Even if a hacker finds out fixed salt string; it will take more computer power to brute force and thereby more time.</li>
</ul>
<p>So how do we do this is practice? First of all we have to ensure to store the random hash, so we&#8217;re able to compare a user input password with the hashed one in the database. This can be done like this (this example will be a simplified version of the final outcome):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$RandomSalt</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">sha1</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">uniqid</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">12</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$hash</span> <span style="color: #339933;">=</span> <span style="color: #990000;">hash</span><span style="color: #009900;">&#40;</span>sha256<span style="color: #339933;">,</span> <span style="color: #000088;">$RandomSalt</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$plain_text_password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$final_hash</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$RandomSalt</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$hash</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><code>substr</code> cut the string from the second parameter to the third parameter; in this case it takes characters 0 to 12 in our generated string.<br />
We generate the random string by taking the SHA-1 hash of a 23 character long unique ID created from the build-in PHP-function <code>uniqid</code> with the second parameter set to <code>true</code> to give us more entropy.<br />
In the last step we append the random salt to the final hash value. This way we can retrieve it for comparison.</p>
<p>Now we want to append a fixed salt to the password hash as well:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'FIXED_SALT'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'@ywaE~H*JSA}7w2I||t%E%ywb}&lt;]Y-I='</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$hash</span> <span style="color: #339933;">=</span> <span style="color: #990000;">hash</span><span style="color: #009900;">&#40;</span>sha256<span style="color: #339933;">,</span> <span style="color: #000088;">$RandomSalt</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$plain_text_password</span> <span style="color: #339933;">.</span> FIXED_SALT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$final_hash</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$RandomSalt</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$hash</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The fixed salt in this example is 32 characters long but could be any length, although the longer the salt is, the more time demanding it will be to brute force the hash.</p>
<p>I&#8217;ve created a function to randomly create a fixed salt with variable length. In the example below will output a 32 character long random string:</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="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> generateRandSalt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$salt_length</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;define('FIXED_SALT', '&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><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;">$x</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$salt_length</span><span style="color: #339933;">;</span><span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$rnd_nr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">33</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">126</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rnd_nr</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">34</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$rnd_nr</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">39</span><span style="color: #009900;">&#41;</span>
			<span style="color: #000088;">$rnd_nr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">33</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">126</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$string</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">htmlentities</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rnd_nr</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000088;">$string</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;');&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$string</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> generateRandSalt<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br/&gt;&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h3>Hash functions</h3>
<p>This example uses the hash function SHA-256, which creates a 64 character long hash string. If you want to save space in your database you can use the SHA-1 hash function instead. You should always use a known hashing system instead of trying to make your own, trying to fool a potential hacker. &#8220;Security through obscurity&#8221; is a widely used term in this industry &#8211; and is widely advised against. The term covers the type of security that is based upon making something weird or obscure. This might seem like a good protection, but if you&#8217;re not a mathematical genius it&#8217;s often easy to analyse how you created your security, and then it&#8217;s normally already broken.</p>
<p>Approximate lifetime left for some known hashing algorithms:<br />
MD5 is completely dead.<br />
SHA-1 is expected to die within 1-3 years.<br />
SHA-256 is expected to have a lifetime of more than 20 years.</p>
<h3>The code</h3>
<p>The final code is presented below. It&#8217;s advised that you change the random salt length and you don&#8217;t use the same fixed salt as in the example below.<br />
Remember: the longer you make the random salt, the more space your hash will take up in your database, since the random salt is appended to the hash.</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SALT_LENGTH'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">24</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'FIXED_SALT'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'q*x&lt;D:_I7c:IG~O]B5Tv&amp;}-V|DOM(~@z'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> generateHash<span style="color: #009900;">&#40;</span><span style="color: #000088;">$plainText</span><span style="color: #339933;">,</span> <span style="color: #000088;">$salt</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// The salt variable is set to NULL if no parameter is set.</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$salt</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// triple equal signs means &quot;defined as&quot; and is used instead of the comparison (double equal signs) for the instance that the given salt is either zero (0) or an empty string (&quot;&quot;).</span>
		<span style="color: #666666; font-style: italic;">// Generates a random string with the defined salt length.</span>
		<span style="color: #666666; font-style: italic;">// Two random functions are used to increase the number of possible outcomes.</span>
		<span style="color: #000088;">$salt</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">hash</span><span style="color: #009900;">&#40;</span>sha256<span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">uniqid</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">uniqid</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> SALT_LENGTH<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: #666666; font-style: italic;">// Uses the supplied string. If the string is too long, it will be shortened to the defined salt length.</span>
		<span style="color: #000088;">$salt</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$salt</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> SALT_LENGTH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #666666; font-style: italic;">// The hash is produced in a matter, so the random salt is appended to the password and the static salt is appended. The final hash is produced by using the SHA-256 algorithm.</span>
	<span style="color: #000088;">$hash</span> <span style="color: #339933;">=</span> <span style="color: #990000;">hash</span><span style="color: #009900;">&#40;</span>sha256<span style="color: #339933;">,</span><span style="color: #000088;">$salt</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$plainText</span> <span style="color: #339933;">.</span> SECURE_SALT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$salt</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$hash</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Returns the hashed string with the random salt appended to it</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$hash</span> <span style="color: #339933;">=</span> generateHash<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Calls the generateHash function and stores it in the $hash variable.</span>
<span style="color: #000088;">$deHash</span> <span style="color: #339933;">=</span> generateHash<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$hash</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// hashes again using the same random hash as in $hash.</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$hash</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br/&gt;&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$deHash</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>To generate a password you call the function like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">generateHash<span style="color: #009900;">&#40;</span><span style="color: #000088;">$plain_text_password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>To compare an input password with the one in the database use:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>generateHash<span style="color: #009900;">&#40;</span><span style="color: #000088;">$input_password</span><span style="color: #339933;">,</span><span style="color: #000088;">$database_password</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$database_password</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// input password is correct</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// input password is incorrect.</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>NOTE:</strong> No matter which way you hash your passwords it will always be possible to brute force the hash. The measures described in this guide are merely to give a potential hacker a great disadvantage in his brute force attempt. Even with a cluster of 1000 computers it would probably take a month to brute force a single hash.<br />
Furthermore it&#8217;s strongly advised to demand a certain password strength from the user. This way it&#8217;s much more difficult for a potential hacker to guess the password on the login form. Also, it&#8217;s advised that you only allow a certain login attempts from the same computer in a specified timeframe. For instance 5 login attempt per hour.</p>
<p>The source file is available for download: <strong><a href="files/password.php">Click here to download the code </a></strong></p>
<h3>Further Improvements</h3>
<p>To make the hash even more time demanding to brute force, you could loop the hashing several times. This practice improves the code in two ways:</p>
<ol>
<li>A potential hacker doesn&#8217;t know how many times the hash function loops.</li>
<li>If the hacker finds out how many times the hash loops, he still need to hash several times for each brute force attempt.</li>
</ol>
<p>I&#8217;ve written an example:</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> generateHash<span style="color: #009900;">&#40;</span><span style="color: #000088;">$plainText</span><span style="color: #339933;">,</span> <span style="color: #000088;">$salt</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</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: #000088;">$salt</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$salt</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">hash</span><span style="color: #009900;">&#40;</span>sha256<span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">uniqid</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">uniqid</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> SALT_LENGTH<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: #000088;">$salt</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$salt</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> SALT_LENGTH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000088;">$hash</span> <span style="color: #339933;">=</span> <span style="color: #990000;">hash</span><span style="color: #009900;">&#40;</span>sha256<span style="color: #339933;">,</span><span style="color: #000088;">$salt</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$plainText</span> <span style="color: #339933;">.</span> SECURE_SALT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><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;">$x</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">4500</span><span style="color: #339933;">;</span><span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$hash</span> <span style="color: #339933;">=</span> <span style="color: #990000;">hash</span><span style="color: #009900;">&#40;</span>sha256<span style="color: #339933;">,</span><span style="color: #000088;">$salt</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$hash</span> <span style="color: #339933;">.</span> SECURE_SALT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$salt</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$hash</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>NOTE:</strong> this solution can be very server power demanding, for which reason it shouldn&#8217;t be used, at least not with 4500 loops, if your site has many login attempts during short timeframes.</p>
]]></content:encoded>
			<wfw:commentRss>http://guides.ricehigh.dk/?feed=rss2&amp;p=1</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
