Page 359 - Open Soource Technologies 304.indd
P. 359
Unit 14: Security
The following ensures that $_POST[‘color’] is red, green, or blue: Notes
<?php
$clean = array();
switch ($_POST[‘color’])
{
case ‘red’:
case ‘green’:
case ‘blue’:
$clean[‘color’] = $_POST[‘color’];
break;
}
?>
The following example ensures that $_POST[‘num’] is an integer:
<?php
$clean = array();
if ($_POST[‘num’] == strval(intval($_POST[‘num’])))
{
$clean[‘num’] = $_POST[‘num’];
}
?>
The following example ensures that $_POST[‘num’] is a float:
<?php
$clean = array();
if ($_POST[‘num’] == strval(floatval($_POST[‘num’])))
{
$clean[‘num’] = $_POST[‘num’];
}
?>
LOVELY PROFESSIONAL UNIVERSITY 353