Question: Adjust the code in the new lab.php file (download it from the books web site). Add code to filter for bad gender code and make
Adjust the code in the new lab.php file (download it from the books web site). Add code to filter for bad gender code and make sure that gender information has been received from the HTML file. Make sure to pass your property through the clean_input method to remove any harmful data.
Lab.php file:
Require_once("e312dog.php");
$lab = new Dog('Fred','Lab','Yellow','100');
list($name_error, $breed_error, $color_error, $weight_error) = explode(',', $lab);
print $name_error == 'TRUE' ? 'Name update successful
' : 'Name update not successful
';
print $breed_error == 'TRUE' ? 'Breed update successful
' : 'Breed update not successful
';
print $color_error == 'TRUE' ? 'Color update successful
' : 'Color update not successful
';
print $weight_error == 'TRUE' ? 'Weight update successful
' : 'Weight update not successful
';
// ------------------------------Set Properties--------------------------
$dog_error_message = $lab->set_dog_name('Sally');
print $dog_error_message == TRUE ? 'Name update successful
' : 'Name update not successful
';
$dog_error_message = $lab->set_dog_weight('5');
print $dog_error_message == TRUE ? 'Weight update successful
' : 'Weight update not successful
';
$dog_error_message = $lab->set_dog_breed('Labrador');
print $dog_error_message == TRUE ? 'Breed update successful
' : 'Breed update not successful
';
$dog_error_message = $lab->set_dog_color('Brown');
print $dog_error_message == TRUE ? 'Color update successful
' : 'Color update not successful
';
// ------------------------------Get Properties--------------------------
print $lab->get_dog_name() . "
";
print $lab->get_dog_weight() . "
";
print $lab->get_dog_breed() . "
";
print $lab->get_dog_color() . "
";
$dog_properties = $lab->get_properties();
list($dog_weight, $dog_breed, $dog_color) = explode(',', $dog_properties);
print "Dog weight is $dog_weight. Dog breed is $dog_breed. Dog color is $dog_color.";
?>
Example 4-3. Partial listing of the top of lab.php with the clean_input method.
Require_once("e40dog.php");
function clean_input($value)
{
$value = htmlentities($value);
// Removes any html from the string and turns it into < format
$value = strip_tags($value);
$bad_chars = array("{", "}", "(", ")", ";", ":", "<", ">", "/", "$");
$value = str_ireplace($bad_chars,"",$value);
if (get_magic_quotes_gpc())
{
$value = stripslashes($value);
// Gets rid of unwanted slashes
}
$value = htmlentities($value);
// Removes any html from the string and turns it into < format
$value = strip_tags($value);
return $value;
}
if ((isset($_POST['dog_name'])) && (isset($_POST['dog_breed'])) && (isset($_POST['dog_color'])) && (isset($_POST['dog_weight'])))
{
$dog_name = clean_input($_POST['dog_name']);
$dog_breed = clean_input($_POST['dog_breed']);
$dog_color = clean_input($_POST['dog_color']);
$dog_weight = clean_input($_POST['dog_weight']);
$lab = new Dog($dog_name,$dog_breed,$dog_color,$dog_weight);
list($name_error, $breed_error, $color_error, $weight_error) = explode(',', $lab);
print $name_error == 'TRUE' ? 'Name update successful
' : 'Name update not successful
';
print $breed_error == 'TRUE' ? 'Breed update successful
' : 'Breed update not successful
';
print $color_error == 'TRUE' ? 'Color update successful
' : 'Color update not successful
';
print $weight_error == 'TRUE' ? 'Weight update successful
' : 'Weight update not successful
';
$dog_properties = $lab->get_properties();
list($dog_weight, $dog_breed, $dog_color) = explode(',', $dog_properties);
print "Dog weight is $dog_weight. Dog breed is $dog_breed. Dog color is $dog_color.";
}
else
{
print "
Missing or invalid parameters. Please go back to the lab.html page to enter valid information.
";
print "Dog Creation Page";
}
?>
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
