<?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>duckranger.com</title>
	<atom:link href="http://duckranger.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://duckranger.com</link>
	<description>JUST DOING IT</description>
	<lastBuildDate>Thu, 12 Aug 2010 00:58:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Configure a custom 404 page in Glassfish</title>
		<link>http://duckranger.com/2010/08/configure-a-custom-404-page-in-glassfish/</link>
		<comments>http://duckranger.com/2010/08/configure-a-custom-404-page-in-glassfish/#comments</comments>
		<pubDate>Thu, 12 Aug 2010 00:58:07 +0000</pubDate>
		<dc:creator>Duck Ranger</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Glassfish]]></category>
		<category><![CDATA[web.xml]]></category>

		<guid isPermaLink="false">http://duckranger.com/?p=475</guid>
		<description><![CDATA[Setting up a custom 404 error page serves two purposes: It allows you to present your users a nice message rather than the ugly, server-generated default page. It serves to mask the server&#8217;s identity &#8211; so, security is a little better. The first point is...]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/hippiebus1970/272903685/" class="tn"><img class="alignright" src="http://farm1.static.flickr.com/114/272903685_f836d953f5_z.jpg"  alt="It's an attack" width="250" height="187" /></a><br />
Setting up a custom 404 error page serves two purposes:</p>
<ol>
<li>It allows you to present your users a nice message rather than the ugly, server-generated default page.</li>
<li>It serves to mask the server&#8217;s identity &#8211; so, security is a little better.</li>
</ol>
<p>The first point is obvious. But the second point may need some clarification &#8211; why do we care about the server&#8217;s identity?<br />
The reason is &#8211; when a potential attacker knows which server your application is running on, they can try specifically crafted attacks that take advantage of the specific system&#8217;s vulnerabilities.<br />
<span id="more-475"></span><br />
If you do not configure Glassfish is especially bad in terms of the second point, since it displays a big message saying &#8220;I am glassfish!&#8221; when the user tries to access a page that does not exist.<br />
Now, you may think &#8211; why is that bad?<br />
Well, there are a few Glassfish vulnerabilities out there. Have a look at these, for example:</p>
<ol>
<li><a href="http://www.securityfocus.com/archive/1/503236">XSS vulnerability in the admin console</a></li>
<li><a href="http://securitytracker.com/alerts/2010/Jan/1023428.html">SSL Man In the Middle attack vector</a></li>
</ol>
<p>Convinced?<br />
Well.. how about setting custom error pages? So &#8211; instead of getting something like this:</p>
<p><a href="http://duckranger.com/wp-content/uploads/2010/08/404.png"><img src="http://duckranger.com/wp-content/uploads/2010/08/404-300x125.png" alt="Glassfish default 404 error page" title="404" width="300" height="125" class="aligncenter size-medium wp-image-477" /></a><br />
which tells the world what application server you&#8217;re using, you get your own error page?</p>
<p>This is quite easy to do. The error pages configuration is found inside domain.xml and you can either edit it manually or use asadmin on the command line.</p>
<p>To edit domain.xml manually, open the file. It should be in your application server&#8217;s home directory, under your domain&#8217;s config folder. So something like</p>
<div class="codecolorer-container dos mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="dos codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">c:\glassfish\domains\mydomain\config\domain.xml</div></div>
<p>Inside, find the part describing your server. It should resemble the following:</p>
<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;virtual-server</span> <span style="color: #000066;">hosts</span>=<span style="color: #ff0000;">&quot;${com.sun.aas.hostName}&quot;</span> <span style="color: #000066;">http-listeners</span>=<span style="color: #ff0000;">&quot;http-listener-1,http-listener-2&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;server&quot;</span> &nbsp;<span style="color: #000066;">log-file</span>=<span style="color: #ff0000;">&quot;${com.sun.aas.instanceRoot}/logs/server.log&quot;</span> <span style="color: #000066;">state</span>=<span style="color: #ff0000;">&quot;on&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;docroot&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;${com.sun.aas.instanceRoot}/docroot&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;accesslog&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;${com.sun.aas.instanceRoot}/logs/access&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;sso-enabled&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/virtual-server<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>Inside, the <b>virtual-server</b> element, you can add properties for your error pages. You need to provide the path to the page you want displayed, the reason code, and a name, like so:</p>
<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;virtual-server</span> <span style="color: #000066;">hosts</span>=<span style="color: #ff0000;">&quot;${com.sun.aas.hostName}&quot;</span> <span style="color: #000066;">http-listeners</span>=<span style="color: #ff0000;">&quot;http-listener-1,http-listener-2&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;server&quot;</span> &nbsp;<span style="color: #000066;">log-file</span>=<span style="color: #ff0000;">&quot;${com.sun.aas.instanceRoot}/logs/server.log&quot;</span> <span style="color: #000066;">state</span>=<span style="color: #ff0000;">&quot;on&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;docroot&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;${com.sun.aas.instanceRoot}/docroot&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;accesslog&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;${com.sun.aas.instanceRoot}/logs/access&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;sso-enabled&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;send-error_1&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;path=../applications/j2ee-apps/myapplicationcontext/myapplication_war/404.html reason=Resource_not_found code=404&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/virtual-server<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>The name is just a unique name. If you need to add more error pages &#8211; just increase the number with each one.<br />
The value attribute contains three parameters: the path to the HTML page you want to display when this error occurs, the reason, and the error code.<br />
Based on the error code you provide, Glassfish knows which page to display.</p>
<p>The path to the error page is relative to your domain&#8217;s config folder. In the example above, I want Glassfish to serve a 404.html page that&#8217;s found inside the war file of an application deployed to this domain. You can, however, point it anywhere.</p>
<p>To configure other error pages (e.g. HTTP502 or HTTP503 &#8211; just add another property (remember to change the name attribute though).</p>
<h3>Configure application specific error page</h3>
<p>The above configuration works well for a Glassfish-wide 404 error page. This is good as a fallback plan, and is probably what you want displayed when the user got the context wrong.<br />
So, for example, let&#8217;s say your application is deployed to http://example.com/app1. The user entered http://example.com/app. In this case, Glassfish&#8217;s 404 page will be displayed, and if you configured it as above &#8211; it will not offer information about your server.</p>
<p>However, you might want a different page to be served if the user tried to navigate to a bad URL, but got the context right. So, let&#8217;s say that you have the following page in your application: http://example.com/app1/win.xhtml.<br />
The user, somehow, managed to try and get http://example.com/app1/wins.xhtml &#8211; which is not valid in your application.<br />
The configuration above would take care of it, but if you want a specific 404 page for your application, you can still do it &#8211; by using your application&#8217;s web.xml:</p>
<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;web-app</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee&quot;</span> <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.5&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;error-page<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;error-code<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>404<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/error-code<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;location<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/404.html<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/location<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/error-page<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; ..</div></div>
<p>The location is relative to your application&#8217;s web root.<br />
The configurations in the web.xml snippet above and the domain.xml before &#8211; point to the same page, but they can point to different locations, thereby allowing you to have an application-specific error page, and a site-wide fallback one as well.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://duckranger.com/2010/08/configure-a-custom-404-page-in-glassfish/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://duckranger.com/2010/08/configure-a-custom-404-page-in-glassfish/&amp;title=Configure+a+custom+404+page+in+Glassfish" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://duckranger.com/2010/08/configure-a-custom-404-page-in-glassfish/&amp;title=Configure+a+custom+404+page+in+Glassfish" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://duckranger.com/2010/08/configure-a-custom-404-page-in-glassfish/&amp;title=Configure+a+custom+404+page+in+Glassfish&amp;desc=%0D%0ASetting%20up%20a%20custom%20404%20error%20page%20serves%20two%20purposes%3A%0D%0A%0D%0A%09It%20allows%20you%20to%20present%20your%20users%20a%20nice%20message%20rather%20than%20the%20ugly%2C%20server-generated%20default%20page.%0D%0A%09It%20serves%20to%20mask%20the%20server%27s%20identity%20-%20so%2C%20security%20is%20a%20little%20better.%0D%0A%0D%0AThe%20first%20point%20is%20obvious.%20But%20the%20second%20point%20may%20n" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.dzone.com/links/add.html?url=http://duckranger.com/2010/08/configure-a-custom-404-page-in-glassfish/&amp;title=Configure+a+custom+404+page+in+Glassfish&amp;description=%0D%0ASetting%20up%20a%20custom%20404%20error%20page%20serves%20two%20purposes%3A%0D%0A%0D%0A%09It%20allows%20you%20to%20present%20your%20users%20a%20nice%20message%20rather%20than%20the%20ugly%2C%20server-generated%20default%20page.%0D%0A%09It%20serves%20to%20mask%20the%20server%27s%20identity%20-%20so%2C%20security%20is%20a%20little%20better.%0D%0A%0D%0AThe%20first%20point%20is%20obvious.%20But%20the%20second%20point%20may%20n" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://duckranger.com/2010/08/configure-a-custom-404-page-in-glassfish/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://duckranger.com/2010/08/configure-a-custom-404-page-in-glassfish/&amp;bm_description=Configure+a+custom+404+page+in+Glassfish&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://duckranger.com/2010/08/configure-a-custom-404-page-in-glassfish/&amp;title=Configure+a+custom+404+page+in+Glassfish" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://duckranger.com/2010/08/configure-a-custom-404-page-in-glassfish/&amp;title=Configure+a+custom+404+page+in+Glassfish" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://duckranger.com/2010/08/configure-a-custom-404-page-in-glassfish/&amp;title=Configure+a+custom+404+page+in+Glassfish" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://duckranger.com/2010/08/configure-a-custom-404-page-in-glassfish/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Configure+a+custom+404+page+in+Glassfish+-+http://b2l.me/agp3cb&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

<img src="http://duckranger.com/?ak_action=api_record_view&id=475&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://duckranger.com/2010/08/configure-a-custom-404-page-in-glassfish/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Securing your Seam application cookies</title>
		<link>http://duckranger.com/2010/08/securing-your-seam-application-cookies/</link>
		<comments>http://duckranger.com/2010/08/securing-your-seam-application-cookies/#comments</comments>
		<pubDate>Mon, 02 Aug 2010 23:09:48 +0000</pubDate>
		<dc:creator>Duck Ranger</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Seam]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Cookies]]></category>
		<category><![CDATA[Glassfish]]></category>
		<category><![CDATA[JSESSIONID]]></category>
		<category><![CDATA[Servlet Filter]]></category>

		<guid isPermaLink="false">http://duckranger.com/?p=465</guid>
		<description><![CDATA[Seam is quite a good and secure framework as far as frameworks go. However, with a simple ServletFilter, it can be made just a little bit more secure, and maybe help you pass that next audit. In this post you&#8217;ll see how to define a...]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/26633972@N06/" class="tn"><img class="alignright" src="http://farm4.static.flickr.com/3042/2499433167_28648f8e7b.jpg"  alt="It's an attack" width="250" height="166" /></a><br />
Seam is quite a good and secure framework as far as frameworks go. However, with a simple ServletFilter, it can be made just a little bit more secure, and maybe help you pass that next audit. In this post you&#8217;ll see how to define a Seam servlet filter to secure your website just a little bit more.<br />
<span id="more-465"></span><br />
The first things any good penetration testing is going to look for are your cookie characteristics. There are simple reasons for that: it&#8217;s easy, and many applications get them wrong. They are:</p>
<ol>
<li>Secure flag</li>
<li>HttpOnly flag</li>
<li>Post-login session key regeneration</li>
</ol>
<h3>The cookie&#8217;s Secure flag</h3>
<p>The secure flag is telling the browser to only send this cookie over secure (SSL) channels. This is irrelevant if your application is not SSL-secured (but penetration testing is rarely done on these anyway).<br />
There&#8217;s no way for Seam to automatically know whether your application is deployed on an SSL-protected server or not. Therefore you need to take care of it yourself.<br />
Normally, you would configure this in the container&#8217;s configuration, but this isn&#8217;t always possible, as not all containers support this, especially if your container does not use Servlet 3.0 specification.<br />
The Servlet Filter below adds the <b>secure</b> flag to your cookie, and does it smartly by checking first to see if you are using a secure connection. This is good for applications that may be deployed either way (e.g. when your development environment isn&#8217;t SSL-protected).</p>
<h3>The cookie&#8217;s HttpOnly flag</h3>
<p>Setting the <b>HttpOnly</b> flag on a cookie helps protect against some forms of attacks related to cookie stealing. This flag was introduced by (surprise surprise!) Microsoft with Internet Explorer 6, to prevent a vulnerability where an attacker could access your cookies directly, using Javascript, and send them over to their website.<br />
This recommendation is a favourite with security consultants, and you&#8217;re bound to have that raised if you don&#8217;t set the flag on your cookies. I personally believe that it&#8217;s a low priority issue (see <a href="http://blog.skeptikal.org/2010/04/5-reasons-httponly-wont-save-you.html">here</a> for a short discussion) &#8211; but it&#8217;s quite easy to do, so why not.<br />
Again, this setting should really be performed in the container, but if it doesn&#8217;t support Servlet 3.0 you may have to revert to using the Servlet Filter.</p>
<h3>Post-login session key regeneration</h3>
<p>This is quite a good one. See, when the user first lands on your website, your Seam application (via the container) sets a cookie on their computer, with a <b>JSESSIONID</b> parameter. This is the session id all JEE containers use to track user sessions in the cookie.<br />
By default, after the user logs in successfully, this session id remains the same. Hence, if an attacker can get hold of this session id &#8211; they can then create a cookie on their computer that has the same session id, and use it as an authenticated user. This attack is called session fixation.<br />
Obviously, this is not a very good vulnerability to have in your website, and the Servlet Filter below takes care of it by regenerating the session id after a successful login.</p>
<h3>The Servlet Filter</h3>
<p>In Seam, it is very easy to add a Servlet Filter to your application without touching any xml. In essence, the filter becomes a Seam component.<br />
You can do everything using Seam&#8217;s annotations like so:</p>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@Scope<span style="color: #009900;">&#40;</span> ScopeType.<span style="color: #006633;">APPLICATION</span> <span style="color: #009900;">&#41;</span> <br />
@<span style="color: #003399;">Name</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;cookieFilter&quot;</span> <span style="color: #009900;">&#41;</span><br />
@Install<span style="color: #009900;">&#40;</span> precedence <span style="color: #339933;">=</span> Install.<span style="color: #006633;">FRAMEWORK</span> <span style="color: #009900;">&#41;</span><br />
@BypassInterceptors<br />
@Filter<span style="color: #009900;">&#40;</span> within <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;org.jboss.seam.web.rewriteFilter&quot;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CookieFilter <span style="color: #000000; font-weight: bold;">extends</span> AbstractFilter <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; .<br />
&nbsp; &nbsp; .<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The two important annotations here are the @BypassInterceptors &#8211; which tells Seam to not apply any of its interceptors to this component. This is important because the Servelt Filter is sort of a pseudo-component that acts like an interceptor itself.<br />
The second important annotation is the @Filter &#8211; telling Seam where this filter needs to go in the filter chain. For this filter, the right place is within Seam&#8217;s rewriteFilter, because the main action here entails rewriting the HTTP response.</p>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> SET_COOKIE <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SET-COOKIE&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> SECURE_FLAG <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;; Secure&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> JSESSION_ATTR <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;JSESSIONID=&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> PATH_ATTR <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;; Path=&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> HTTPONLY_FLAG <span style="color: #339933;">=</span> &nbsp;<span style="color: #0000ff;">&quot;; HttpOnly&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> POST_LOGON_ATTR <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;postLogon&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doFilter<span style="color: #009900;">&#40;</span> ServletRequest request, ServletResponse response, FilterChain chain <span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span>, ServletException <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; .<br />
&nbsp; &nbsp; .</div></div>
<p>The only method we need to override on the AbstractFilter is the doFilter() method. This method takes a ServletRequest and a ServletResponse objects, together with the FilterChain.<br />
The request and response objects are the actual request the user&#8217;s browser submitted, and the response it is going to receive. The FilterChain is the chain of filters configured for the application &#8211; of which the CookieFilter is a member.<br />
It is important to link back the chain at the end of the doFilter method, otherwise it will not be completed.</p>
<p>The first part of <b>doFilter()</b> below tests whether the response object contains the &#8220;SET-COOKIE&#8221; header. We do not want the application to send a new cookie with every request made of the server, so we only let the servlet filter piggy-back on responses that already have this flag set.<br />
Now, because the cookie is sent as a header on the response object, we can&#8217;t just add the <b>HttpOnly</b> and <b>secure</b> to it &#8211; we need to recreate the cookie header completely.<br />
The code excerpt below does just that by retrieving the session id (the JSESSIONID attribute), and the context path from the existing header.<br />
It then tests to see whether the connection is secure (using the isSecure() method on the request object) &#8211; and sets the flag accordingly.</p>
<p>Finally, the code re-sets the &#8220;SET-COOKIE&#8221; header, using all the existing content, and adding the HttpOnly flag, and also the secure flag if required.</p>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doFilter<span style="color: #009900;">&#40;</span> ServletRequest request, ServletResponse response, FilterChain chain <span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span>, ServletException <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; HttpServletResponse httpResponse <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> HttpServletResponse <span style="color: #009900;">&#41;</span> response<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; HttpServletRequest httpRequest <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>HttpServletRequest<span style="color: #009900;">&#41;</span> request<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> httpResponse.<span style="color: #006633;">containsHeader</span><span style="color: #009900;">&#40;</span> SET_COOKIE <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> sessionid <span style="color: #339933;">=</span> httpRequest.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> contextPath <span style="color: #339933;">=</span> httpRequest.<span style="color: #006633;">getContextPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> secure <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> request.<span style="color: #006633;">isSecure</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; secure <span style="color: #339933;">=</span> SECURE_FLAG<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; httpResponse.<span style="color: #006633;">setHeader</span><span style="color: #009900;">&#40;</span> SET_COOKIE, JSESSION_ATTR <span style="color: #339933;">+</span> sessionid <span style="color: #339933;">+</span> PATH_ATTR <span style="color: #339933;">+</span> contextPath <span style="color: #339933;">+</span> HTTPONLY_FLAG <span style="color: #339933;">+</span> secure <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>To generate a new session-id after a successful login attempt, we are going to use a little flag of our own. On our web application, the first request will have the &#8216;SET-COOKIE&#8217; header, but subsequent requests will not.<br />
We use this knowledge to find the first post-login request that goes to a page (and not, say, an image) &#8211; this is done by testing the query string, which in a Seam application must not be null (because it will have the cid= attribute on it).<br />
We also test for the presence of POST_LOGON_ATTR &#8211; an attribute we are going to manually add to the session after we change the session id. This will ensure that we don&#8217;t create a new session id on each new request.</p>
<p>When the first post-logon request on a session hits this filter, the if statement below returns &#8216;true&#8217;. The filter then needs to invalidate the session and create a new one (thereby generating a new JSESSIONID).<br />
However, the filter must make sure that the new session retains all the information from the old one. Therefore, before invalidating the session, it saves a map of all the session attributes, and copies them over to the new one.</p>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>httpRequest.<span style="color: #006633;">getQueryString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> httpRequest.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAttribute</span><span style="color: #009900;">&#40;</span> POST_LOGON_ATTR <span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span><span style="color: #000066; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Save existing session attributes</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HashMap<span style="color: #339933;">&lt;</span>String, Object<span style="color: #339933;">&gt;</span> old <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>String, Object<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Enumeration<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> keys <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Enumeration<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span> httpRequest.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAttributeNames</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>keys.<span style="color: #006633;">hasMoreElements</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> key <span style="color: #339933;">=</span> keys.<span style="color: #006633;">nextElement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; old.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>key, httpRequest.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAttribute</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Invalidate existing session and create a new one</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; httpRequest.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">invalidate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpSession session <span style="color: #339933;">=</span> httpRequest.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Copy attributes onto the new session</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> key <span style="color: #339933;">:</span> old.<span style="color: #006633;">keySet</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; session.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span>key, old.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Set the flag to signal that the JSESSIONID has already been changed.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; session.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span> POST_LOGON_ATTR, <span style="color: #0000ff;">&quot;true&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Add the 'SET-COOKIE' header, to send the new JSESSIONID to the browser.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> secure <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> request.<span style="color: #006633;">isSecure</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; secure <span style="color: #339933;">=</span>SECURE_FLAG<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; httpResponse.<span style="color: #006633;">setHeader</span><span style="color: #009900;">&#40;</span> SET_COOKIE, JSESSION_ATTR <span style="color: #339933;">+</span> httpRequest.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> PATH_ATTR <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; httpRequest.<span style="color: #006633;">getContextPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> HTTPONLY_FLAG &nbsp;<span style="color: #339933;">+</span> secure <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Remember to re-link the filter chain.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; chain.<span style="color: #006633;">doFilter</span><span style="color: #009900;">&#40;</span> request, response <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>This is it! your filter is now fully configured as a Seam component, and will work on your request. You can use Firefox&#8217;s <a href="http://www.bitstorm.org/extensions/view-cookies/">View Cookies</a> extension to verify that your JSESSIONID changes post login, and that the <b>HttpOnly</b> and <b>secure</b> flags are added to the cookie.</p>
<p>Below is the full Servlet Filter class, including the import statements.</p>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.duckranger.filters</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Enumeration</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.HashMap</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.FilterChain</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpSession</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jboss.seam.ScopeType</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jboss.seam.annotations.Install</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jboss.seam.annotations.Name</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jboss.seam.annotations.Scope</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jboss.seam.annotations.intercept.BypassInterceptors</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jboss.seam.annotations.web.Filter</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jboss.seam.web.AbstractFilter</span><span style="color: #339933;">;</span><br />
<br />
@Scope<span style="color: #009900;">&#40;</span> ScopeType.<span style="color: #006633;">APPLICATION</span> <span style="color: #009900;">&#41;</span> <br />
@<span style="color: #003399;">Name</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;cookieFilter&quot;</span> <span style="color: #009900;">&#41;</span><br />
@Install<span style="color: #009900;">&#40;</span> precedence <span style="color: #339933;">=</span> Install.<span style="color: #006633;">FRAMEWORK</span> <span style="color: #009900;">&#41;</span><br />
@BypassInterceptors<br />
@Filter<span style="color: #009900;">&#40;</span> within <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;org.jboss.seam.web.rewriteFilter&quot;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CookieFilter <span style="color: #000000; font-weight: bold;">extends</span> AbstractFilter <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> SET_COOKIE <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SET-COOKIE&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> SECURE_FLAG <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;; Secure&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> JSESSION_ATTR <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;JSESSIONID=&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> PATH_ATTR <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;; Path=&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> HTTPONLY_FLAG <span style="color: #339933;">=</span> &nbsp;<span style="color: #0000ff;">&quot;; HttpOnly&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> POST_LOGON_ATTR <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;postLogon&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doFilter<span style="color: #009900;">&#40;</span> ServletRequest request, ServletResponse response, FilterChain chain <span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span>, ServletException <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; HttpServletResponse httpResponse <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> HttpServletResponse <span style="color: #009900;">&#41;</span> response<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; HttpServletRequest httpRequest <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>HttpServletRequest<span style="color: #009900;">&#41;</span> request<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> httpResponse.<span style="color: #006633;">containsHeader</span><span style="color: #009900;">&#40;</span> SET_COOKIE <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> sessionid <span style="color: #339933;">=</span> httpRequest.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> contextPath <span style="color: #339933;">=</span> httpRequest.<span style="color: #006633;">getContextPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> secure <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> request.<span style="color: #006633;">isSecure</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; secure <span style="color: #339933;">=</span> SECURE_FLAG<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; httpResponse.<span style="color: #006633;">setHeader</span><span style="color: #009900;">&#40;</span> SET_COOKIE, JSESSION_ATTR <span style="color: #339933;">+</span> sessionid <span style="color: #339933;">+</span> PATH_ATTR <span style="color: #339933;">+</span> contextPath <span style="color: #339933;">+</span> HTTPONLY_FLAG <span style="color: #339933;">+</span> secure <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>httpRequest.<span style="color: #006633;">getQueryString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> httpRequest.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAttribute</span><span style="color: #009900;">&#40;</span> POST_LOGON_ATTR <span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span><span style="color: #000066; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HashMap<span style="color: #339933;">&lt;</span>String, Object<span style="color: #339933;">&gt;</span> old <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>String, Object<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Enumeration<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> keys <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Enumeration<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span> httpRequest.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">getAttributeNames</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>keys.<span style="color: #006633;">hasMoreElements</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> key <span style="color: #339933;">=</span> keys.<span style="color: #006633;">nextElement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; old.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>key, httpRequest.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAttribute</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; httpRequest.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">invalidate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpSession session <span style="color: #339933;">=</span> httpRequest.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> key <span style="color: #339933;">:</span> old.<span style="color: #006633;">keySet</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; session.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span>key, old.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; session.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span> POST_LOGON_ATTR, <span style="color: #0000ff;">&quot;true&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> secure <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> request.<span style="color: #006633;">isSecure</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; secure <span style="color: #339933;">=</span>SECURE_FLAG<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; httpResponse.<span style="color: #006633;">setHeader</span><span style="color: #009900;">&#40;</span> SET_COOKIE, JSESSION_ATTR <span style="color: #339933;">+</span> httpRequest.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> PATH_ATTR <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; httpRequest.<span style="color: #006633;">getContextPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> HTTPONLY_FLAG &nbsp;<span style="color: #339933;">+</span> secure <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; chain.<span style="color: #006633;">doFilter</span><span style="color: #009900;">&#40;</span> request, response <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://duckranger.com/2010/08/securing-your-seam-application-cookies/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://duckranger.com/2010/08/securing-your-seam-application-cookies/&amp;title=Securing+your+Seam+application+cookies" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://duckranger.com/2010/08/securing-your-seam-application-cookies/&amp;title=Securing+your+Seam+application+cookies" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://duckranger.com/2010/08/securing-your-seam-application-cookies/&amp;title=Securing+your+Seam+application+cookies&amp;desc=%0D%0ASeam%20is%20quite%20a%20good%20and%20secure%20framework%20as%20far%20as%20frameworks%20go.%20However%2C%20with%20a%20simple%20ServletFilter%2C%20it%20can%20be%20made%20just%20a%20little%20bit%20more%20secure%2C%20and%20maybe%20help%20you%20pass%20that%20next%20audit.%20In%20this%20post%20you%27ll%20see%20how%20to%20define%20a%20Seam%20servlet%20filter%20to%20secure%20your%20website%20just%20a%20little%20bit%20more." rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.dzone.com/links/add.html?url=http://duckranger.com/2010/08/securing-your-seam-application-cookies/&amp;title=Securing+your+Seam+application+cookies&amp;description=%0D%0ASeam%20is%20quite%20a%20good%20and%20secure%20framework%20as%20far%20as%20frameworks%20go.%20However%2C%20with%20a%20simple%20ServletFilter%2C%20it%20can%20be%20made%20just%20a%20little%20bit%20more%20secure%2C%20and%20maybe%20help%20you%20pass%20that%20next%20audit.%20In%20this%20post%20you%27ll%20see%20how%20to%20define%20a%20Seam%20servlet%20filter%20to%20secure%20your%20website%20just%20a%20little%20bit%20more." rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://duckranger.com/2010/08/securing-your-seam-application-cookies/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://duckranger.com/2010/08/securing-your-seam-application-cookies/&amp;bm_description=Securing+your+Seam+application+cookies&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://duckranger.com/2010/08/securing-your-seam-application-cookies/&amp;title=Securing+your+Seam+application+cookies" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://duckranger.com/2010/08/securing-your-seam-application-cookies/&amp;title=Securing+your+Seam+application+cookies" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://duckranger.com/2010/08/securing-your-seam-application-cookies/&amp;title=Securing+your+Seam+application+cookies" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://duckranger.com/2010/08/securing-your-seam-application-cookies/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Securing+your+Seam+application+cookies+-+http://b2l.me/aej5fe&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

<img src="http://duckranger.com/?ak_action=api_record_view&id=465&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://duckranger.com/2010/08/securing-your-seam-application-cookies/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Protecting your cake site against timing attacks</title>
		<link>http://duckranger.com/2010/07/protecting-your-cake-site-against-timing-attacks/</link>
		<comments>http://duckranger.com/2010/07/protecting-your-cake-site-against-timing-attacks/#comments</comments>
		<pubDate>Tue, 27 Jul 2010 04:12:01 +0000</pubDate>
		<dc:creator>Duck Ranger</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[incremental cracking]]></category>
		<category><![CDATA[timing attacks]]></category>

		<guid isPermaLink="false">http://duckranger.com/?p=406</guid>
		<description><![CDATA[Timing attacks are very difficult to pull off, but are possible nevertheless. However, it is quite easy to protect your Cake application against them, and as an aside &#8211; against incremental password crackers brute-forcing. This post shows you how. What&#8217;s a timing attack? A timing...]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/mandolinn/" class="tn"><img class="alignright" src="http://farm1.static.flickr.com/103/296618555_c914924a2d.jpg"  alt="It's an attack" width="250" height="166" /></a><br />
<a href="http://www.computerworld.com/s/article/9179224/Researchers_Authentication_crack_could_affect_millions?taxonomyId=17&#038;pageNumber=1">Timing attacks</a> are very difficult to pull off, but are possible nevertheless. However, it is quite easy to protect your Cake application against them, and as an aside &#8211; against incremental password crackers brute-forcing. This post shows you how.<br />
<span id="more-406"></span></p>
<h3>What&#8217;s a timing attack?</h3>
<p>A timing attack is when an attacker guesses your hashed password character by character, as opposed to using a dictionary or anything similar to try a brute force attack. </p>
<p>The way systems work is that your application hashes the password it receives from the user, and matches it to the hash it stores for this user in the users table.</p>
<p>This comparison stops at the first non-matching character. Therefore, it is possible to find your hashed value character by character, because the more correct characters you get &#8211; the longer the function takes to return. At least in theory.</p>
<h3>What&#8217;s an incremental cracker brute force attack?</h3>
<p>In an incremental crackin brute force attack, the attacker uses an incremental password cracker (e.g. <a href="http://www.openwall.com/john/">John the Ripper</a>) to feed your system a huge number of passwords &#8211; until it finds a good one.<br />
While you may think that this takes time and effort &#8211; in reality these things rely on how responsive your web server is &#8211; so the faster your login code returns &#8211; the faster these tools will crack your user&#8217;s password (given a username, of course).</p>
<p>To this end, it makes sense to make your hashing algorithm as non-optimized as possible. This is nothing new, and has been out there for over a decade (look up &#8216;adaptive hashing&#8217;) but &#8211; most of these hashing algorithms, while they take their time &#8211; are at least consistent in the sense that they return their result consistently as well &#8211; therefore still rendering your system vulnerable to timing attacks.</p>
<h3>The solution</h3>
<p>The solution is only 2 lines added to the security.php file under cake/libs:</p>
<div class="codecolorer-container php mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">//In cake/libs/Security.php</span><br />
<span style="color: #339933;">.</span><br />
<span style="color: #339933;">.</span><br />
<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">hash</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span> <span style="color: #000088;">$type</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$salt</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><br />
&nbsp; &nbsp; <span style="color: #000088;">$_this</span> <span style="color: #339933;">=&amp;</span> Security<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//introduce a random sleep</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$sleep</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mt_rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//1</span><br />
&nbsp; &nbsp; <span style="color: #990000;">usleep</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sleep</span><span style="color: #339933;">%</span><span style="color:#800080;">1000000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<span style="color: #666666; font-style: italic;">//2</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//original code continues</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$salt</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #339933;">.</span><br />
<span style="color: #339933;">.</span></div></div>
<p>The two lines above introduce a random wait time into your hash function.<br />
line (1) generates a random number. The mt_rand function returns a number that can be quite large. Therefore, the usleep() method in line (2) uses its modulo 1,000,000 value, and multiplies it by 3, to introduce a sleep time of 0..almost 3 seconds. Since this is a pseudo-random number, it plays hide and seek with any timing-attack algorithm, but also delays incremental crackers enough to send them elsewhere.</p>
<p>The impact on your users should be minimal. They shouldn&#8217;t log on or log off more than once a session, and a mean time to wait of just over 1.5 seconds is not too bad in these scenarios.</p>
<p>Good luck!</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://duckranger.com/2010/07/protecting-your-cake-site-against-timing-attacks/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://duckranger.com/2010/07/protecting-your-cake-site-against-timing-attacks/&amp;title=Protecting+your+cake+site+against+timing+attacks" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://duckranger.com/2010/07/protecting-your-cake-site-against-timing-attacks/&amp;title=Protecting+your+cake+site+against+timing+attacks" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://duckranger.com/2010/07/protecting-your-cake-site-against-timing-attacks/&amp;title=Protecting+your+cake+site+against+timing+attacks&amp;desc=%0D%0ATiming%20attacks%20are%20very%20difficult%20to%20pull%20off%2C%20but%20are%20possible%20nevertheless.%20However%2C%20it%20is%20quite%20easy%20to%20protect%20your%20Cake%20application%20against%20them%2C%20and%20as%20an%20aside%20-%20against%20incremental%20password%20crackers%20brute-forcing.%20This%20post%20shows%20you%20how.%0D%0A%0D%0AWhat%27s%20a%20timing%20attack%3F%0D%0AA%20timing%20attack%20is%20when" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.dzone.com/links/add.html?url=http://duckranger.com/2010/07/protecting-your-cake-site-against-timing-attacks/&amp;title=Protecting+your+cake+site+against+timing+attacks&amp;description=%0D%0ATiming%20attacks%20are%20very%20difficult%20to%20pull%20off%2C%20but%20are%20possible%20nevertheless.%20However%2C%20it%20is%20quite%20easy%20to%20protect%20your%20Cake%20application%20against%20them%2C%20and%20as%20an%20aside%20-%20against%20incremental%20password%20crackers%20brute-forcing.%20This%20post%20shows%20you%20how.%0D%0A%0D%0AWhat%27s%20a%20timing%20attack%3F%0D%0AA%20timing%20attack%20is%20when" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://duckranger.com/2010/07/protecting-your-cake-site-against-timing-attacks/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://duckranger.com/2010/07/protecting-your-cake-site-against-timing-attacks/&amp;bm_description=Protecting+your+cake+site+against+timing+attacks&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://duckranger.com/2010/07/protecting-your-cake-site-against-timing-attacks/&amp;title=Protecting+your+cake+site+against+timing+attacks" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://duckranger.com/2010/07/protecting-your-cake-site-against-timing-attacks/&amp;title=Protecting+your+cake+site+against+timing+attacks" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://duckranger.com/2010/07/protecting-your-cake-site-against-timing-attacks/&amp;title=Protecting+your+cake+site+against+timing+attacks" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://duckranger.com/2010/07/protecting-your-cake-site-against-timing-attacks/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Protecting+your+cake+site+against+timing+attacks+-+http://b2l.me/acxsua&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

<img src="http://duckranger.com/?ak_action=api_record_view&id=406&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://duckranger.com/2010/07/protecting-your-cake-site-against-timing-attacks/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Setting session timeout dynamically with Seam</title>
		<link>http://duckranger.com/2010/07/setting-session-timeout-dynamically-with-seam/</link>
		<comments>http://duckranger.com/2010/07/setting-session-timeout-dynamically-with-seam/#comments</comments>
		<pubDate>Wed, 14 Jul 2010 02:17:12 +0000</pubDate>
		<dc:creator>Duck Ranger</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Seam]]></category>
		<category><![CDATA[JEE listener]]></category>

		<guid isPermaLink="false">http://duckranger.com/?p=393</guid>
		<description><![CDATA[In a recent application, the customer wanted to be able to change session timeout dynamically, without restarting the application. This being a Seam application, I thought it was going to be quite straight-forward: either add it in the timeout= attribute in pages.xml, or just add...]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/robnas/" class="tn"><img class="alignright" title="Session timeout yay" src="http://farm3.static.flickr.com/2716/4138965979_0e9b488031.jpg" alt="I need a timeout" width="250" height="167" /></a>In a recent application, the customer wanted to be able to change session timeout dynamically, without restarting the application.<br />
This being a Seam application, I thought it was going to be quite straight-forward: either add it in the timeout= attribute in pages.xml, or just add it wherever we start a new conversation (conversation timeout is effectively what we were looking for anyway).</p>
<p>But, I was wrong.<br />
<span id="more-393"></span><br />
For starters, the timeout= attribute does not take an EL expression. You can only have a numeric value there. So much for setting it dynamically.<br />
The other thing is &#8211; we never start conversations manually &#8211; only with pages.xml directives. And I really didn&#8217;t want to change the entire conversation management, or dig into Seam code for that matter.</p>
<p>Some google-searches later and I found the <a href="http://download.oracle.com/docs/cd/E17410_01/javaee/6/api/javax/servlet/http/HttpSessionListener.html">HTTP session listener</a>.   </p>
<p>The HTTP session listener is a small class you can configure in web.xml, that captures session creation. It has two methods:</p>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">void</span> sessionCreated<span style="color: #009900;">&#40;</span>HttpSessionEvent se<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">void</span> sessionDestroyed<span style="color: #009900;">&#40;</span>HttpSessionEvent se<span style="color: #009900;">&#41;</span></div></div>
<p>sessionCreated() is called when a new session is created. All that&#8217;s left is setting the required timeout there:</p>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@Override<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> sessionCreated<span style="color: #009900;">&#40;</span>HttpSessionEvent se<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; HttpSession session <span style="color: #339933;">=</span> se.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; session.<span style="color: #006633;">setMaxInactiveInterval</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//in seconds</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>This is all very nice when you know the session timeout &#8211; but if you know it in advance &#8211; what&#8217;s the use of adding this listener? Just configure components.xml and be done with it.</p>
<h3>Getting the timeout configuration value from a database</h3>
<p>In our application we have an ApplicationParameterService Seam component. It exposes a method to fetch a configuration value from the database&#8217;s configuration table. Sweet. Let&#8217;s use it in the session listener:</p>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TimeoutConfigListener <span style="color: #000000; font-weight: bold;">implements</span> HttpSessionListener <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> TIMEOUT_ID <span style="color: #339933;">=</span> <span style="color: #cc66cc;">70</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> sessionCreated<span style="color: #009900;">&#40;</span>HttpSessionEvent se<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; HttpSession session <span style="color: #339933;">=</span> se.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ApplicationParameterService applicationParameterService <span style="color: #339933;">=</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span>ApplicationParameterService<span style="color: #009900;">&#41;</span> <span style="color: #003399;">Component</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;applicationParameterService&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> timeout <span style="color: #339933;">=</span> applicationParameterService.<span style="color: #006633;">getParameterById</span><span style="color: #009900;">&#40;</span> TIMEOUT_ID <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; session.<span style="color: #006633;">setMaxInactiveInterval</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">60</span> <span style="color: #339933;">*</span> <span style="color: #003399;">Integer</span>.<span style="color: #006633;">parseInt</span><span style="color: #009900;">&#40;</span> timeout <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> sessionDestroyed<span style="color: #009900;">&#40;</span> HttpSessionEvent se<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>So &#8211; in the listener, I ask Seam for the application parameter service, look up the session timeout value, which I get in minutes, and set it (transposed into seconds) on the session. No sweat.</p>
<p>Ah, but a little sweat.<br />
Seam throws an IllegalStateException &#8211; No application context. This is a weired Seam phenomenon, where even though the application is running (and in fact, the login page uses the ApplicationParameterService) &#8211; there&#8217;s no active context at the time that the listener is called.</p>
<p>This can be solved by forcing an application context if there is none:</p>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> sessionCreated<span style="color: #009900;">&#40;</span>HttpSessionEvent se<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; HttpSession session <span style="color: #339933;">=</span> se.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Contexts.<span style="color: #006633;">isApplicationContextActive</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Lifecycle.<span style="color: #006633;">beginCall</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ApplicationParameterService applicationParameterService <span style="color: #339933;">=</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span>ApplicationParameterService<span style="color: #009900;">&#41;</span> <span style="color: #003399;">Component</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;applicationParameterService&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> timeout <span style="color: #339933;">=</span> applicationParameterService.<span style="color: #006633;">getParameterById</span><span style="color: #009900;">&#40;</span> TIMEOUT_ID <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> timeoutSeconds <span style="color: #339933;">=</span> <span style="color: #cc66cc;">60</span> <span style="color: #339933;">*</span> <span style="color: #003399;">Integer</span>.<span style="color: #006633;">parseInt</span><span style="color: #009900;">&#40;</span> timeout <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; session.<span style="color: #006633;">setMaxInactiveInterval</span><span style="color: #009900;">&#40;</span>timeoutSeconds<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>and that&#8217;s it.</p>
<p>To have the listener capture session creation &#8211; you need to add it to web.xml. Make sure you keep Seam&#8217;s listener as the first one in the listeners list though. Seam needs it this way:</p>
<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">&lt;!-- in web.xml --&gt;</span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.jboss.seam.servlet.SeamListener<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.duckranger.TimeoutConfigListener<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://duckranger.com/2010/07/setting-session-timeout-dynamically-with-seam/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://duckranger.com/2010/07/setting-session-timeout-dynamically-with-seam/&amp;title=Setting+session+timeout+dynamically+with+Seam" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://duckranger.com/2010/07/setting-session-timeout-dynamically-with-seam/&amp;title=Setting+session+timeout+dynamically+with+Seam" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://duckranger.com/2010/07/setting-session-timeout-dynamically-with-seam/&amp;title=Setting+session+timeout+dynamically+with+Seam&amp;desc=In%20a%20recent%20application%2C%20the%20customer%20wanted%20to%20be%20able%20to%20change%20session%20timeout%20dynamically%2C%20without%20restarting%20the%20application.%0D%0AThis%20being%20a%20Seam%20application%2C%20I%20thought%20it%20was%20going%20to%20be%20quite%20straight-forward%3A%20either%20add%20it%20in%20the%20timeout%3D%20attribute%20in%20pages.xml%2C%20or%20just%20add%20it%20wherever%20we%20sta" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.dzone.com/links/add.html?url=http://duckranger.com/2010/07/setting-session-timeout-dynamically-with-seam/&amp;title=Setting+session+timeout+dynamically+with+Seam&amp;description=In%20a%20recent%20application%2C%20the%20customer%20wanted%20to%20be%20able%20to%20change%20session%20timeout%20dynamically%2C%20without%20restarting%20the%20application.%0D%0AThis%20being%20a%20Seam%20application%2C%20I%20thought%20it%20was%20going%20to%20be%20quite%20straight-forward%3A%20either%20add%20it%20in%20the%20timeout%3D%20attribute%20in%20pages.xml%2C%20or%20just%20add%20it%20wherever%20we%20sta" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://duckranger.com/2010/07/setting-session-timeout-dynamically-with-seam/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://duckranger.com/2010/07/setting-session-timeout-dynamically-with-seam/&amp;bm_description=Setting+session+timeout+dynamically+with+Seam&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://duckranger.com/2010/07/setting-session-timeout-dynamically-with-seam/&amp;title=Setting+session+timeout+dynamically+with+Seam" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://duckranger.com/2010/07/setting-session-timeout-dynamically-with-seam/&amp;title=Setting+session+timeout+dynamically+with+Seam" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://duckranger.com/2010/07/setting-session-timeout-dynamically-with-seam/&amp;title=Setting+session+timeout+dynamically+with+Seam" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://duckranger.com/2010/07/setting-session-timeout-dynamically-with-seam/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Setting+session+timeout+dynamically+with+Seam+-+http://b2l.me/aajck4&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

<img src="http://duckranger.com/?ak_action=api_record_view&id=393&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://duckranger.com/2010/07/setting-session-timeout-dynamically-with-seam/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting 404 with jquery.gritter?</title>
		<link>http://duckranger.com/2010/06/getting-404-with-jquery-gritter/</link>
		<comments>http://duckranger.com/2010/06/getting-404-with-jquery-gritter/#comments</comments>
		<pubDate>Mon, 21 Jun 2010 13:00:04 +0000</pubDate>
		<dc:creator>Duck Ranger</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[gritter]]></category>
		<category><![CDATA[growl]]></category>
		<category><![CDATA[IE bugs]]></category>

		<guid isPermaLink="false">http://duckranger.com/?p=376</guid>
		<description><![CDATA[You&#8217;ll probably never see it unless you use Firebug or look at Apache raw access logs, but jquery.gritter causes 404 errors repeatedly.The problem with 404&#8242;s is that they are like visiting relatives. They don&#8217;t go away unless you do something about it, and you usually...]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/bluhousworker" class="tn"><img class="alignright" title="Pick the color you like" src="http://farm2.static.flickr.com/1287/762963242_71e89dd2d4.jpg" alt="You won't find 404 here" width="250" height="176" /></a>You&#8217;ll probably never see it unless you use Firebug or look at Apache raw access logs, but jquery.gritter causes 404 errors repeatedly.<br/>The problem with 404&#8242;s is that they are like visiting relatives. They don&#8217;t go away unless you do something about it, and you usually can&#8217;t be bothered to do something about them &#8211; because it&#8217;s really not <b>that</b> bad.<br />
Of course, sometimes it is. Especially if you get it on every page load. These errors are annoying and clutter your log, potentially hiding important ones. You also get 404 blindness &#8211; and you may miss the &#8220;real&#8221; 404 that your users see.<br />
Still not convince &#8211; just think abot the extra half a second or so your users waste with every page load&#8230;<br />
<span id="more-376"></span><br />
If you look at firebug&#8217;s console you&#8217;ll see something along the following:<br />
<a href="http://duckranger.com/wp-content/uploads/2010/06/grittererror.png"><img src="http://duckranger.com/wp-content/uploads/2010/06/grittererror-300x66.png" alt="" title="grittererror" width="300" height="120" class="aligncenter size-medium wp-image-377" /></a></p>
<p>Examining the request (in firebug or Apache access log) shows:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&quot;GET /css/ HTTP/1.1&quot; 404</div></div>
<p>In firebug, you can also see the referer:<br />
<a href="http://duckranger.com/wp-content/uploads/2010/06/gritterreferer.jpg"><img src="http://duckranger.com/wp-content/uploads/2010/06/gritterreferer-300x284.jpg" alt="" title="gritterreferer" width="300" height="284" class="aligncenter size-medium wp-image-379" /></a></p>
<p>As you can clearly see (click on the image to see clearly&#8230;)  &#8211; the referer of the GET request is the jquery.gritter.css file. This is strange right?</p>
<p>Well, css files can definitely issue GET requests. For example, they may try to load background images. This calls for a quick look into the css file itself:</p>
<div class="codecolorer-container css mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6666ff;">.gritter-item-wrapper</span> <span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #933;">10px</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'.'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* ie7/8 fix */</span> <br />
<span style="color: #00AA00;">&#125;</span></div></div>
<p>This tries to set a background image on the wrapper div, so that it can solve a nasty IE7/8 positioning bug. However, this results in a GET request to the current directory &#8211; which happens to be the css directory. Naturally &#8211; this does not end well.</p>
<p>Luckily, the fix is easy. All you have to do is add a real background image. As long as you make it 1px by 1px (or completely transparent) &#8211; you&#8217;ll be fine and the 404&#8242;s will disappear:</p>
<div class="codecolorer-container css mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6666ff;">.gritter-item-wrapper</span> <span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #933;">10px</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'../img/blank.gif'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* ie7/8 fix */</span> <br />
<span style="color: #00AA00;">&#125;</span></div></div>
<p>Look at your logs/firebug &#8211; all good now!</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://duckranger.com/2010/06/getting-404-with-jquery-gritter/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://duckranger.com/2010/06/getting-404-with-jquery-gritter/&amp;title=Getting+404+with+jquery.gritter%3F" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://duckranger.com/2010/06/getting-404-with-jquery-gritter/&amp;title=Getting+404+with+jquery.gritter%3F" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://duckranger.com/2010/06/getting-404-with-jquery-gritter/&amp;title=Getting+404+with+jquery.gritter%3F&amp;desc=You%27ll%20probably%20never%20see%20it%20unless%20you%20use%20Firebug%20or%20look%20at%20Apache%20raw%20access%20logs%2C%20but%20jquery.gritter%20causes%20404%20errors%20repeatedly.The%20problem%20with%20404%27s%20is%20that%20they%20are%20like%20visiting%20relatives.%20They%20don%27t%20go%20away%20unless%20you%20do%20something%20about%20it%2C%20and%20you%20usually%20can%27t%20be%20bothered%20to%20do%20somethi" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.dzone.com/links/add.html?url=http://duckranger.com/2010/06/getting-404-with-jquery-gritter/&amp;title=Getting+404+with+jquery.gritter%3F&amp;description=You%27ll%20probably%20never%20see%20it%20unless%20you%20use%20Firebug%20or%20look%20at%20Apache%20raw%20access%20logs%2C%20but%20jquery.gritter%20causes%20404%20errors%20repeatedly.The%20problem%20with%20404%27s%20is%20that%20they%20are%20like%20visiting%20relatives.%20They%20don%27t%20go%20away%20unless%20you%20do%20something%20about%20it%2C%20and%20you%20usually%20can%27t%20be%20bothered%20to%20do%20somethi" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://duckranger.com/2010/06/getting-404-with-jquery-gritter/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://duckranger.com/2010/06/getting-404-with-jquery-gritter/&amp;bm_description=Getting+404+with+jquery.gritter%3F&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://duckranger.com/2010/06/getting-404-with-jquery-gritter/&amp;title=Getting+404+with+jquery.gritter%3F" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://duckranger.com/2010/06/getting-404-with-jquery-gritter/&amp;title=Getting+404+with+jquery.gritter%3F" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://duckranger.com/2010/06/getting-404-with-jquery-gritter/&amp;title=Getting+404+with+jquery.gritter%3F" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://duckranger.com/2010/06/getting-404-with-jquery-gritter/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Getting+404+with+jquery.gritter%3F+-+http://b2l.me/5fmfe&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

<img src="http://duckranger.com/?ak_action=api_record_view&id=376&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://duckranger.com/2010/06/getting-404-with-jquery-gritter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Typography – more important than you may think</title>
		<link>http://duckranger.com/2010/06/typography-%e2%80%93-more-important-than-you-may-think/</link>
		<comments>http://duckranger.com/2010/06/typography-%e2%80%93-more-important-than-you-may-think/#comments</comments>
		<pubDate>Tue, 15 Jun 2010 23:47:05 +0000</pubDate>
		<dc:creator>Duck Ranger</dc:creator>
				<category><![CDATA[Be nice to your users]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[User Interface]]></category>
		<category><![CDATA[typography]]></category>

		<guid isPermaLink="false">http://duckranger.com/?p=361</guid>
		<description><![CDATA[This post is part of the “Be nice to your users” web UI design series. There are many things that contribute to the rise or fall of your website. Anyone, and Google especially, will tell you that content is king. You have to have good,...]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/rexkevinaggabao/" class="tn"><img class="alignright" title="Pick the color you like" src="http://farm4.static.flickr.com/3471/3758310618_efb1ec7170.jpg" alt="typos?" width="250" height="165" /></a>This post is part of the “Be nice to your users” web UI design series.</p>
<p>There are many things that contribute to the rise or fall of your website. Anyone, and Google especially, will tell you that content is king. You have to have good, original  content. Your site needs to be original, provide good value for your users, etc etc.</p>
<p>But there are always those small things that can make or break it for you.</p>
<p>Design is one of these things.</p>
<p><span id="more-361"></span>A good design can make or break you. It’s a fact. Websites can no longer live on content alone. It has to look nice, and it has to feel good. Human beings are aesthetic creatures. We like things to look good, and if your website looks bad – well – your users are going to go somewhere else.</p>
<p>I won’t focus on badly designed websites. The nature of the Internet is that designs change, and sites I may link to here today may no longer look like this in the future. If you want to have a look at some bad design examples, go over to the <a href="http://www.webpagesthatsuck.com/" target="_blank">webpages that suck</a> website and have a look around.</p>
<p>I want to talk about typography.</p>
<h3>Typography is fashion</h3>
<p>Like everything else about website design (and really, any design at  all) – typography is a matter of fashion. It changes, morphes and is  being reborned every few months.</p>
<p>Think about webpages from the nineties: default fonts (or worse –  Comic Sans), default spacing, awful colors – you get the drift. Reading  the web was just not a happy activity.</p>
<p>But design changes. With the emergence of Web 2.0 – we got a lot of  white space. Websites today are more airy – there’s a lot of empty  space, that brings the actual content to the front. Be it images, blocks  of text, or videos – they stand out, because there’s so much white (or  other non-obtrusive background color) around them.</p>
<p>When your text stands-out on the merit of it being the black spot in  all this white – you don’t need to draw a lot of attention to it based  on font, loud colors and size. That being so, designers started making  the fonts blend in more with the general background. White background  received grayish texts, with large line-spacing, and all was well.</p>
<p>Additionally, we started using larger text size to draw attention to  specific parts of the page. The slogan, the mission statement, etc. (See  <a href="http://picapp.com/" target="_blank">picapp </a>for example)</p>
<p><a href="http://duckranger.com/wp-content/uploads/2010/06/picapp.png"><img src="http://duckranger.com/wp-content/uploads/2010/06/picapp-300x152.png" alt="" title="picapp" width="300" height="152" class="aligncenter size-medium wp-image-370" /></a></p>
<p>Websites started using <a href="http://www.instantshift.com/2009/06/19/125-creative-text-based-logo-designs-for-design-inspiration/" target="_blank">texts as logos</a>, and even when these are images –  you still read them.</p>
<h3>The font stack</h3>
<p>It was only a matter of time until a common font-stack started  appearing in all websites. In 2008-2009 many websites switched to the <a href="http://doctype.com/font-stack-should-use-lucida-grande-when-considering-unix-users" target="_blank">Lucida stack</a> :</p>
<div class="codecolorer-container css mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> <span style="color: #ff0000;">&quot;Lucida Grande&quot;</span><span style="color: #00AA00;">,</span> <span style="color: #ff0000;">&quot;Lucida Sans Unicode&quot;</span><span style="color: #00AA00;">,</span> <span style="color: #ff0000;">&quot;Lucida Sans&quot;</span><span style="color: #00AA00;">,</span> Verdana<span style="color: #00AA00;">,</span> <span style="color: #993333;">sans-serif</span><span style="color: #00AA00;">;</span></div></div>
<p>However, today (mid-2010) we see more and websites go to the Helvetica and Arial option, which I personally find better on the eyes. Also, and that’s a very important consideration when you’re trying to create an authoritative or business website – fonts have feelings.<br />
That font feeling</p>
<p>It sounds a little silly, I admit, but think about it. Fonts have feelings attached to them. Even if you don’t sense it upfront – it’s there. Have a look at the following fonts, see what you make of them:</p>
<p style="padding:4px;font-family: 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans';border:1px dotted #059bff">
I am a very respectable website. I am an authoritative source of information about all things electronics. The writers here know everything there is to know about the gadgets they are reviewing. Really, you don’t need to look anywhere else – read it all here, and buy from me!
</p>
<p>What did you feel about this text? Did it convince you? How about if it looked like this?</p>
<p style="padding:4px;font-family: 'Comic SANS MS';border:1px dotted #059bff">
I am a very respectable website. I am an authoritative source of information about all things electronics. The writers here know everything there is to know about the gadgets they are reviewing. Really, you don’t need to look anywhere else – read it all here, and buy from me!
</p>
<p>Ok, so the second example is maybe pushing it a bit too far, but the point I am trying to make, is that some fonts just don’t come across as serious enough. The Lucida stack, or the Comic Sans MS fonts give your users a feeling of reading a childrens book – not a serious website.<br />
For some readers (and you may be one of them) – this feeling is very subtle, but it is there.</p>
<p>Another issue with these fonts is that they are a bit more difficult to read, compared to other fonts. Try reading a long text in any of the Lucida family and you’ll see what I mean. The spacing between the letters, and the ‘not quite there’ shape of each individual letter makes your eyes and brain work more. I would suggest not to use these fonts for long articles…</p>
<p>Let’s try this one:</p>
<p style="padding:4px;font-family: 'Helvetica','Arial','Verdana';border:1px dotted #059bff">
I am a very respectable website. I am an authoritative source of information about all things electronics. The writers here know everything there is to know about the gadgets they are reviewing. Really, you don’t need to look anywhere else – read it all here, and buy from me!
</p>
<p>See? already better.</p>
<p>So fonts have feelings. In the sense that they stir specific feelings in readers. You need to make sure you find the right fonr for your content. Ask yourself:</p>
<ul>
<li>What are you trying to achieve?</li>
<li>What is your goal?</li>
<li>How do you want users to feel towards your content?</li>
<li>How long do you want them to spend on your site?</li>
</ul>
<p>Typography, along with space, multimedia and colors, makes your website appeal or repel. Yes, it’s “just text” – but most of the websites out there are about text. Don’t punish your users – go easily on their eyes.<br />
A final word – why is font choice so very important:</p>
<p><a href="http://duckranger.com/wp-content/uploads/2010/06/typography.png"><img src="http://duckranger.com/wp-content/uploads/2010/06/typography-300x144.png" alt="" title="typography" width="300" height="144" class="aligncenter size-medium wp-image-369" /></a></p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://duckranger.com/2010/06/typography-–-more-important-than-you-may-think/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://duckranger.com/2010/06/typography-%e2%80%93-more-important-than-you-may-think/&amp;title=Typography+%E2%80%93+more+important+than+you+may+think" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://duckranger.com/2010/06/typography-%e2%80%93-more-important-than-you-may-think/&amp;title=Typography+%E2%80%93+more+important+than+you+may+think" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://duckranger.com/2010/06/typography-%e2%80%93-more-important-than-you-may-think/&amp;title=Typography+%E2%80%93+more+important+than+you+may+think&amp;desc=This%20post%20is%20part%20of%20the%20%E2%80%9CBe%20nice%20to%20your%20users%E2%80%9D%20web%20UI%20design%20series.%0D%0A%0D%0AThere%20are%20many%20things%20that%20contribute%20to%20the%20rise%20or%20fall%20of%20your%20website.%20Anyone%2C%20and%20Google%20especially%2C%20will%20tell%20you%20that%20content%20is%20king.%20You%20have%20to%20have%20good%2C%20original%20%20content.%20Your%20site%20needs%20to%20be%20original%2C%20provid" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.dzone.com/links/add.html?url=http://duckranger.com/2010/06/typography-%e2%80%93-more-important-than-you-may-think/&amp;title=Typography+%E2%80%93+more+important+than+you+may+think&amp;description=This%20post%20is%20part%20of%20the%20%E2%80%9CBe%20nice%20to%20your%20users%E2%80%9D%20web%20UI%20design%20series.%0D%0A%0D%0AThere%20are%20many%20things%20that%20contribute%20to%20the%20rise%20or%20fall%20of%20your%20website.%20Anyone%2C%20and%20Google%20especially%2C%20will%20tell%20you%20that%20content%20is%20king.%20You%20have%20to%20have%20good%2C%20original%20%20content.%20Your%20site%20needs%20to%20be%20original%2C%20provid" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://duckranger.com/2010/06/typography-%e2%80%93-more-important-than-you-may-think/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://duckranger.com/2010/06/typography-%e2%80%93-more-important-than-you-may-think/&amp;bm_description=Typography+%E2%80%93+more+important+than+you+may+think&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://duckranger.com/2010/06/typography-%e2%80%93-more-important-than-you-may-think/&amp;title=Typography+%E2%80%93+more+important+than+you+may+think" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://duckranger.com/2010/06/typography-%e2%80%93-more-important-than-you-may-think/&amp;title=Typography+%E2%80%93+more+important+than+you+may+think" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://duckranger.com/2010/06/typography-%e2%80%93-more-important-than-you-may-think/&amp;title=Typography+%E2%80%93+more+important+than+you+may+think" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://duckranger.com/2010/06/typography-%e2%80%93-more-important-than-you-may-think/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Typography+%E2%80%93+more+important+than+you+may+think+-+http://b2l.me/3zztw&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

<img src="http://duckranger.com/?ak_action=api_record_view&id=361&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://duckranger.com/2010/06/typography-%e2%80%93-more-important-than-you-may-think/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Changing text color dynamically on web pages</title>
		<link>http://duckranger.com/2010/06/changing-text-color-dynamically-on-web-pages/</link>
		<comments>http://duckranger.com/2010/06/changing-text-color-dynamically-on-web-pages/#comments</comments>
		<pubDate>Wed, 02 Jun 2010 09:47:07 +0000</pubDate>
		<dc:creator>Duck Ranger</dc:creator>
				<category><![CDATA[Be nice to your users]]></category>
		<category><![CDATA[User Interface]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[colorpicker]]></category>
		<category><![CDATA[My designer is going to hate me forever]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://duckranger.com/?p=273</guid>
		<description><![CDATA[This post is part of a &#8220;Be nice to your users&#8221; web UI design series I plan to write. With the whole Web2.0 fluid/milky design that many sites are using now, it became very popular to use shades of gray for the main text color...]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/dr_33139/" class="tn"><img class="alignright" title="Pick the color you like" src="http://farm3.static.flickr.com/2261/2169794444_012143c562.jpg" alt="Pick the color you like" width="225" height="200" /></a>This post is part of a &#8220;Be nice to your users&#8221; web UI design series I plan to write.<br />
With the whole Web2.0 fluid/milky design that many sites are using now, it became very popular to use shades of gray for the main text color on websites.<br />
Sometimes, this gray can be really difficult to read, especially for people with less-than-perfect eye sight.<br />
Additionally, your users may not have the same excellent screen that you use to design, and different screens show colors differently. </p>
<p>It would be nice if you could offer your users the option to automatically change the text color your website, and is very easy to do with Jquery.<br />
<span id="more-273"></span><br />
To start, make sure to download <a href="http://jquery.com">jQuery</a> and have it accessible. As an alternative, you can link to it from a CDN (e.g. http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js).</p>
<p>I&#8217;m going to use just an index.html file for the simple <a href="http://duckranger.com/demos/graychanges/">demo</a>. Ensure the jquery .js file is accessible by your page, and you are good to go:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Color change<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">style</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; div.color-box {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height:30px;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width:30px;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; float:left;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; div#main {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color:#666666;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width:100%;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clear:both;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; padding-top:50px;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">style</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width:70%;margin:40px auto;overflow:auto;&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width:auto;float:right&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;float:left;padding-right:20px;line-height:30px;&quot;</span>&gt;</span>Click on a square to change text color:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;color-box&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background-color:#c0c0c0&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;color-box&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background-color:#a0a0a0&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;color-box&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background-color:#909090&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;color-box&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background-color:#707070&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;color-box&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background-color:#666666&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;color-box&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background-color:#606060&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;color-box&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background-color:#404040&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;color-box&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background-color:#222222&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;color-box&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background-color:#000000&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;main&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;">&lt;!-- all your content goes here --&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></div></div>
<p>This is not the full thing. It is just the header. What you create here is a strip of small boxes in different shades of gray to black &#8211; like in the <a href="http://duckranger.com/demos/graychanges/">demo</a> page.</p>
<p>Our goal is that when a user clicks on a colored div &#8211; the text changes color to the shade that was clicked. This can be really neat!</p>
<p>To add the magic, we&#8217;ll add a simple jQuery to capture a click:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div.color-box&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $color <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-color'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#main&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'color'</span><span style="color: #339933;">,</span>$color<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></div></div>
<p>Changing the color is easy. When a color &lt;div> is clicked, the browser executes the code found in the click() callback we just added.</p>
<p>All the function does is find the background color of the clicked div and set it as the text color on the main div.</p>
<p>You can test this for yourself on the demo page.</p>
<h2>I refresh and it&#8217;s gone..</h2>
<p>After your user changes the color, he may refresh the page, or go to another link on your website. When this happens &#8211; the color is reset to normal. This is not so good.</p>
<p>Luckily, we have cookies. Using a cookie we can save the user&#8217;s color preference:<br />
(thanks to <a href="http://www.dynamicdrive.com">dynamicdrive</a> for the cookie scripts)</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> getCookie<span style="color: #009900;">&#40;</span>c_name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">cookie</span>.<span style="color: #660066;">length</span><span style="color: #339933;">&gt;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; c_start<span style="color: #339933;">=</span>document.<span style="color: #660066;">cookie</span>.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span>c_name <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;=&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>c_start<span style="color: #339933;">!=-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c_start<span style="color: #339933;">=</span>c_start <span style="color: #339933;">+</span> c_name.<span style="color: #660066;">length</span><span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c_end<span style="color: #339933;">=</span>document.<span style="color: #660066;">cookie</span>.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;;&quot;</span><span style="color: #339933;">,</span>c_start<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>c_end<span style="color: #339933;">==-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c_end<span style="color: #339933;">=</span>document.<span style="color: #660066;">cookie</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> unescape<span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">cookie</span>.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span>c_start<span style="color: #339933;">,</span>c_end<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> setCookie<span style="color: #009900;">&#40;</span>c_name<span style="color: #339933;">,</span>value<span style="color: #339933;">,</span>expiredays<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> exdate<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; exdate.<span style="color: #660066;">setDate</span><span style="color: #009900;">&#40;</span>exdate.<span style="color: #660066;">getDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>expiredays<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; document.<span style="color: #660066;">cookie</span><span style="color: #339933;">=</span>c_name<span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;=&quot;</span> <span style="color: #339933;">+</span>escape<span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>expiredays<span style="color: #339933;">==</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;;expires=&quot;</span><span style="color: #339933;">+</span>exdate.<span style="color: #660066;">toGMTString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The functions above just set and get a value on a cookie.<br />
To use them, change the previous code a little bit:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//Change 1: try and load color from cookie on load. This makes color change survive </span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; page refresh or following links.</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//on ready - find if we have a color set in the cookie.</span><br />
&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $color <span style="color: #339933;">=</span> getCookie<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'text-color'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$color.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#main&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'color'</span><span style="color: #339933;">,</span>$color<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div.color-box&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $color <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-color'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#main&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'color'</span><span style="color: #339933;">,</span>$color<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//change 2: store the new color in the cookie</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; setCookie<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'text-color'</span><span style="color: #339933;">,</span>$color<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></div></div>
<p>All that&#8217;s left is add some text in the main div and test:<br />
(Below is the full file I use)</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- this is index.html that you change below to get the ultimate color picker --&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Color change<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">style</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; div.color-box {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height:30px;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width:30px;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; float:left;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; div#main {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color:#666666;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width:100%;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clear:both;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; padding-top:50px;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">style</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width:70%;margin:40px auto;overflow:auto;&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width:auto;float:right&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;float:left;padding-right:20px;line-height:30px;&quot;</span>&gt;</span>Click on a square to change text color:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;color-box&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background-color:#c0c0c0&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;color-box&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background-color:#a0a0a0&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;color-box&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background-color:#909090&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;color-box&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background-color:#707070&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;color-box&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background-color:#666666&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;color-box&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background-color:#606060&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;color-box&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background-color:#404040&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;color-box&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background-color:#222222&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;color-box&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background-color:#000000&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;main&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>Be nice to your users<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h3</span>&gt;</span>Let them set their own text color...<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h3</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; The Wiggles are a children's musical group formed in Sydney, Australia in 1991. Their original members were<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Anthony Field, Murray Cook, Greg Page, Jeff Fatt and Phillip Wilcher. Wilcher left the group after its first<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; album. In 2006, Page was forced to retire from the group due to illness and was replaced by understudy<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Sam Moran.<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span><span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span><span style="color: #66cc66;">/</span>&gt;</span>Field and Fatt were members of the Australian pop band The Cockroaches in the <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1980s, and Cook was a member of several bands before meeting Field and Page at Macquarie University, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; where they were studying to become pre-school teachers.<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span><span style="color: #66cc66;">/</span>&gt;</span>A school project led to the recording of &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; their first album and tour in 1991. As a result of their background, the group combines music and theories<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; of child development in their videos, television programs, and live shows.<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span><span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span><span style="color: #66cc66;">/</span>&gt;</span>Since their inception, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; other regular characters (Captain Feathersword, Dorothy the Dinosaur, Henry the Octopus, and Wags the <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dog) and a troupe called &quot;The Wiggly dancers&quot; have toured with them and appeared in their CDs, DVDs, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; and television programs.<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span><span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span><span style="color: #66cc66;">/</span>&gt;</span>The group has franchised their concepts to other countries, developed <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Wiggles sections in amusement parks in Australia and the US, and won several recording industry awards. <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; The Wiggles have been called &quot;the world's biggest preschool band&quot; and &quot;your child's first rock band&quot;.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span><span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span><span style="color: #66cc66;">/</span>&gt;</span>The group has achieved worldwide success with their children's albums, videos, television series, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; and concert appearances. The Wiggles were named Business Review Weekly's top-earning Australian <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entertainers for four years in a row and earned AU$45 million in 2009.<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span><span style="color: #66cc66;">/</span>&gt;</span>They have earned seventeen <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gold, twelve platinum, three double-platinum, and ten multi-platinum awards for sales of over 17 million <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DVDs and four million CDs.<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span><span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span><span style="color: #66cc66;">/</span>&gt;</span>By 2002, The Wiggles had become the Australian Broadcasting <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Corporation's (ABC) most successful pre-school television program. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;float:right;width:120px;margin-top:20px;font-size:10px;text-align:right;&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://en.wikipedia.org/wiki/Henry_the_Octopus#Wags_the_Dog_and_Henry_the_Octopus&quot;</span>&gt;</span>Source: Wikipedia<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Change 1: try and load color from cookie on load. This makes color change survive <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; page refresh or following links.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //on ready - find if we have a color set in the cookie.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(document).ready(function() {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var $color = getCookie('text-color');<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($color.length &gt; 0) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(&quot;#main&quot;).css('color',$color);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(&quot;div.color-box&quot;).click(function() {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var $color = $(this).css('background-color');<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(&quot;#main&quot;).css('color',$color);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //change 2: store the new color in the cookie<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setCookie('text-color',$color);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;});<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function getCookie(c_name) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (document.cookie.length&gt;0) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c_start=document.cookie.indexOf(c_name + &quot;=&quot;);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (c_start!=-1) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c_start=c_start + c_name.length+1;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c_end=document.cookie.indexOf(&quot;;&quot;,c_start);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (c_end==-1) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c_end=document.cookie.length;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return unescape(document.cookie.substring(c_start,c_end));<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return &quot;&quot;;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function setCookie(c_name,value,expiredays) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var exdate=new Date();<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exdate.setDate(exdate.getDate()+expiredays);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.cookie=c_name+ &quot;=&quot; +escape(value)+ ((expiredays==null) ? &quot;&quot; : &quot;;expires=&quot;+exdate.toGMTString());<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></div></div>
<p>But wait, there&#8217;s another option:</p>
<h2>And now &#8211; with any color!</h2>
<p>This second <a href="http://duckranger.com/demos/colorchanges/">demo</a> tells it all.<br />
I am using the excellent <a href="http://www.eyecon.ro/colorpicker/">colorPicker plugin</a> to do it.</p>
<p>To implement:<br />
1. Download the plugin&#8217;s js files, css and images. The only js file you really need is colorpicker.js &#8211; so you can disregard the rest of them. (Naturally, you need jquery.js &#8211; but we took care of it already in the previous example)<br />
2. Put the css files nicely somewhere (have a look at the source code in the demo to see. I basically put css files under /css/ and images under /images/)<br />
3. Copy these two styles to your style element, and remove the style for <b>div.color-box</b>.</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">style</span>&gt;</span><br />
#colorSelector div {<br />
&nbsp; &nbsp; background:url(&quot;../images/select.png&quot;) repeat scroll center center transparent;<br />
&nbsp; &nbsp; height:30px;<br />
&nbsp; &nbsp; left:3px;<br />
&nbsp; &nbsp; position:absolute;<br />
&nbsp; &nbsp; top:3px;<br />
&nbsp; &nbsp; width:30px;<br />
}<br />
<br />
#colorSelector &nbsp;{<br />
&nbsp; &nbsp; background:url(&quot;../images/select.png&quot;) repeat scroll 0 0 transparent;<br />
&nbsp; &nbsp; height:36px;<br />
&nbsp; &nbsp; position:relative;<br />
&nbsp; &nbsp; width:36px;<br />
&nbsp; &nbsp; float:left;<br />
}<br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">style</span>&gt;</span></div></div>
<p>Add the colorpicker plugin and its main css to your <head> element:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;./js/colorpicker.js&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">media</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;screen&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;./css/colorpicker.css&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span></div></div>
<p>Change the top part of your index.html page to only show the color picker div and not the series of gray div elements:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">...<br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width:70%;margin:40px auto;overflow:auto;&quot;</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width:auto;float:right&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;float:left;padding-right:20px;line-height:30px;&quot;</span>&gt;</span>Click on the square to change text color:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;colorSelector&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> &nbsp;<span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;bgdiv&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background-color:#666666;&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;main&quot;</span>&gt;</span><br />
...</div></div>
<p>Now add the following javascript down the bottom (replace the $(document).ready() function with this one) :</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $color <span style="color: #339933;">=</span> getCookie<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'text-color'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$color.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#main&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'color'</span><span style="color: #339933;">,</span>$color<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; $color <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#main&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'color'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#colorSelector'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ColorPicker</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; color<span style="color: #339933;">:</span> <span style="color: #3366CC;">'#0000ff'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onShow<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>colpkr<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span>colpkr<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeIn</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onHide<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>colpkr<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span>colpkr<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeOut</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onChange<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>hsb<span style="color: #339933;">,</span> hex<span style="color: #339933;">,</span> rgb<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#main'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'color'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'#'</span> <span style="color: #339933;">+</span> hex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setCookie<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'text-color'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'#'</span> <span style="color: #339933;">+</span> hex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#bgdiv&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-color'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'#'</span> <span style="color: #339933;">+</span> hex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>That&#8217;s it really <img src='http://duckranger.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  enjoy&#8230;</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://duckranger.com/2010/06/changing-text-color-dynamically-on-web-pages/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://duckranger.com/2010/06/changing-text-color-dynamically-on-web-pages/&amp;title=Changing+text+color+dynamically+on+web+pages" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://duckranger.com/2010/06/changing-text-color-dynamically-on-web-pages/&amp;title=Changing+text+color+dynamically+on+web+pages" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://duckranger.com/2010/06/changing-text-color-dynamically-on-web-pages/&amp;title=Changing+text+color+dynamically+on+web+pages&amp;desc=This%20post%20is%20part%20of%20a%20%22Be%20nice%20to%20your%20users%22%20web%20UI%20design%20series%20I%20plan%20to%20write.%0D%0AWith%20the%20whole%20Web2.0%20fluid%2Fmilky%20design%20that%20many%20sites%20are%20using%20now%2C%20it%20became%20very%20popular%20to%20use%20shades%20of%20gray%20for%20the%20main%20text%20color%20on%20websites.%20%0D%0ASometimes%2C%20this%20gray%20can%20be%20really%20difficult%20to%20read%2C%20espe" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.dzone.com/links/add.html?url=http://duckranger.com/2010/06/changing-text-color-dynamically-on-web-pages/&amp;title=Changing+text+color+dynamically+on+web+pages&amp;description=This%20post%20is%20part%20of%20a%20%22Be%20nice%20to%20your%20users%22%20web%20UI%20design%20series%20I%20plan%20to%20write.%0D%0AWith%20the%20whole%20Web2.0%20fluid%2Fmilky%20design%20that%20many%20sites%20are%20using%20now%2C%20it%20became%20very%20popular%20to%20use%20shades%20of%20gray%20for%20the%20main%20text%20color%20on%20websites.%20%0D%0ASometimes%2C%20this%20gray%20can%20be%20really%20difficult%20to%20read%2C%20espe" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://duckranger.com/2010/06/changing-text-color-dynamically-on-web-pages/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://duckranger.com/2010/06/changing-text-color-dynamically-on-web-pages/&amp;bm_description=Changing+text+color+dynamically+on+web+pages&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://duckranger.com/2010/06/changing-text-color-dynamically-on-web-pages/&amp;title=Changing+text+color+dynamically+on+web+pages" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://duckranger.com/2010/06/changing-text-color-dynamically-on-web-pages/&amp;title=Changing+text+color+dynamically+on+web+pages" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://duckranger.com/2010/06/changing-text-color-dynamically-on-web-pages/&amp;title=Changing+text+color+dynamically+on+web+pages" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://duckranger.com/2010/06/changing-text-color-dynamically-on-web-pages/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Changing+text+color+dynamically+on+web+pages+-+File: /data/app/webapp/functions.php<br />Line: 7<br />Message: Too many connections&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

<img src="http://duckranger.com/?ak_action=api_record_view&id=273&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://duckranger.com/2010/06/changing-text-color-dynamically-on-web-pages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Resize images on the fly with CakePHP</title>
		<link>http://duckranger.com/2010/06/resize-images-on-the-fly-with-cakephp/</link>
		<comments>http://duckranger.com/2010/06/resize-images-on-the-fly-with-cakephp/#comments</comments>
		<pubDate>Mon, 31 May 2010 21:55:57 +0000</pubDate>
		<dc:creator>Duck Ranger</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[User Interface]]></category>
		<category><![CDATA[images]]></category>

		<guid isPermaLink="false">http://duckranger.com/?p=294</guid>
		<description><![CDATA[We&#8217;ve all been there: You have a website design. There are placeholders for galleries or thumbnails or what-have-you. Sweet as. You code, fire up the browser and - the images are all wrong! They are too large, your page looks awful, or you get sliders...]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/nathaninsandiego"><img class="alignright" title="Resized?" src="http://farm4.static.flickr.com/3249/2977512372_2203cb5615.jpg" alt="Resized?" width="250" height="146" /></a>We&#8217;ve all been there: You have a website design. There are placeholders for galleries or thumbnails or what-have-you. Sweet as. </p>
<p>You code, fire up the browser and -</p>
<div style="clear:both"></div>
<p>the images are all wrong! They are too large, your page looks awful, or you get sliders all over the place. Not happy at all.<br />
<span id="more-294"></span><br />
<a href="http://duckranger.com/wp-content/uploads/2010/06/sliders1.png"><img src="http://duckranger.com/wp-content/uploads/2010/06/sliders1-300x153.jpg" alt="" title="sliders" width="300" height="153" class="aligncenter size-medium wp-image-300" /></a><br />
This is even worse when you don&#8217;t control the images. Say you have to hotlink from somewhere, e.g. Amazon. What are you going to do?</p>
<p>Well &#8211; here we go, <b>rsz</b> helper to the rescue!</p>
<p>The helper is simple. I coded two methods, but of course you can extend it whichever way you want.</p>
<p>The first method receives the image url, and the maximum dimensions (width and height) it can take on your screen. The helper finds the actual dimensions of the image, calculates the new height and width &#8211; according to the maximum dimensions you pass in &#8211; and returns an array with the new dimensions. The new dimensions keep the aspect ratio of the image so it doesn&#8217;t appear distorted.<br />
You can later use this array to output the image on your website:</p>
<div class="codecolorer-container php mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// in /views/helpers/rsz.php</span><br />
<span style="color: #000000; font-weight: bold;">class</span> RszHelper <span style="color: #000000; font-weight: bold;">extends</span> AppHelper <span style="color: #009900;">&#123;</span><br />
<span style="color: #339933;">...</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> imgResize<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</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;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//if no image url received - return zero dimensions.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$size</span> <span style="color: #339933;">=</span> <span style="color: #990000;">getimagesize</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$rx</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$ry</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$y</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$rx</span><span style="color: #339933;">&lt;=</span><span style="color: #cc66cc;">1</span> and <span style="color: #000088;">$ry</span><span style="color: #339933;">&lt;=</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//if both ratios are smaller than 1 - the image fits alright inside the designated space.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$size</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//resize as per the larger ratio</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$r</span> <span style="color: #339933;">=</span> <span style="color: #990000;">max</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rx</span><span style="color: #339933;">,</span><span style="color: #000088;">$ry</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">ceil</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">/</span><span style="color: #000088;">$r</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #990000;">ceil</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">/</span><span style="color: #000088;">$r</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span>catch<span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</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: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #339933;">...</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The code above resizes any image to the right dimensions. It returns an array with the new width and height. You can use this is your view as follows:</p>
<div class="codecolorer-container php mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">...<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$xy</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$rsz</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">imgResize</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://farm3.static.flickr.com/2284/2248449557_1ec805d771.jpg&quot;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">200</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">//this image is 500x329 pixels. ?&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://farm3.static.flickr.com/2284/2248449557_1ec805d771.jpg&quot;</span> width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?php echo <span style="color: #006699; font-weight: bold;">$xy[0]</span>;?&gt;&quot;</span> height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?php echo <span style="color: #006699; font-weight: bold;">$xy[1]</span>;?&gt;&quot;</span> <span style="color: #339933;">/&gt;</span><br />
<span style="color: #339933;">...</span></div></div>
<p>The image will be displayed in its new size: 152x100px.</p>
<p>You can change the helper to output the img tag directly, making your views cleaner, and allowing less space for copy/paste mistakes:</p>
<div class="codecolorer-container php mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// in /views/helpers/rsz.php</span><br />
<span style="color: #000000; font-weight: bold;">class</span> RszHelper <span style="color: #000000; font-weight: bold;">extends</span> AppHelper <span style="color: #009900;">&#123;</span><br />
<span style="color: #339933;">...</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> imgResizeAndPrint<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</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;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//if no image url received - return zero dimensions.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$xy</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$size</span> <span style="color: #339933;">=</span> <span style="color: #990000;">getimagesize</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$rx</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$ry</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$y</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$rx</span><span style="color: #339933;">&lt;=</span><span style="color: #cc66cc;">1</span> and <span style="color: #000088;">$ry</span><span style="color: #339933;">&lt;=</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//if both ratios are smaller than 1 - the image fits alright inside the designated space.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$size</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//resize as per the larger ratio</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$r</span> <span style="color: #339933;">=</span> <span style="color: #990000;">max</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rx</span><span style="color: #339933;">,</span><span style="color: #000088;">$ry</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$xy</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">ceil</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">/</span><span style="color: #000088;">$r</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #990000;">ceil</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">/</span><span style="color: #000088;">$r</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span>catch<span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$xy</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</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: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;img src=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$url</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot; width=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$xy</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot; height=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$xy</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot; /&gt;'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #339933;">...</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>and in your view:</p>
<div class="codecolorer-container php mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;?php</span> rsz<span style="color: #339933;">-&gt;</span><span style="color: #004000;">imgResizeAndPrint</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://farm3.static.flickr.com/2284/2248449557_1ec805d771.jpg&quot;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">200</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<h2>The problem with this method</h2>
<p>The problem with this method is that you actually load the image twice. The helper loads the image on the server side (with getimageresize), and the client&#8217;s browser loads it on the client side. This is a bit wasteful and can make your pages load slower.</p>
<p>There are a few ways to overcome this, but it depends on the way you store your data. In my application, for example, I constantly retrieve new images, but once I load them, I store them in a table containing the image&#8217;s url, size and width. </p>
<p>The first time I load an image, I use getimagesize() to find its dimensions. I then store them in the table, and the next calls to the helper&#8217;s imgresize method do not need to visit the image&#8217;s url at all:</p>
<div class="codecolorer-container php mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// in /views/helpers/rsz.php</span><br />
<span style="color: #000000; font-weight: bold;">class</span> RszHelper <span style="color: #000000; font-weight: bold;">extends</span> AppHelper <span style="color: #009900;">&#123;</span><br />
<span style="color: #339933;">...</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//$ix and $iy are the original image's width and height.</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> imgResizeAndPrint<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span><span style="color: #000088;">$x</span><span style="color: #339933;">,</span><span style="color: #000088;">$y</span><span style="color: #339933;">,</span><span style="color: #000088;">$ix</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #000088;">$iy</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//if no image url received - return zero dimensions.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$xy</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ix</span><span style="color: #339933;">==</span><span style="color: #009900; font-weight: bold;">null</span> and <span style="color: #000088;">$iy</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$size</span> <span style="color: #339933;">=</span> <span style="color: #990000;">getimagesize</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$size</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ix</span><span style="color: #339933;">,</span> <span style="color: #000088;">$iy</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$rx</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$ry</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$y</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$rx</span><span style="color: #339933;">&lt;=</span><span style="color: #cc66cc;">1</span> and <span style="color: #000088;">$ry</span><span style="color: #339933;">&lt;=</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//if both ratios are smaller than 1 - the image fits alright inside the designated space.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$size</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//resize as per the larger ratio</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$r</span> <span style="color: #339933;">=</span> <span style="color: #990000;">max</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rx</span><span style="color: #339933;">,</span><span style="color: #000088;">$ry</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">ceil</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">/</span><span style="color: #000088;">$r</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #990000;">ceil</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">/</span><span style="color: #000088;">$r</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span>catch<span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</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: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #339933;">...</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>In this very simple case, getimagesize is called only if the image&#8217;s dimensions are null. Depending on your application, this method can be extended to either receive an Image model and update it, or to return more values if this is the first time it is called.</p>
<h2>See an image resizer demo on <a href="http://choozza.com/compare/Weber_6550_Viny_vs._Weber_Baby_Q_%28Q/1320/1318/78">Choozza.com</a></he</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://duckranger.com/2010/06/resize-images-on-the-fly-with-cakephp/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://duckranger.com/2010/06/resize-images-on-the-fly-with-cakephp/&amp;title=Resize+images+on+the+fly+with+CakePHP" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://duckranger.com/2010/06/resize-images-on-the-fly-with-cakephp/&amp;title=Resize+images+on+the+fly+with+CakePHP" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://duckranger.com/2010/06/resize-images-on-the-fly-with-cakephp/&amp;title=Resize+images+on+the+fly+with+CakePHP&amp;desc=We%27ve%20all%20been%20there%3A%20You%20have%20a%20website%20design.%20There%20are%20placeholders%20for%20galleries%20or%20thumbnails%20or%20what-have-you.%20Sweet%20as.%20%0D%0A%0D%0AYou%20code%2C%20fire%20up%20the%20browser%20and%20-%0D%0A%0D%0Athe%20images%20are%20all%20wrong%21%20They%20are%20too%20large%2C%20your%20page%20looks%20awful%2C%20or%20you%20get%20sliders%20all%20over%20the%20place.%20Not%20happy%20at%20all.%0D%0A%0D%0A" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.dzone.com/links/add.html?url=http://duckranger.com/2010/06/resize-images-on-the-fly-with-cakephp/&amp;title=Resize+images+on+the+fly+with+CakePHP&amp;description=We%27ve%20all%20been%20there%3A%20You%20have%20a%20website%20design.%20There%20are%20placeholders%20for%20galleries%20or%20thumbnails%20or%20what-have-you.%20Sweet%20as.%20%0D%0A%0D%0AYou%20code%2C%20fire%20up%20the%20browser%20and%20-%0D%0A%0D%0Athe%20images%20are%20all%20wrong%21%20They%20are%20too%20large%2C%20your%20page%20looks%20awful%2C%20or%20you%20get%20sliders%20all%20over%20the%20place.%20Not%20happy%20at%20all.%0D%0A%0D%0A" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://duckranger.com/2010/06/resize-images-on-the-fly-with-cakephp/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://duckranger.com/2010/06/resize-images-on-the-fly-with-cakephp/&amp;bm_description=Resize+images+on+the+fly+with+CakePHP&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://duckranger.com/2010/06/resize-images-on-the-fly-with-cakephp/&amp;title=Resize+images+on+the+fly+with+CakePHP" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://duckranger.com/2010/06/resize-images-on-the-fly-with-cakephp/&amp;title=Resize+images+on+the+fly+with+CakePHP" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://duckranger.com/2010/06/resize-images-on-the-fly-with-cakephp/&amp;title=Resize+images+on+the+fly+with+CakePHP" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://duckranger.com/2010/06/resize-images-on-the-fly-with-cakephp/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Resize+images+on+the+fly+with+CakePHP+-+http://b2l.me/x8ch7&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

<img src="http://duckranger.com/?ak_action=api_record_view&id=294&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://duckranger.com/2010/06/resize-images-on-the-fly-with-cakephp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cake&#8217;s Security Component Timeout problem</title>
		<link>http://duckranger.com/2010/05/cakes-security-component-timeout-problem/</link>
		<comments>http://duckranger.com/2010/05/cakes-security-component-timeout-problem/#comments</comments>
		<pubDate>Wed, 26 May 2010 05:56:16 +0000</pubDate>
		<dc:creator>Duck Ranger</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[component]]></category>
		<category><![CDATA[csrf]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[HTML]]></category>

		<guid isPermaLink="false">http://duckranger.com/?p=280</guid>
		<description><![CDATA[CakePHP&#8217;s security component is awesome. I use it all the time for its CSRF protection (Which will be the subject of a future post). But sometimes, it does not work as expected. Frustration ensues. In one of my projects, I have a form with a...]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/punkassphotos/"><img class="alignright" title="She is a security expert" src="http://farm5.static.flickr.com/4016/4278600736_94eaf68ccb.jpg" alt="She is a security expert" width="250" height="156" /></a>CakePHP&#8217;s security component is awesome. I use it all the time for its CSRF protection (Which will be the subject of a future post).</p>
<p>But sometimes, it does not work as expected. Frustration ensues.</p>
<p>In one of my projects, I have a form with a few text areas on it. The user uses this form to create or edit a story. After adding the security component to the controller, I noticed that when the user submits the form they get blackholed, and the story was not saved. With users pouring their hearts into the stories, this is really bad. </p>
<p>This behaviour seemed strange, to say the least.<br />
<span id="more-280"></span><br />
The first thing I did was look for the session timing out on me. In core.php I have the following:</p>
<div class="codecolorer-container php mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Configure<span style="color: #339933;">::</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Security.level'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'high'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
Configure<span style="color: #339933;">::</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Session.timeout'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'120'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>based on Cake&#8217;s configuration, these should set the session timeout to 20 minutes (120 multiplied by 10). This may be a little too short for writing stories, so I raised the timeout value to 240 &#8211; resulting in 40 minutes, which is definitely enough. The stories are not THAT long.</p>
<p>However, this did not change the system&#8217;s behaviour. It almost seemed like the session times out long before the 40 minutes passed.</p>
<h2>Enter Security component</h2>
<p>When I removed the Security component from the controller, all was working great. It was time to delve into the code.</p>
<p>So, how does this security component work, anyway?</p>
<p>When you use the security component, Cake generates your forms differently. </p>
<p>Notice: In order for the security component to work correctly, you must use the form helper to create <b>all</b> of the fields on the form, as well as creating the form with $form->create() and closing it with $form->end(). </p>
<p>Consider this in the view:</p>
<div class="codecolorer-container php mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//add.ctp</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Story'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'target'</span><span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'_parent'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span> <span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'label'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'excerpt'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'rows'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'20'</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000ff;">'cols'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'80'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'label'</span><span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'body'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'rows'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'20'</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000ff;">'cols'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'80'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'label'</span><span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'notes'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'rows'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'20'</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #0000ff;">'cols'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'80'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'label'</span><span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">end</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'save'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>When the security component is not activated, the resulting form looks like this:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">target</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;_parent&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;StoryAddForm&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/stories/add&quot;</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">fieldset</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;display:none;&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;hidden&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;_method&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;POST&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">fieldset</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;input text&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;data[Story][title]&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;StoryTitle&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;input text&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">textarea</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;data[Story][excerpt]&quot;</span> <span style="color: #000066;">cols</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;80&quot;</span> <span style="color: #000066;">rows</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;20&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;StoryExcerpt&quot;</span> &gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">textarea</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;input text&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">textarea</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;data[Story][body]&quot;</span> <span style="color: #000066;">cols</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;80&quot;</span> <span style="color: #000066;">rows</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;20&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;StoryBody&quot;</span> &gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">textarea</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;input text&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">textarea</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;data[Story][notes]&quot;</span> <span style="color: #000066;">cols</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;80&quot;</span> <span style="color: #000066;">rows</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;20&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;StoryNotes&quot;</span> &gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">textarea</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;save&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Submit&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></div></div>
<p>As you can see, this is just a simple html form.<br />
To have the security component work its magic, you add it to the <b>uses</b> array in the controller:</p>
<div class="codecolorer-container php mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">//stories_controller.php</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$components</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Security'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Auth'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'RequestHandler'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>With the Security component activated this way, the form is generated with a few extra fields:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">target</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;_parent&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;StoryAddForm&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/stories/add&quot;</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">fieldset</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;display:none;&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;hidden&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;_method&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;POST&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;hidden&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;data[_Token][key]&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;5a364d27f9e39686bb6032ceed0a4ebcc1abba8e&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Token1821306795&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">fieldset</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;input text&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;data[Story][title]&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;StoryTitle&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;input text&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">textarea</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;data[Story][excerpt]&quot;</span> <span style="color: #000066;">cols</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;80&quot;</span> <span style="color: #000066;">rows</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;20&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;StoryExcerpt&quot;</span> &gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">textarea</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;input text&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">textarea</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;data[Story][body]&quot;</span> <span style="color: #000066;">cols</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;80&quot;</span> <span style="color: #000066;">rows</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;20&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;StoryBody&quot;</span> &gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">textarea</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;input text&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">textarea</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;data[Story][notes]&quot;</span> <span style="color: #000066;">cols</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;80&quot;</span> <span style="color: #000066;">rows</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;20&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;StoryNotes&quot;</span> &gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">textarea</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;save&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Submit&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">fieldset</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;display:none;&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;hidden&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;data[_Token][fields]&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;6e6401136c6c253e621550b05eaefa112da437c2%3An%3A0%3A%7B%7D&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;TokenFields1859539122&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">fieldset</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></div></div>
<p>Assuming you used the $form helper to generate the form and all its fields, the Security component adds two more hidden fields to it:</p>
<p><b>data[_Token][key]</b> &#8211; this is a token that marks this form as generated using the Security component. The token is unique to each form, and when the user submits the form back, Security component verifies that the token is valid. This is to ensure that the form originated from your website, and prevents attackers from crafting similar forms themselves and submitting them to your application.</p>
<p><b>data[_Token][fields]</b> is a hash of all the fields created using the $form helper. When the form is submitted a valid key, Security component also validates that the hash matches. This way, if an attacker managed to get a valid token, they can&#8217;t add fields to the form that weren&#8217;t there to begin with.</p>
<p>(Note: it is possible to instruct the Security component to disregard specific form-fields when generating and validating the data[_Token][fields] hash. You can do it in your beforeFilter() method by using the $this->Security->disabledFields array).</p>
<h2>Blackholing Requests</h2>
<p>If either the key or hash submitted with the form is invalid (or missing altogether), the form submit request will be blackholed. This will usually result in your users getting a blank page, and the data not saved anywhere. Obviously, this was happening to my stories, but why?</p>
<p>The token was valid, because I got it from the Security component to begin with. The hash was valid, as I never tempered with the form. All the signs pointed to a timeout&#8230;</p>
<h2>Your token has a time to live as well!</h2>
<p>Diving into Cake&#8217;s Security component code, I found the following:</p>
<div class="codecolorer-container php mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">//in cake/libs/controller/components/security.php</span><br />
<span style="color: #339933;">..</span><br />
<span style="color: #000000; font-weight: bold;">function</span> _generateToken<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$controller</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">...</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$authKey</span> <span style="color: #339933;">=</span> Security<span style="color: #339933;">::</span><span style="color: #004000;">generateAuthKey</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$expires</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'+'</span> <span style="color: #339933;">.</span> Security<span style="color: #339933;">::</span><span style="color: #004000;">inactiveMins</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' minutes'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$token</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'key'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$authKey</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'expires'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$expires</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'allowedControllers'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">allowedControllers</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'allowedActions'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">allowedActions</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'disabledFields'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">disabledFields</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">...</span></div></div>
<p>So, when a token is generated, it gets an expiration period. Mmm&#8230; so let&#8217;s have a look at the <b>inactiveMins()</b> method.</p>
<div class="codecolorer-container php mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// in cake/libs/security.php</span><br />
<span style="color: #339933;">..</span><br />
<span style="color: #000000; font-weight: bold;">function</span> inactiveMins<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$_this</span> <span style="color: #339933;">=&amp;</span> Security<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span>Configure<span style="color: #339933;">::</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Security.level'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'high'</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'medium'</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'low'</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">300</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #339933;">..</span></div></div>
<p>See what happens here? Regardless of the timeout you configure for your sessions, the token expiry time relies only on your <b>Security.level</b> settings. This means that my tokens may expire long before my session does. Which, apparently, is exactly what happens&#8230;</p>
<p>The way the session timeout is calculated, is that the number you set for <b>Session.timeout</b> in core.php is multiplied by a number depending on your Security.level setting.<br />
For Security.level = high, this number is 10.<br />
For Security.level = medium, this number is 100.<br />
For Security.level = low, this number is 300.</p>
<p>See the correlation? Instead of returning the calculated session timeout, this method returns only part of the equation!</p>
<p>Now, there may be some very good reasons for that, but none of them help me when I want all the benefits of a high Security.level setting together with allowing my users ample time to write their stories.</p>
<h2>It is time to change cake&#8217;s code!</h2>
<div class="codecolorer-container php mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// in cake/libs/security.php</span><br />
<span style="color: #339933;">..</span><br />
<span style="color: #000000; font-weight: bold;">function</span> inactiveMins<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$_this</span> <span style="color: #339933;">=&amp;</span> Security<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$timeout</span> <span style="color: #339933;">=</span> Configure<span style="color: #339933;">::</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Session.timeout'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$timeout</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$timeout</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// if not configured - use the original pattern</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span>Configure<span style="color: #339933;">::</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Security.level'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'high'</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$factor</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span> <span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'medium'</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$factor</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span> <span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'low'</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$factor</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">300</span> <span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$factor</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$timeout</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">60</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Using the piece of code above, inactiveMins(), when called, returns a number of minutes that&#8217;s equal to your session timeout. That&#8217;s it, problem solved. Security component will not blackhole my innocent users. Now, let the stories roll&#8230;</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://duckranger.com/2010/05/cakes-security-component-timeout-problem/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://duckranger.com/2010/05/cakes-security-component-timeout-problem/&amp;title=Cake%27s+Security+Component+Timeout+problem" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://duckranger.com/2010/05/cakes-security-component-timeout-problem/&amp;title=Cake%27s+Security+Component+Timeout+problem" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://duckranger.com/2010/05/cakes-security-component-timeout-problem/&amp;title=Cake%27s+Security+Component+Timeout+problem&amp;desc=CakePHP%27s%20security%20component%20is%20awesome.%20I%20use%20it%20all%20the%20time%20for%20its%20CSRF%20protection%20%28Which%20will%20be%20the%20subject%20of%20a%20future%20post%29.%0D%0A%0D%0ABut%20sometimes%2C%20it%20does%20not%20work%20as%20expected.%20Frustration%20ensues.%0D%0A%0D%0AIn%20one%20of%20my%20projects%2C%20I%20have%20a%20form%20with%20a%20few%20text%20areas%20on%20it.%20The%20user%20uses%20this%20form%20to%20cre" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.dzone.com/links/add.html?url=http://duckranger.com/2010/05/cakes-security-component-timeout-problem/&amp;title=Cake%27s+Security+Component+Timeout+problem&amp;description=CakePHP%27s%20security%20component%20is%20awesome.%20I%20use%20it%20all%20the%20time%20for%20its%20CSRF%20protection%20%28Which%20will%20be%20the%20subject%20of%20a%20future%20post%29.%0D%0A%0D%0ABut%20sometimes%2C%20it%20does%20not%20work%20as%20expected.%20Frustration%20ensues.%0D%0A%0D%0AIn%20one%20of%20my%20projects%2C%20I%20have%20a%20form%20with%20a%20few%20text%20areas%20on%20it.%20The%20user%20uses%20this%20form%20to%20cre" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://duckranger.com/2010/05/cakes-security-component-timeout-problem/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://duckranger.com/2010/05/cakes-security-component-timeout-problem/&amp;bm_description=Cake%27s+Security+Component+Timeout+problem&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://duckranger.com/2010/05/cakes-security-component-timeout-problem/&amp;title=Cake%27s+Security+Component+Timeout+problem" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://duckranger.com/2010/05/cakes-security-component-timeout-problem/&amp;title=Cake%27s+Security+Component+Timeout+problem" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://duckranger.com/2010/05/cakes-security-component-timeout-problem/&amp;title=Cake%27s+Security+Component+Timeout+problem" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://duckranger.com/2010/05/cakes-security-component-timeout-problem/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Cake%27s+Security+Component+Timeout+problem+-+File: /data/app/webapp/functions.php<br />Line: 7<br />Message: Too many connections&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

<img src="http://duckranger.com/?ak_action=api_record_view&id=280&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://duckranger.com/2010/05/cakes-security-component-timeout-problem/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Facelets: Setter not found for property class</title>
		<link>http://duckranger.com/2010/05/facelets-setter-not-found-for-property-class/</link>
		<comments>http://duckranger.com/2010/05/facelets-setter-not-found-for-property-class/#comments</comments>
		<pubDate>Sat, 15 May 2010 02:33:54 +0000</pubDate>
		<dc:creator>Duck Ranger</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Facelets]]></category>
		<category><![CDATA[Seam]]></category>

		<guid isPermaLink="false">http://duckranger.com/?p=259</guid>
		<description><![CDATA[&#8220;Setter not found for property class&#8221; is one of the most frustrating errors you can get in a Seam / facelets project. It usually appears late, and you spend a long time trying to solve it. The solution is so simple you have to bang...]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/lenniez/"><img class="alignright" title="Face of the class" src="http://farm2.static.flickr.com/1261/1174954998_1ff36425b2.jpg" alt="Face of the class" width="250" height="165" /></a>&#8220;Setter not found for property class&#8221; is one of the most frustrating errors you can get in a Seam / facelets project.</p>
<p>It usually appears late, and you spend a long time trying to solve it. The solution is so simple you have to bang your hand when you finally find it.</p>
<p>I write this in the hope that it shows first in Google searches for this error&#8230;<br />
<span id="more-259"></span></p>
<div style="clear:both"></div>
<h2>The stack trace</h2>
<p>The stack trace is not very useful:</p>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">java.<span style="color: #006633;">lang</span>.<span style="color: #003399;">IllegalArgumentException</span><span style="color: #339933;">:</span> Setter not found <span style="color: #000000; font-weight: bold;">for</span> property <span style="color: #000000; font-weight: bold;">class</span><br />
at javax.<span style="color: #006633;">faces</span>.<span style="color: #006633;">component</span>.<span style="color: #006633;">UIComponentBase</span>$AttributesMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>UIComponentBase.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">1594</span><span style="color: #009900;">&#41;</span><br />
at javax.<span style="color: #006633;">faces</span>.<span style="color: #006633;">component</span>.<span style="color: #006633;">UIComponentBase</span>$AttributesMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>UIComponentBase.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">1499</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">facelets</span>.<span style="color: #006633;">tag</span>.<span style="color: #006633;">jsf</span>.<span style="color: #006633;">ComponentRule</span>$LiteralAttributeMetadata.<span style="color: #006633;">applyMetadata</span><span style="color: #009900;">&#40;</span>ComponentRule.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">49</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">facelets</span>.<span style="color: #006633;">tag</span>.<span style="color: #006633;">MetadataImpl</span>.<span style="color: #006633;">applyMetadata</span><span style="color: #009900;">&#40;</span>MetadataImpl.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">36</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">facelets</span>.<span style="color: #006633;">tag</span>.<span style="color: #006633;">MetaTagHandler</span>.<span style="color: #006633;">setAttributes</span><span style="color: #009900;">&#40;</span>MetaTagHandler.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">62</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">facelets</span>.<span style="color: #006633;">tag</span>.<span style="color: #006633;">jsf</span>.<span style="color: #006633;">ComponentHandler</span>.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>ComponentHandler.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">144</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">facelets</span>.<span style="color: #006633;">tag</span>.<span style="color: #006633;">CompositeFaceletHandler</span>.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>CompositeFaceletHandler.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">47</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">facelets</span>.<span style="color: #006633;">tag</span>.<span style="color: #006633;">ui</span>.<span style="color: #006633;">DefineHandler</span>.<span style="color: #006633;">applyDefinition</span><span style="color: #009900;">&#40;</span>DefineHandler.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">64</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">facelets</span>.<span style="color: #006633;">tag</span>.<span style="color: #006633;">ui</span>.<span style="color: #006633;">CompositionHandler</span>.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>CompositionHandler.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">131</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">facelets</span>.<span style="color: #006633;">impl</span>.<span style="color: #006633;">DefaultFaceletContext</span>$TemplateManager.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>DefaultFaceletContext.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">337</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">facelets</span>.<span style="color: #006633;">impl</span>.<span style="color: #006633;">DefaultFaceletContext</span>.<span style="color: #006633;">includeDefinition</span><span style="color: #009900;">&#40;</span>DefaultFaceletContext.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">307</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">facelets</span>.<span style="color: #006633;">tag</span>.<span style="color: #006633;">ui</span>.<span style="color: #006633;">InsertHandler</span>.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>InsertHandler.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">68</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">facelets</span>.<span style="color: #006633;">tag</span>.<span style="color: #006633;">CompositeFaceletHandler</span>.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>CompositeFaceletHandler.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">47</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">facelets</span>.<span style="color: #006633;">tag</span>.<span style="color: #006633;">jsf</span>.<span style="color: #006633;">ComponentHandler</span>.<span style="color: #006633;">applyNextHandler</span><span style="color: #009900;">&#40;</span>ComponentHandler.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">314</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">facelets</span>.<span style="color: #006633;">tag</span>.<span style="color: #006633;">jsf</span>.<span style="color: #006633;">ComponentHandler</span>.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>ComponentHandler.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">169</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">facelets</span>.<span style="color: #006633;">tag</span>.<span style="color: #006633;">CompositeFaceletHandler</span>.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>CompositeFaceletHandler.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">47</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">facelets</span>.<span style="color: #006633;">compiler</span>.<span style="color: #006633;">NamespaceHandler</span>.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>NamespaceHandler.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">49</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">facelets</span>.<span style="color: #006633;">tag</span>.<span style="color: #006633;">CompositeFaceletHandler</span>.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>CompositeFaceletHandler.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">47</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">facelets</span>.<span style="color: #006633;">compiler</span>.<span style="color: #006633;">EncodingHandler</span>.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>EncodingHandler.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">25</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">facelets</span>.<span style="color: #006633;">impl</span>.<span style="color: #006633;">DefaultFacelet</span>.<span style="color: #006633;">include</span><span style="color: #009900;">&#40;</span>DefaultFacelet.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">248</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">facelets</span>.<span style="color: #006633;">impl</span>.<span style="color: #006633;">DefaultFacelet</span>.<span style="color: #006633;">include</span><span style="color: #009900;">&#40;</span>DefaultFacelet.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">294</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">facelets</span>.<span style="color: #006633;">impl</span>.<span style="color: #006633;">DefaultFacelet</span>.<span style="color: #006633;">include</span><span style="color: #009900;">&#40;</span>DefaultFacelet.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">273</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">facelets</span>.<span style="color: #006633;">impl</span>.<span style="color: #006633;">DefaultFaceletContext</span>.<span style="color: #006633;">includeFacelet</span><span style="color: #009900;">&#40;</span>DefaultFaceletContext.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">140</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">facelets</span>.<span style="color: #006633;">tag</span>.<span style="color: #006633;">ui</span>.<span style="color: #006633;">CompositionHandler</span>.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>CompositionHandler.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">113</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">facelets</span>.<span style="color: #006633;">compiler</span>.<span style="color: #006633;">NamespaceHandler</span>.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>NamespaceHandler.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">49</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">facelets</span>.<span style="color: #006633;">compiler</span>.<span style="color: #006633;">EncodingHandler</span>.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>EncodingHandler.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">25</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">facelets</span>.<span style="color: #006633;">impl</span>.<span style="color: #006633;">DefaultFacelet</span>.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>DefaultFacelet.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">95</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">facelets</span>.<span style="color: #006633;">FaceletViewHandler</span>.<span style="color: #006633;">buildView</span><span style="color: #009900;">&#40;</span>FaceletViewHandler.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">524</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">facelets</span>.<span style="color: #006633;">FaceletViewHandler</span>.<span style="color: #006633;">renderView</span><span style="color: #009900;">&#40;</span>FaceletViewHandler.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">567</span><span style="color: #009900;">&#41;</span><br />
at org.<span style="color: #006633;">ajax4jsf</span>.<span style="color: #006633;">application</span>.<span style="color: #006633;">ViewHandlerWrapper</span>.<span style="color: #006633;">renderView</span><span style="color: #009900;">&#40;</span>ViewHandlerWrapper.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><br />
at org.<span style="color: #006633;">ajax4jsf</span>.<span style="color: #006633;">application</span>.<span style="color: #006633;">AjaxViewHandler</span>.<span style="color: #006633;">renderView</span><span style="color: #009900;">&#40;</span>AjaxViewHandler.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">176</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">faces</span>.<span style="color: #006633;">lifecycle</span>.<span style="color: #006633;">RenderResponsePhase</span>.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span>RenderResponsePhase.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">106</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">faces</span>.<span style="color: #006633;">lifecycle</span>.<span style="color: #006633;">LifecycleImpl</span>.<span style="color: #006633;">phase</span><span style="color: #009900;">&#40;</span>LifecycleImpl.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">251</span><span style="color: #009900;">&#41;</span><br />
at com.<span style="color: #006633;">sun</span>.<span style="color: #006633;">faces</span>.<span style="color: #006633;">lifecycle</span>.<span style="color: #006633;">LifecycleImpl</span>.<span style="color: #006633;">render</span><span style="color: #009900;">&#40;</span>LifecycleImpl.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">144</span><span style="color: #009900;">&#41;</span></div></div>
<p>The issue is actually quite simple &#8211; you used a <strong>class</strong> attribute on a non-html element. I always get it when I translate our designer&#8217;s pages to JSF:</p>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">...</div></div>
<p>See the problem? the &lt;s:div&gt; element actually requires a <strong>styleClass</strong> attribute, not a <strong>class</strong>.</p>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">...</div></div>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://duckranger.com/2010/05/facelets-setter-not-found-for-property-class/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://duckranger.com/2010/05/facelets-setter-not-found-for-property-class/&amp;title=Facelets%3A+Setter+not+found+for+property+class" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://duckranger.com/2010/05/facelets-setter-not-found-for-property-class/&amp;title=Facelets%3A+Setter+not+found+for+property+class" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://duckranger.com/2010/05/facelets-setter-not-found-for-property-class/&amp;title=Facelets%3A+Setter+not+found+for+property+class&amp;desc=%22Setter%20not%20found%20for%20property%20class%22%20is%20one%20of%20the%20most%20frustrating%20errors%20you%20can%20get%20in%20a%20Seam%20%2F%20facelets%20project.%0D%0A%0D%0AIt%20usually%20appears%20late%2C%20and%20you%20spend%20a%20long%20time%20trying%20to%20solve%20it.%20The%20solution%20is%20so%20simple%20you%20have%20to%20bang%20your%20hand%20when%20you%20finally%20find%20it.%0D%0A%0D%0AI%20write%20this%20in%20the%20hope%20t" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.dzone.com/links/add.html?url=http://duckranger.com/2010/05/facelets-setter-not-found-for-property-class/&amp;title=Facelets%3A+Setter+not+found+for+property+class&amp;description=%22Setter%20not%20found%20for%20property%20class%22%20is%20one%20of%20the%20most%20frustrating%20errors%20you%20can%20get%20in%20a%20Seam%20%2F%20facelets%20project.%0D%0A%0D%0AIt%20usually%20appears%20late%2C%20and%20you%20spend%20a%20long%20time%20trying%20to%20solve%20it.%20The%20solution%20is%20so%20simple%20you%20have%20to%20bang%20your%20hand%20when%20you%20finally%20find%20it.%0D%0A%0D%0AI%20write%20this%20in%20the%20hope%20t" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://duckranger.com/2010/05/facelets-setter-not-found-for-property-class/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://duckranger.com/2010/05/facelets-setter-not-found-for-property-class/&amp;bm_description=Facelets%3A+Setter+not+found+for+property+class&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://duckranger.com/2010/05/facelets-setter-not-found-for-property-class/&amp;title=Facelets%3A+Setter+not+found+for+property+class" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://duckranger.com/2010/05/facelets-setter-not-found-for-property-class/&amp;title=Facelets%3A+Setter+not+found+for+property+class" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://duckranger.com/2010/05/facelets-setter-not-found-for-property-class/&amp;title=Facelets%3A+Setter+not+found+for+property+class" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://duckranger.com/2010/05/facelets-setter-not-found-for-property-class/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Facelets%3A+Setter+not+found+for+property+class+-+http://su.pr/9UOCii&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

<img src="http://duckranger.com/?ak_action=api_record_view&id=259&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://duckranger.com/2010/05/facelets-setter-not-found-for-property-class/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
