<?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>Jeffri Hong &#187; Programming</title>
	<atom:link href="http://jeffri.net/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://jeffri.net</link>
	<description> </description>
	<lastBuildDate>Sun, 05 Sep 2010 12:00:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>The Basics to Write a Secure PHP Web Application</title>
		<link>http://jeffri.net/2010/09/the-basics-to-write-a-secure-php-web-application/</link>
		<comments>http://jeffri.net/2010/09/the-basics-to-write-a-secure-php-web-application/#comments</comments>
		<pubDate>Fri, 03 Sep 2010 07:00:33 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[application]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=654</guid>
		<description><![CDATA[Securing your application is the most important things when building an application. This is the basic that every programmer should follow, it&#8217;s a must. However, sometime programmer might forgot the basic, and the more complex your application is, the harder it is to maintain and looking for security holes. While securing your application doesn&#8217;t mean [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjeffri.net%2F2010%2F09%2Fthe-basics-to-write-a-secure-php-web-application%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjeffri.net%2F2010%2F09%2Fthe-basics-to-write-a-secure-php-web-application%2F&amp;source=keaglez&amp;style=normal&amp;service=bit.ly&amp;service_api=R_8412a66499172453880523e0cb1bb28b" height="61" width="50" />
			</a>
		</div><p>Securing your application is the most important things when building an application. This is the basic that every programmer should follow, it&#8217;s a must. However, sometime programmer might forgot the basic, and the more complex your application is, the harder it is to maintain and looking for security holes. While securing your application doesn&#8217;t mean that you will be totally safe from a hack, since there is many factor of why a web can be hacked, but reduce the possibility is always a good practice.</p>
<p><img src="http://jeffri.net/wp-content/uploads/hack-target.jpg" alt="" title="hack-target" width="540" height="200" class="alignnone size-full wp-image-682" /></p>
<p>This article will give you walkthrough on the basics of creating a secured PHP application. I will give some step by step to filtering input, keep your code up to date and standard. I&#8217;ll also give you some good practice I always do.</p>
<p><span id="more-654"></span></p>
<h2>Filtering Input</h2>
<p>A web application is operated by retrieving inputs from user and then decide what to do based of the inputs. Filtering your input is a must to prevent any unwanted code to get executed.</p>
<h3>Making sure the input data type</h3>
<p>This is the most basic you should take care of for filtering. For example if you are retrieving integer type, you will need to make sure you always get an integer. Here is some example of how to make sure you get an integer.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'number'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;You must input a number&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'number'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #990000;">intval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'number'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;You must input a number&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Mostly, input retrieved will be always have a string data type, so sometime, you will need to convert it to make sure you get a correct data type.</p>
<h3>Validate the string</h3>
<p>If you have a list of string you will be accepting, you will need to make sure you only retrieve this value. Here is some example you can do.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'command'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;edit&quot;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'command'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;add&quot;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Unknown command&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'command'</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;add&quot;</span><span style="color: #339933;">:</span>
    <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;edit&quot;</span><span style="color: #339933;">:</span>
        <span style="color: #666666; font-style: italic;">// some code you will run</span>
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
        <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Unknown command&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^(edit|add)$/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'command'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Unknown command&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>I personally like the Regular Expression (<em>regex</em>) solution, since it is simple and need less work when you decide to accept a bunch of other strings. Also when you are accepting a pattern of string, it&#8217;s always good to use <em>regex</em>. Here is some example of basic email and URL filtering.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^[A-Za-z0-9_.-]@[A-Za-z0-9_.-]\.[A-Za-z0-9]{2,4}$/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;You entered invalid email&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^(http|https):\/\/[A-Za-z0-9_.-]\.[A-Za-z0-9]{2,4}$/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;You entered invalid url&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Remember that while PHP have two function to execute <em>regex</em>, <a href="http://www.php.net/manual/en/book.regex.php">POSIX Extended</a> and <a href="http://php.net/manual/en/book.pcre.php">PCRE</a>, you must always use the PCRE solution as the other one has deprecated since PHP 5.3.0.</p>
<h3>Escape your string</h3>
<p>You should always escape your string before you insert it to your database. If your server have <em>magic quotes</em> turned on by default, you might be safe from this. But your code shouldn&#8217;t rely on server capabilities, your code should escape the string if the <em>magic quotes</em> is turned off. You also must check the <em>magic quotes</em> configuration to prevent you to escape your string twice.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #990000;">get_magic_quotes_gpc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #990000;">addslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>If you are using MySQL database, use <strong>mysql_real_escape_string</strong> instead.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #990000;">get_magic_quotes_gpc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Finally, if you don&#8217;t want to accept html string, you can strip it or convert it to html entities.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$no_html</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strip_tags</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'no_html'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// this will strip all html tags</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$no_html</span> <span style="color: #339933;">=</span> <span style="color: #990000;">htmlentities</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'no_html'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// this will convert all html tags to entities</span></pre></td></tr></table></div>

<h3>Wrap it out in a function</h3>
<p>Making your code easier to maintain is one way to secure your application. Wrap it out in a function is a good solution. All you need to do is making sure the retrieved input has filtered in this function. So if you find any vulnerabilities, the first you will look is the function. This should save your time looking for the security holes.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> input_filter<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$input</span><span style="color: #339933;">,</span> <span style="color: #000088;">$type</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$filtered_input</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$input</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">get_magic_quotes_gpc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #000088;">$filtered_input</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filtered_input</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$type</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'numeric'</span><span style="color: #339933;">:</span>
            <span style="color: #000088;">$filtered_input</span> <span style="color: #339933;">=</span> <span style="color: #990000;">intval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filtered_input</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'email'</span><span style="color: #339933;">:</span>
            <span style="color: #000088;">$filtered_input</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^[A-Za-z0-9_.-]@[A-Za-z0-9_.-]\.[A-Za-z0-9]{2,4}$/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$filtered_input</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$filtered_input</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'url'</span><span style="color: #339933;">:</span>
            <span style="color: #000088;">$filtered_input</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^(http|https):\/\/[A-Za-z0-9_.-]\.[A-Za-z0-9]{2,4}$/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$filtered_input</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$filtered_input</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'html'</span><span style="color: #339933;">:</span>
            <span style="color: #000088;">$filtered_input</span> <span style="color: #339933;">=</span> <span style="color: #990000;">htmlentities</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filtered_input</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filtered_input</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And the example to use this function.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$number</span> <span style="color: #339933;">=</span> input_filter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'number'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'numeric'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// it will return 0 if it is not numeric</span>
<span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> input_filter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// it will return empty string if invalid</span>
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> input_filter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// it will return empty string if invalid</span>
<span style="color: #000088;">$no_html</span> <span style="color: #339933;">=</span> input_filter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'no_html'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'html'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// it will return all html converted to entities</span>
<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> input_filter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'string'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// it will simply escape the string</span></pre></td></tr></table></div>

<p>Depends to your preference, you can wrap it to a numbers of separated functions or to a class. But I think wrap it in a function is not bad either if you are creating a simple application and only need a small numbers of filtering.</p>
<h2>Keeping Your Code Up To Date and Standards</h2>
<p>PHP keep growing. If you are writing your application in PHP 4, most likely, it will still working in PHP 5. However, some of them might not work anymore in PHP 6. Some example is the use of deprecated function. The function that deprecated in PHP 5 will still working for backward compability, but using this function is not safe anymore as they can be removed anytime in future.</p>
<p>Make sure you are up to date to the new PHP version. Always look the PHP manual for each function you use. Some time you can find a notice of how to correctly use the function. The user contributed notes is also helpful if you are looking for a solution related to that function.</p>
<h3>Do not rely on register globals</h3>
<p><em>Register globals</em> has been turned off by default and deprecated since PHP 5.3.0. Incorrect use of <em>register globals</em> will make your code vulnerable. Always use <a href="http://www.php.net/manual/en/language.variables.superglobals.php"><em>superglobal</em></a> variable instead.</p>
<p>However, it&#8217;s also a good practice to not use <strong>$_REQUEST</strong> variable if you don&#8217;t need to. This variable contains the content of <strong>$_GET</strong>, <strong>$_POST</strong> and <strong>$_COOKIE</strong>. Using this variable will prevent you to know where the variable came from, it&#8217;s always good to know where your variable came from. For example you would like some inputs came from post or cookie, but using <strong>$_REQUEST</strong>, the user can use get and it still get executed.</p>
<h3>Always define a variable before use it</h3>
<p>You must always define your variable before you use it. In some server, <em>register globals</em> might be turned on, and failing to do so might result in vulnerable codes.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> validate_user<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$safe</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$safe</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #666666; font-style: italic;">// some sensitive code</span></pre></td></tr></table></div>

<p>The above code is vulnerable if <em>register globals</em> is turned on. If user run the script by calling <em>user.php?safe=1</em>, the user will always get validated. You can do this instead.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$safe</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> validate_user<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$safe</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$safe</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #666666; font-style: italic;">// some sensitive code</span></pre></td></tr></table></div>

<h3>Use strict error reporting in development</h3>
<p>Checking each function you use to find a deprecated function is not fun. So in development environment you must show all PHP errors. You can do this by calling this function on top of your application.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">error_reporting</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">E_ALL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This way, you will know if you are using deprecated function. You will also know if there is any undefined variable. This also includes the array in <em>superglobals</em> variable. Making sure to check undefined variable with <strong>isset</strong> before you use it is also a good practice.</p>
<h3>Turn off all error reporting in production</h3>
<p>If your website is ready now, you will need to turn off all error reporting. Showing your error to everyone will not be a good thing, one thing is you will reveal something to your visitor, such as file structure. For example, your main code might not be vulnerable, but some of included codes might be vulnerable if requested directly. Keeping your file structure a secret might give you a better security.</p>
<p>One thing to note, you also shouldn&#8217;t show <strong>mysql_error</strong> value to your visitor. If somehow you don&#8217;t filter the inputs properly and you have a code like this.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM some_table WHERE some_field='<span style="color: #006699; font-weight: bold;">$field_value</span>'&quot;</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The code above might look familiar as new programmer tends to use it to check for query error. However, if the <strong>$field_value</strong> variable is not filtered properly and vulnerable to injection, you will show the visitor the error and at the same time, you will reveal that your code is vulnerable. So instead of showing the error, you must log the error instead, this way only you can see the error.</p>
<h2>Conclusion</h2>
<p>So this is the basic you can use to secure your web application properly. Meeting this standard will make your application less vulnerable. Again, it will not guarantee that your application will be perfectly safe, as there is many other factors such as using an outdated web server or using some vulnerable library.</p>
<p>Hopefully, this will be useful for you. Do you also have a technique to write a secure web application? Please share in comment.</p>
<p>Final words, thank you to read this. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2010/09/the-basics-to-write-a-secure-php-web-application/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Best practice to clearing default text with jQuery</title>
		<link>http://jeffri.net/2010/08/best-practice-to-clearing-default-text-with-jquery/</link>
		<comments>http://jeffri.net/2010/08/best-practice-to-clearing-default-text-with-jquery/#comments</comments>
		<pubDate>Mon, 30 Aug 2010 02:45:00 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[xhtml]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=620</guid>
		<description><![CDATA[Having default text that we later removed when retrieving user input is common in today&#8217;s website. Many web developer use this to enhance usability of the website, so user know what to type in what field. There is a lot of way to do this, using both focus and blur event. For example, this is [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjeffri.net%2F2010%2F08%2Fbest-practice-to-clearing-default-text-with-jquery%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjeffri.net%2F2010%2F08%2Fbest-practice-to-clearing-default-text-with-jquery%2F&amp;source=keaglez&amp;style=normal&amp;service=bit.ly&amp;service_api=R_8412a66499172453880523e0cb1bb28b" height="61" width="50" />
			</a>
		</div><p>Having default text that we later removed when retrieving user input is common in today&#8217;s website. Many web developer use this to enhance usability of the website, so user know what to type in what field. There is a lot of way to do this, using both <em>focus</em> and <em>blur</em> event. For example, this is the shortest and easiest way that is commonly used:</p>
<input type="text" id="example-field" name="example-field" value="Your name" onfocus="if (this.value=='Your name') this.value=''" onblur="if (this.value=='') this.value='Your name'" />
&nbsp;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><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;text&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;example-field&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;example-field&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Your name&quot;</span> <span style="color: #000066;">onfocus</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;if (this.value=='Your name') this.value=''&quot;</span> <span style="color: #000066;">onblur</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;if (this.value=='') this.value='Your name'&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span></pre></td></tr></table></div>

<p>However, this approach will be a pain when you are managing a lot of input fields. You will need to type the default text three times, which can lead to a trivial typo. Also, since we check againts the default value, we will not be able to accept this value. Here is the solution, by using the simplicity and power of jQuery, this is a clean and a better way to do this.</p>
<p><span id="more-620"></span></p>
<input type="text" id="example-field2" name="example-field2" value="Your name" />
<p><script type="text/javascript">
jQuery(document).ready(function($){
	$('#example-field2').focus(example_clean_field).blur(example_reset_field).each(example_populate_field);
	function example_populate_field()
	{
		if ( ! $.data(this, 'default') )
			$.data(this, 'default', $(this).val());	
	}
	function example_clean_field()
	{
		if ( ! $(this).hasClass('hasfocus') )
			$(this).addClass('hasfocus').val('');
	}
	function example_reset_field()
	{
		if ( $(this).val() == '' )
			$(this).removeClass('hasfocus').val($.data(this, 'default'));
	}
});
</script></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&lt;input type=&quot;text&quot; id=&quot;example-field2&quot; name=&quot;example-field2&quot; value=&quot;Your name&quot; /&gt;
&nbsp;
<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>
jQuery<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>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#example-field2'</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span>example_clean_field<span style="color: #009900;">&#41;</span>.<span style="color: #000066;">blur</span><span style="color: #009900;">&#40;</span>example_reset_field<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>example_populate_field<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">function</span> example_populate_field<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> $.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'default'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
			$.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'default'</span><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;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span>
	<span style="color: #003366; font-weight: bold;">function</span> example_clean_field<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <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;">hasClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'hasfocus'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</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;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'hasfocus'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #003366; font-weight: bold;">function</span> example_reset_field<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</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;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">''</span> <span style="color: #009900;">&#41;</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;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'hasfocus'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>$.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'default'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>You can see that we define three functions here, one for <em>focus</em> event, one for <em>blur</em> event and the other one to populate the default value. I&#8217;ll briefly explain how they work in short.</p>
<p>We add a callback to each fields with function <strong>example_populate_field</strong>, this function will make use of <a href="http://api.jquery.com/jQuery.data/">jQuery.data</a>, which allow us to attach any data to DOM element. We will iterate to each fields, and store the data in &#8220;default&#8221; key.</p>
<p>Next, we have a <em>focus</em> callback, <strong>example_clean_field</strong>, this function check whether the field has &#8220;hasfocus&#8221; class, if it doesn&#8217;t then we add the class and clear the field. Note that we use the &#8220;hasfocus&#8221; class to determine the state of the field, whether it has been cleared or not.</p>
<p>Lastly, we have <strong>example_reset_field</strong> that we use for <em>blur</em> event. This function will check the current value, when it&#8217;s empty, we remove the &#8220;hasfocus&#8221; class and set the value back to the default.</p>
<p>The usage as shown above is very simple, you just need to select any input or textarea element, and it will work. For example, using this selector below will make this work to all text input and textarea element.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input[type=text], textarea'</span><span style="color: #009900;">&#41;</span> .<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span>example_clean_field<span style="color: #009900;">&#41;</span> .<span style="color: #000066;">blur</span><span style="color: #009900;">&#40;</span>example_reset_field<span style="color: #009900;">&#41;</span> .<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>example_populate_field<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>That&#8217;s all. It&#8217;s simple, clean and it looks like the most elegant solution I can find. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Hope that will be useful for you as well. Enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2010/08/best-practice-to-clearing-default-text-with-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Filter get_terms to return only terms with published post</title>
		<link>http://jeffri.net/2010/08/filter-get_terms-to-return-only-terms-with-published-post/</link>
		<comments>http://jeffri.net/2010/08/filter-get_terms-to-return-only-terms-with-published-post/#comments</comments>
		<pubDate>Sat, 21 Aug 2010 22:37:03 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=609</guid>
		<description><![CDATA[So you are using custom taxonomy. You add terms to the taxonomy, and add the terms to your posts. Then, you use get_terms and list all your terms. Everything works fine, terms that doesn&#8217;t have any posts will not be returned if you set hide_empty argument to true. Now you move some of your posts [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjeffri.net%2F2010%2F08%2Ffilter-get_terms-to-return-only-terms-with-published-post%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjeffri.net%2F2010%2F08%2Ffilter-get_terms-to-return-only-terms-with-published-post%2F&amp;source=keaglez&amp;style=normal&amp;service=bit.ly&amp;service_api=R_8412a66499172453880523e0cb1bb28b" height="61" width="50" />
			</a>
		</div><p>So you are using custom taxonomy. You add terms to the taxonomy, and add the terms to your posts. Then, you use <strong>get_terms</strong> and list all your terms. Everything works fine, terms that doesn&#8217;t have any posts will not be returned if you set <em>hide_empty</em> argument to true. Now you move some of your posts to draft and you get a problem. Terms that doesn&#8217;t have any published posts still returned. This lead to 404 error when you click on the link of this term. It doesn&#8217;t look good now, we need to remove term that doesn&#8217;t have any published post.</p>
<p>So how to do that? The answer is by using WordPress filter. This piece of code below will solve the problem.</p>
<p><span id="more-609"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> get_terms_filter<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$terms</span><span style="color: #339933;">,</span> <span style="color: #000088;">$taxonomies</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$taxonomy</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$taxonomies</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: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$terms</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$terms</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$terms</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$filtered_terms</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$terms</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$term</span> <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_var</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT COUNT(*) FROM <span style="color: #006699; font-weight: bold;">$wpdb-&gt;posts</span> p JOIN <span style="color: #006699; font-weight: bold;">$wpdb-&gt;term_relationships</span> rl ON p.ID = rl.object_id WHERE rl.term_taxonomy_id = <span style="color: #006699; font-weight: bold;">$term-&gt;term_id</span> AND p.post_status = 'publish' LIMIT 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">intval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
			<span style="color: #000088;">$filtered_terms</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$term</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$filtered_terms</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'get_terms'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'get_terms_filter'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The code work like this. We add a filter to the <strong>get_terms</strong> hook. This hook is called before the terms is returned, so it is perfect to place the filter here. Next, we check each returned terms and see if they have any published post, by using our own query. Why didn&#8217;t I use the WordPress WP_Query instead? The answer is I can&#8217;t get it to work, beside, it builds a pretty complicated query that might increase your WordPress site load time if it is used too much. Using our own query, we make sure it only use the query that we need. If our query returned that the term has a published post, we add this term to our new array which we use to store the filtered terms. Finally, we return the filtered terms.</p>
<p>Now, when you are using <strong>get_terms</strong>, you will filter out all terms that doesn&#8217;t have any published post. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Hope that helps. I have a hard time to explain the situation, so let me know if it doesn&#8217;t clear.</p>
<p>Thank you.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2010/08/filter-get_terms-to-return-only-terms-with-published-post/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CSS Captcha 0.2 beta</title>
		<link>http://jeffri.net/2010/08/css-captcha-0-2-beta/</link>
		<comments>http://jeffri.net/2010/08/css-captcha-0-2-beta/#comments</comments>
		<pubDate>Sun, 08 Aug 2010 19:48:20 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[captcha]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[CSS Captcha]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=597</guid>
		<description><![CDATA[Here I update the CSS Captcha, it is now on 0.2 beta! Thank you for the response of the previous version. While this can work nicely on major new browser, the compatibility with older browser is still minimum. Some mobile browser also haven&#8217;t implement some of CSS3 functionality, such as Opera Mini and Opera Mobile. [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjeffri.net%2F2010%2F08%2Fcss-captcha-0-2-beta%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjeffri.net%2F2010%2F08%2Fcss-captcha-0-2-beta%2F&amp;source=keaglez&amp;style=normal&amp;service=bit.ly&amp;service_api=R_8412a66499172453880523e0cb1bb28b" height="61" width="50" />
			</a>
		</div><p>Here I update the CSS Captcha, it is now on 0.2 beta! Thank you for the response of the previous version. While this can work nicely on major new browser, the compatibility with older browser is still minimum. Some mobile browser also haven&#8217;t implement some of CSS3 functionality, such as Opera Mini and Opera Mobile. So this is still experimental and shouldn&#8217;t be used yet on production website.</p>
<p><img src="http://jeffri.net/wp-content/uploads/csscaptcha0.2.jpg" alt="" title="csscaptcha0.2" width="540" height="189" class="alignnone size-full wp-image-598" /></p>
<p><a href="http://jeffri.net/2010/06/css-captcha-0-1-beta/">Please read the older version information here</a>.</p>
<p>This update is merely a security update. The inline style is randomized now, which make it more difficult for bot to read. The order of the character is also randomized. Sure, it still doesn&#8217;t prevent the bot to read or make it impossible to read at all, but it should give more difficulty on creating such bot. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Still, no support for IE browser at all. <img src='http://jeffri.net/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>Change log 0.2b:<br />
- Automatically randomize inline style<br />
- Automatically randomize character order</p>
<p><a href="http://csscaptcha.keaglez.com">Demonstration</a><br />
<a href="http://csscaptcha.keaglez.com/csscaptcha0.2b.zip">Download</a></p>
<p>Enjoy! Any comment is appreciated.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2010/08/css-captcha-0-2-beta/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Some useful tips for WordPress custom post type and taxonomy</title>
		<link>http://jeffri.net/2010/07/some-useful-tips-for-wordpress-custom-post-type-and-taxonomy/</link>
		<comments>http://jeffri.net/2010/07/some-useful-tips-for-wordpress-custom-post-type-and-taxonomy/#comments</comments>
		<pubDate>Mon, 19 Jul 2010 23:28:08 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=569</guid>
		<description><![CDATA[Custom post type is one of the most powerful WordPress feature. It allows us to create just any content we need, combined with custom taxonomy, the possibility is just endless. WordPress have allowed to have custom post type and taxonomy for some while, but with WordPress 3, everything is far more easier. We have register_post_type [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjeffri.net%2F2010%2F07%2Fsome-useful-tips-for-wordpress-custom-post-type-and-taxonomy%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjeffri.net%2F2010%2F07%2Fsome-useful-tips-for-wordpress-custom-post-type-and-taxonomy%2F&amp;source=keaglez&amp;style=normal&amp;service=bit.ly&amp;service_api=R_8412a66499172453880523e0cb1bb28b" height="61" width="50" />
			</a>
		</div><p>Custom post type is one of the most powerful WordPress feature. It allows us to create just any content we need, combined with custom taxonomy, the possibility is just endless. WordPress have allowed to have custom post type and taxonomy for some while, but with WordPress 3, everything is far more easier. We have <a href="http://codex.wordpress.org/Function_Reference/register_post_type">register_post_type</a> and <a href="http://codex.wordpress.org/Function_Reference/register_taxonomy">register_taxonomy</a> created specifically for this purpose.</p>
<p>While this two combined to be a wonderful addition for your WordPress blog, there is still a lot of things that is not yet documented. Here is a few useful tips I collected when working with custom post type and taxonomy.</p>
<p><span id="more-569"></span></p>
<h2>Add taxonomy to more than one post type</h2>
<p>There is few ways to doing this, depend to your code. If you register the post type first, then when register taxonomy, you&#8217;ll be able to include it to any post types you want.</p>
<p>Example (taken from codex).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">  register_taxonomy<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'genre'</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'magazine'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'post'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'hierarchical'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'labels'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$labels</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'show_ui'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'query_var'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'rewrite'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'slug'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'genre'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>See the line 1 above, the second parameter for register_taxonomy function is the list of post types you can include. It accept both string and array, as you can expect, we use array when we want to include many post type, and use string when you only need one. The same thing is applied if you register the taxonomy first, you can assign any taxonomy you like when register the post type.</p>
<p>Example (taken from codex)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'labels'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$labels</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'public'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'publicly_queryable'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'show_ui'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> 
    <span style="color: #0000ff;">'query_var'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'rewrite'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'capability_type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'post'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'hierarchical'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'menu_position'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'taxonomies'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'genre'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'grade'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'category'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'supports'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'editor'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'author'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'thumbnail'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'excerpt'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'comments'</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  register_post_type<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book'</span><span style="color: #339933;">,</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>As you can see, the <em>taxonomies</em> parameter can be used to assign any taxonomy you like to the post type you just registered. Again, it accept both array and string.</p>
<p>But what if you want to add taxonomy to certain post type later, after they are defined? WordPress have a function to do this, it is <a href="http://codex.wordpress.org/Function_Reference/register_taxonomy_for_object_type">register_taxonomy_for_object_type</a>. Take a look on the example below.</p>
<p>Example</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">register_taxonomy_for_object_type<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'genre'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'book'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Handy, huh?</p>
<h2>Querying more than one post type</h2>
<p>Let&#8217;s say, for example, you have defined post type for book, comic and magazine. Then, somewhere in your web page, you want to show the latest list of all of these post types together. As you might know already, custom post type is not included in normal WordPress loop, so you&#8217;ll need to add your own query and loop, whether by using query_posts, get_posts or by creating a new instance of WP_Query object.</p>
<p>Example</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'post_type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'comic'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'magazine'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'posts_per_page'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
query_posts<span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> the_post<span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// and so on</span>
<span style="color: #b1b100;">endwhile</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>See the line 2. You can add any post type you need in the <em>post_type</em> parameter. As always, it also accept both array and string.</p>
<h2>Querying the custom taxonomy</h2>
<p>You know how to querying multiple post type now, but how if you want to filter it by taxonomy? While post category and tags offer many ways to filter it, the custom taxonomy is still limited. The only way I can find right now is to filter the taxonomy by the slug.</p>
<p>Example</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'post_type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'comic'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'magazine'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'taxonomy'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'genre'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'term'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'science'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'fiction'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'posts_per_page'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
query_posts<span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> the_post<span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// and so on</span>
<span style="color: #b1b100;">endwhile</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>From the example above, I filtered the result by including only science and fiction genre from all three post types we defined. Unfortunately, I haven&#8217;t find any way to filter it by multiple taxonomies at once.</p>
<h2>The custom post type feed</h2>
<p>Custom post type also have its own feed. Depend to your permalink structure, you can find it by either</p>
<blockquote><p>http://www.yourwordpresswebsite.com/?feed=rss2&#038;post_type=book</p></blockquote>
<p>or</p>
<blockquote><p>http://www.yourwordpresswebsite.com/feed/?post_type=book</p></blockquote>
<p>Unfortunately, again, I can&#8217;t find any way to get the feed for multiple post type.</p>
<p>So that&#8217;s it for now. If you find any good tips, you can share it in comment. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2010/07/some-useful-tips-for-wordpress-custom-post-type-and-taxonomy/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Blog redesign</title>
		<link>http://jeffri.net/2010/07/blog-redesign/</link>
		<comments>http://jeffri.net/2010/07/blog-redesign/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 01:46:22 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Designing]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[templates]]></category>
		<category><![CDATA[xhtml]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=553</guid>
		<description><![CDATA[Welcome to my new redesigned blog! It&#8217;s been a year and a half since the last time I changed the design. I have always wanted to change it, but I just always don&#8217;t have enough time. I know I&#8217;m not that good enough in designing, but at least, this is the best design I ever [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjeffri.net%2F2010%2F07%2Fblog-redesign%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjeffri.net%2F2010%2F07%2Fblog-redesign%2F&amp;source=keaglez&amp;style=normal&amp;service=bit.ly&amp;service_api=R_8412a66499172453880523e0cb1bb28b" height="61" width="50" />
			</a>
		</div><p>Welcome to my new redesigned blog! It&#8217;s been a year and a half since the last time I changed the design. I have always wanted to change it, but I just always don&#8217;t have enough time. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  I know I&#8217;m not that good enough in designing, but at least, this is the best design I ever made until now!</p>
<p><img src="http://jeffri.net/wp-content/uploads/blog_design.jpg" alt="" title="blog_design" width="560" height="238" class="alignnone size-full wp-image-554" /></p>
<p>The idea behind this design is, to make a simple, usable layout and still looks beautiful. There is a lot of tutorial on the web that helped me when making this. I try to make the website small in size, so you&#8217;ll able to load it fast enough. There is also a cool trick on the background, which still make the markup small without much wrapping div over div to accomplish the effect. I also realize the importance on social networking nowadays, so I add links to some of my account. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><span id="more-553"></span></p>
<p>With CSS 3 coming, I won&#8217;t forget to add some CSS 3 elements. I add a little transition, on the sidebar links and the social network icon above. You won&#8217;t see it if you don&#8217;t use webkit browser though, so if you use Google Chrome 5 and Safari 5, then you are in. This transition have no importance at all, the purpose is just to enrich the user experience, but won&#8217;t lose any functionality without it. Another CSS 3 property I used is the <em>border-radius</em>. I used this on some input fields you can find. There is also some jQuery here, although I&#8217;m only using it on input fields now, it might become handy in future.</p>
<p>While I haven&#8217;t check this on all browser yet, but I&#8217;m sure it will work fine on all major browser, includes Internet Explorer 8, Mozilla Firefox 3.6, Opera 10, Google Chrome 5 and Safari 5. While I&#8217;m not supporting older Internet Explorer version, it still, on some basic level, works fine. I even can confirm that the IE 7 load almost everything fine, but it just not perfect yet. I have some issues on the logo and button, same as IE 6. This might get fixed soon. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I wish I could write the process on making this design. I might be able write it, but I won&#8217;t promise that. In short, there is 2 steps at least. First is design it in PSD, the second is convert it to WordPress. I&#8217;m skipping the step to convert it to XHTML/CSS first though, well, that&#8217;s just how I work. <img src='http://jeffri.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>I hope you will have one or two minute to type down a comment to share your opinion, so I can get better. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Thanks!</p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2010/07/blog-redesign/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Checking the current post type with is_singular function</title>
		<link>http://jeffri.net/2010/07/checking-the-current-post-type-with-is_singular-function/</link>
		<comments>http://jeffri.net/2010/07/checking-the-current-post-type-with-is_singular-function/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 01:38:50 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=541</guid>
		<description><![CDATA[The WordPress 3 introduce a new feature that let us easily make our own custom post type. This is really a cool feature that give more possibility to customize the WordPress powered website. However, I find that the documentation is not complete yet. In the recent problem I got, it requires me to check the [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjeffri.net%2F2010%2F07%2Fchecking-the-current-post-type-with-is_singular-function%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjeffri.net%2F2010%2F07%2Fchecking-the-current-post-type-with-is_singular-function%2F&amp;source=keaglez&amp;style=normal&amp;service=bit.ly&amp;service_api=R_8412a66499172453880523e0cb1bb28b" height="61" width="50" />
			</a>
		</div><p>The WordPress 3 introduce a new feature that let us easily make our own custom post type. This is really a cool feature that give more possibility to customize the WordPress powered website. However, I find that the documentation is not complete yet.</p>
<p>In the recent problem I got, it requires me to check the current post type, something like <a href="http://codex.wordpress.org/Function_Reference/is_single">is_single</a> for post and <a href="http://codex.wordpress.org/Function_Reference/is_page">is_page</a> for page. Luckily enough, the <a href="http://codex.wordpress.org/Function_Reference/is_singular">is_singular</a> is now changed to accept a parameter, and that is post type. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>In current codex page, it still not updated. However, now we can use this to check whether we are on our post type page or not.</p>
<blockquote><p>
if ( is_singular(&#8220;my-post-type&#8221;) )<br />
    // whatever you want to do here
</p></blockquote>
<p>Great!</p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2010/07/checking-the-current-post-type-with-is_singular-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CSS Captcha 0.1 beta</title>
		<link>http://jeffri.net/2010/06/css-captcha-0-1-beta/</link>
		<comments>http://jeffri.net/2010/06/css-captcha-0-1-beta/#comments</comments>
		<pubDate>Wed, 23 Jun 2010 19:01:41 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[captcha]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[CSS Captcha]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=522</guid>
		<description><![CDATA[With coming of the CSS 3, it is now possible to draw using merely HTML and CSS. After seeing some awesome work that can be done with CSS 3, one of my friend suddenly get an idea to create a captcha. Thinking that bot today might haven&#8217;t able to pass this kind of captcha, I [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjeffri.net%2F2010%2F06%2Fcss-captcha-0-1-beta%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjeffri.net%2F2010%2F06%2Fcss-captcha-0-1-beta%2F&amp;source=keaglez&amp;style=normal&amp;service=bit.ly&amp;service_api=R_8412a66499172453880523e0cb1bb28b" height="61" width="50" />
			</a>
		</div><p><img src="http://jeffri.net/wp-content/uploads/csscaptcha.jpg" alt="" title="CSS Captcha" width="282" height="134" class="alignnone size-full wp-image-523" /></p>
<p>With coming of the CSS 3, it is now possible to draw using merely HTML and CSS. After seeing some awesome work that can be done with CSS 3, one of my friend suddenly get an idea to create a captcha. Thinking that bot today might haven&#8217;t able to pass this kind of captcha, I created this, CSS Captcha. The captcha that didn&#8217;t use any image. It just simply using HTML and CSS to draw characters in your screen. I might be not the first one to create this though. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a href="http://csscaptcha.keaglez.com">Click here for demonstration</a></p>
<p><span id="more-522"></span></p>
<p>This is a very simple captcha. Giving the ability of CSS 3, we can create a better and more eye-candy captcha soon. Yeah, we know that it is possible to do animation in CSS 3. There is also some useful pseudo state in CSS, such as <em>:hover</em> (my favorite!), imagine the captcha that need a mouse hover to be completely seen! That might be impossible for bot.</p>
<p>However, the browser support is still minimum. Internet Explorer, on other hand, didn&#8217;t support CSS 3 until the version 9. So we still need to wait for a while until IE 9 officially released. Also, browser support for CSS 3 currently is still using browser specific property. So cross browser compability is a pain. Fortunately, I add a feature to automatically add browser specific properties, as long as one of the property is there, either using the browser specific property or from CSS specification. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Feature:<br />
- Render captcha using HTML and CSS alone<br />
- Rotation and scaling support<br />
- Multiple text color (chosen randomly)<br />
- Changeable background color<br />
- Adjustable length (maximum 32 character)<br />
- Changeable template/font<br />
- Automatically add browser specific properties for cross compability<br />
- Lightweight</p>
<p>The good:<br />
- Since it only output the HTML and CSS, there is no much processing in the server<br />
- It outputs directly to the HTML, there is no external resource needed at all, so it loads at the same time the page loaded</p>
<p>Downside:<br />
- Support for Internet Explorer is minimum or no at all<br />
- Rotation and scaling break the text a little (still readable), since it is only smart placement of multiple div element</p>
<p>Supported browser:<br />
- Google Chrome 5.0<br />
- Mozilla Firefox 3.6<br />
- Opera 10.5<br />
- Safari 5.0</p>
<p>Server Requirement:<br />
- PHP 5</p>
<p>License: GNU GPL</p>
<p><a href="http://csscaptcha.keaglez.com/csscaptcha0.1b.zip" title="Download">Download</a></p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2010/06/css-captcha-0-1-beta/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Moving Stick Figure with CSS 3 and jQuery</title>
		<link>http://jeffri.net/2010/06/moving-stick-figure-with-css-3-and-jquery/</link>
		<comments>http://jeffri.net/2010/06/moving-stick-figure-with-css-3-and-jquery/#comments</comments>
		<pubDate>Tue, 22 Jun 2010 11:23:21 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Designing]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[CSS 3]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[JQuery]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=517</guid>
		<description><![CDATA[With CSS 3, creating a stick figure is easy and fun. But it is even better if it can move. So here it is Click here for the demo. This currently worked on Google Chrome and Safari. However, in my test, Safari lagged a lot in the animation. So Google Chrome performed the best! Enjoy&#8230; [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjeffri.net%2F2010%2F06%2Fmoving-stick-figure-with-css-3-and-jquery%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjeffri.net%2F2010%2F06%2Fmoving-stick-figure-with-css-3-and-jquery%2F&amp;source=keaglez&amp;style=normal&amp;service=bit.ly&amp;service_api=R_8412a66499172453880523e0cb1bb28b" height="61" width="50" />
			</a>
		</div><p>With CSS 3, creating a stick figure is easy and fun. But it is even better if it can move. So here it is</p>
<p><img src="http://jeffri.net/wp-content/uploads/stickfigure.jpg" alt="" title="Stick Figure" width="400" height="161" class="alignnone size-full wp-image-518" /></p>
<p><a href="http://www.keaglez.com/projects/stickfigure">Click here for the demo.</a></p>
<p>This currently worked on Google Chrome and Safari. However, in my test, Safari lagged a lot in the animation. So Google Chrome performed the best!</p>
<p>Enjoy&#8230; <img src='http://jeffri.net/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>Update: This is also submitted on <a href="http://www.smashingmagazine.com/2010/07/12/css3-design-contest-results">Smashing Magazine CSS 3 Contest</a>, you can preview and download from there too. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2010/06/moving-stick-figure-with-css-3-and-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pure CSS Dropdown with a Little CSS 3 Juice</title>
		<link>http://jeffri.net/2010/06/pure-css-dropdown-with-a-little-css-3-juice/</link>
		<comments>http://jeffri.net/2010/06/pure-css-dropdown-with-a-little-css-3-juice/#comments</comments>
		<pubDate>Mon, 21 Jun 2010 08:28:55 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Designing]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[menu]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=502</guid>
		<description><![CDATA[Menu dropdown is one of the feature that almost every today webs have and it is one of the most popular feature used in the web. In past, such feature need Javascript event. Such as using onMouseOver or onMouseOut event. However, since CSS introduced, the new psuedo classes now have this ability. Also, this solution [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjeffri.net%2F2010%2F06%2Fpure-css-dropdown-with-a-little-css-3-juice%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjeffri.net%2F2010%2F06%2Fpure-css-dropdown-with-a-little-css-3-juice%2F&amp;source=keaglez&amp;style=normal&amp;service=bit.ly&amp;service_api=R_8412a66499172453880523e0cb1bb28b" height="61" width="50" />
			</a>
		</div><p>Menu dropdown is one of the feature that almost every today webs have and it is one of the most popular feature used in the web. In past, such feature need Javascript event. Such as using <em>onMouseOver</em> or <em>onMouseOut</em> event. However, since CSS introduced, the new psuedo classes now have this ability. Also, this solution is work for all major browser, except, of course for the CSS3 juice added, which is currently only worked on web-kit browser such as Chrome and Safari. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><span id="more-502"></span></p>
<p>Here is the menu result</p>
<style type="text/css">
	#example-dropdown-menu
	{
		background: #000;
		height: 30px;
		list-style-type: none;
		margin: 0;
		padding: 0;
	}
	#example-dropdown-menu > li
	{
		background: #333;
		float: left;
		height: 30px;
		line-height: 30px;
		margin: 0;
		padding: 0 15px;
		position: relative;
	}
	#example-dropdown-menu > li:hover
	{
		background: #666;
	}
	#example-dropdown-menu > li > a
	{
		text-decoration: none;
		color: #fff;
		display: block;
	}
	#example-dropdown-menu > li > ul
	{
		list-style-type: none;
		background: #f6f6f6;
		border-left: 1px solid #666;
		border-right: 1px solid #666;
		border-bottom: 1px solid #666;
		border-bottom-left-radius: 5px 5px;
		border-bottom-right-radius: 5px 5px;
		-moz-border-radius-bottomright: 5px 5px;
		-moz-border-radius-bottomleft: 5px 5px;
		position: absolute;
		width: 150px;
		margin: 0;
		padding: 0;
		top: 30px;
		left: 0;
		opacity: 0;
		-webkit-transition-property: opacity;
		-webkit-transition-duration: 0.5s;
		visibility: hidden;
	}
	#example-dropdown-menu > li:hover > ul
	{
		opacity: 1;
		visibility: visible;
	}
	#example-dropdown-menu > li > ul > li
	{
		height: 25px;
		line-height: 25px;
		padding: 0 15px;
	}
	#example-dropdown-menu > li > ul > li:hover
	{
		background: #666;
	}
	#example-dropdown-menu > li > ul > li > a
	{
		text-decoration: none;
		color: #666;
		display: block;
	}
	#example-dropdown-menu > li > ul > li:hover > a
	{
		color: #fff;
	}
</style>
<div style="height:120px;padding:10px;">
<ul id="example-dropdown-menu">
<li>
		<a href="#">Menu 1</a></p>
<ul>
<li><a href="#">Sub Menu 1</a></li>
<li><a href="#">Sub Menu 2</a></li>
<li><a href="#">Sub Menu 3</a></li>
</ul>
</li>
<li>
		<a href="#">Menu 2</a></p>
<ul>
<li><a href="#">Sub Menu 1</a></li>
<li><a href="#">Sub Menu 2</a></li>
<li><a href="#">Sub Menu 3</a></li>
</ul>
</li>
<li>
		<a href="#">Menu 3</a></p>
<ul>
<li><a href="#">Sub Menu 1</a></li>
<li><a href="#">Sub Menu 2</a></li>
<li><a href="#">Sub Menu 3</a></li>
</ul>
</li>
</ul>
</div>
<div style="clear:both;"></div>
<p>This is pure CSS, without any single Javascript involved. If you was using webkit browser, you will see a nice transition effect. The CSS 3 juice added here is <em>border-radius</em>, <em>transition-property</em> and <em>transition-duration</em>.</p>
<p>The trick for dropdown is done by using relative and absolute positioning. As you might know already, absolute position element is positioned relative to the parent that has position other than static. So, if we have relative position as the parent, absolute position element will get the position relative to the parent.</p>
<p>To hide and show the absolute child element, we use the <em>:hover</em> state. This worked fine on all major browser, except for the old IE 6, since the IE 6 can&#8217;t have <em>:hover</em> state work on all other element than <em>a</em> (anchor tag).</p>
<p>Now that CSS 3 has come, why bother with old browser anymore? <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2010/06/pure-css-dropdown-with-a-little-css-3-juice/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
