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 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