Twitter API with Zend Part 1. Post Moved to ReinforceMedia.com
Sphere: Related ContentUsing the Twitter API with Zend Part 1: Setting up your environment
September 10th, 2009 § 0
This is not form validation — and how to fix it
August 31st, 2009 § 0
I was working on a site and noticed something that concerned me. This is fodder for TheDailyWTF and makes me worry for the fate of any person who receives the results of forms with [non-]“validation” like this.
The form element:
-
<select name="favorite-color">
-
<option>Favorite Color – Choose One</option>
-
<option value="blue">Blue</option>
-
<option value="red">Red</option>
-
<option value="orange">Orange</option>
-
<option value="green">Green</option>
-
<option value="black">Black</option>
-
</select>
Here is what was in the processing script:
-
switch ($_POST["favorite-color"])
-
{
-
case "blue":
-
case "red":
-
case "orange":
-
case "green":
-
case "black":
-
$data["favorite-color"] = $_POST["favorite-color"];
-
break;
-
}
-
…
-
//data is appended to a string – matt's comment not a "real" comment in the script
-
$string .= $data['favorite-color'];
So… What is wrong with this? OMG what isn’t wrong with it?
- The select element has an “option” with no value
- The $data array is never initialized (I didn’t show this, but are you suprised?)
- The no value option is not accounted for in the processing: i.e.
If no value is passed, then $data['favorite-color'] is never set - The variable that would be set in processing, is then used in a string. If there is no value, PHP throws an error for accessing an array index that does not exist
- It is assumed that ONLY the values in the select will ever be passed to the processing and completely ignores the null value.
- Why in the hell would you want to type every single possible option that could be passed in the processing script? Especially when you are just going to append it to a string!!
- The string concatenation assumes that the variable being concatenated is already set.
So, how do we fix this?
Actually, it is not all that hard.
First, you have a choice. You can choose to add a value to the first option element, or you can handle the fact that it has no value in the processing, or you can do both (I suggest this one).
-
<!– You can output processing errors here –>
-
<select name="favorite-color">
-
<option value="0">Favorite Color – Choose One</option>
-
<option value="blue">Blue</option>
-
<option value="red">Red</option>
-
<option value="orange">Orange</option>
-
<option value="green">Green</option>
-
<option value="black">Black</option>
-
</select>
Now handle the processing:
-
//initialize the $data array
-
$data = array():
-
//initialize the $string variable
-
$string = '';
-
…
-
/**
-
* This code will handle the processing of the field
-
* It looks to see if the value is set, and not empty. It will use the non-empty value or it
-
* will set an empty string
-
*/
-
if (isset($_POST['favorite-color']) && !empty($_POST['favorite-color'])) {
-
$data['favorite-color'] = $_POST['favorite-color'];
-
/**
-
* if you were going to do a mysql db insert instead of string concatenation you
-
* would do something like:
-
* $data['favorite-color'] = mysql_real_escape_string($_POST['favorite-color']);
-
*/
-
} else {
-
//This is what was forgotten before
-
$data['favorite-color'] = ''; // or $data['favorite-color'] = null;
-
/**
-
* you could also set an error variable, which could be passed back to the form output
-
* to show the user what they missed or did wrong.
-
*/
-
}
Now that you set up your “validation” – which is really just a way to initialize the variable you will be writing to the string into the $data array.
So, all that is left is to concatenate the value to the string. You can do one of two things. The first is to just concatenate the string, since you know it has already been initialized properly:
-
// you could add checks right here to see if there are errors and redirect back to the form before creating the string
-
…
-
//concatenate the value to the string
-
$string .= $data['favorite-color'];
The second way is to use a ternary operator (READ: inline if statement) to append the string:
-
// you could add checks right here to see if there are errors and redirect back to the form before creating the string
-
…
-
//append the string if it has a length greater than zero, otherwise append empty string
-
$string .= (strlen($data['favorite-color']) > 0 ? $data['favorite-color'] : '');
This method of handling form elements takes a little more time, requires a little more effort up front, but it will save you WAY more trouble later on. You will not see the PHP errors that you saw before and if you are inserting the data into a DB you will prevent your users from being able to use SQL injection attacks on you.
Sphere: Related ContentUsername URLs just like Twitter
August 14th, 2009 § 0
In your .htaccess just do this:
-
RewriteEngine on
-
RewriteRule ^([A-za-z0-9]+)/?$ user.php?name=$1
This will ignore php files, and any files that are requested deeper into the site like JS or CSS.
Sphere: Related ContentThe AOL logo and the Genius form error are a little confusing
February 7th, 2009 § 0
I was setting up Genius in Itunes tonight and I got to the form below, and didnt fill out the second input because it looked like they wanted AOL credentials. Then, when the error came up, the red arrow and the blue triangle point to the same place, kind of confusing.
Now, I know that if I would have just paid more attention, I would have gotten this from the start. Even so, I sometimes stop paying attention and hope that the interface will be intuitive enough that I can just use it without thinking. I know, lame right? Not necessarily, I have found that this is a good way to test out user functionality.
If you have to stop and think about how to make the interface work, then there is something wrong. The idea is that you want the users who pay full attention to find things that are especially for them, and for the other users you want them to be able skate by.
Sphere: Related ContentTrim, I love your service, but please sanitize your inputs!!
February 5th, 2009 § 0
I was replying to a friend on Twitter using trim, and I had a <script> tag in the post. I realized when I submitted that the tag made everything after it in my tweet dissapear. If you want to see the actual tweets, you can find them in my twitter feed here: Matt Bernier’s Twitter Feed
First thougt was, “No Way!”. Second thought was, “What Else Can I do?”.
So, I tried basic HTML with this tweet:
-
<h2>Testing whether HTML breaks tr.im</h2> B/c my
-
<script> tag did earlier</script>
-
<span style="color:blue;"> ScreenShot coming</script>
This got me this result:
Then I tried an alert:
-
<script type="text/javascript">
-
alert('does this work?');
-
</script>
That got me this result:
Then lastly, I tried a little more JS, pay attention though. To make it fit, I used a tr.im URL!!
-
<script type="text/javascript">
-
document.body.select('img').each(function(e){e.src="http://tr.im/evmz"});
-
alert('check the images')
-
</script>
Which got me this result:
I have submitted this information to tr.im. I did very mundane, topical things to the page I was looking at, and did not even attempt anything more dangerous. My hope is that you will see the humor in this, urge tr.im to fix this issue and to continue the amazing job that they do.
UPDATE: The Tr.im developers are quick to read their emails, respond, and fix issues. It took all of twenty minutes from when I sent the email to them, for a response saying that this issue was fixed.
Sphere: Related ContentServices
January 18th, 2009 § 0
I currently provide a range of services in the area of web development and business consulting. If you are interested in these services, please visit my “Services” page.
My current services include:
- Web Development
- Proposal Consulting
- Website Consulting
- SEO Consulting
Contact me if you would like to talk to me about the services I can provide your company.
Sphere: Related Content


