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
   354   355   356   357   358   359   360   361   362   363   364