{"id":238,"date":"2014-07-03T04:25:58","date_gmt":"2014-07-03T12:25:58","guid":{"rendered":"http:\/\/www.tech.dimprash.com\/?p=238"},"modified":"2014-08-19T22:23:11","modified_gmt":"2014-08-20T06:23:11","slug":"input-validation","status":"publish","type":"post","link":"http:\/\/www.tech.dimprash.com\/?p=238","title":{"rendered":"Input Validation"},"content":{"rendered":"<p>1) Use in built functions<br \/>\nfilter_input, filter_var, filter_input_array <\/p>\n<p>$search_html = filter_input(INPUT_GET, &#8216;search&#8217;, FILTER_SANITIZE_SPECIAL_CHARS);<\/p>\n<p>$args = array(<br \/>\n    &#8216;product_id&#8217;   => FILTER_SANITIZE_ENCODED,<br \/>\n    &#8216;component&#8217;    => array(&#8216;filter&#8217;    => FILTER_VALIDATE_INT,<br \/>\n                            &#8216;flags&#8217;     => FILTER_REQUIRE_ARRAY,<br \/>\n                            &#8216;options&#8217;   => array(&#8216;min_range&#8217; => 1, &#8216;max_range&#8217; => 10)<br \/>\n                           ),<br \/>\n    &#8216;versions&#8217;     => FILTER_SANITIZE_ENCODED,<br \/>\n    &#8216;doesnotexist&#8217; => FILTER_VALIDATE_INT,<br \/>\n    &#8216;testscalar&#8217;   => array(<br \/>\n                            &#8216;filter&#8217; => FILTER_VALIDATE_INT,<br \/>\n                            &#8216;flags&#8217;  => FILTER_REQUIRE_SCALAR,<br \/>\n                           ),<br \/>\n    &#8216;testarray&#8217;    => array(<br \/>\n                            &#8216;filter&#8217; => FILTER_VALIDATE_INT,<br \/>\n                            &#8216;flags&#8217;  => FILTER_REQUIRE_ARRAY,<br \/>\n                           )<\/p>\n<p>);<br \/>\n$myinputs = filter_input_array(INPUT_POST, $args);<\/p>\n<p>2) htmlspecialchars()<br \/>\nThe htmlspecialchars() function converts special characters to HTML entities. This means that it will replace HTML characters like < and > with &lt; and &gt;. This prevents attackers from exploiting the code by injecting HTML or Javascript code (Cross-site Scripting attacks) in forms.<\/p>\n<p>3) stripslashes($data)<\/p>\n<p>4) Use PHP Data Objects (PDO) for binding query parameters and prepared statements to avoid sql injection:  PDO provides a data-access abstraction layer, which means that, regardless of which database you&#8217;re using, you use the same functions to issue queries and fetch data.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1) Use in built functions filter_input, filter_var, filter_input_array $search_html = filter_input(INPUT_GET, &#8216;search&#8217;, FILTER_SANITIZE_SPECIAL_CHARS); $args = array( &#8216;product_id&#8217; => FILTER_SANITIZE_ENCODED, &#8216;component&#8217; => array(&#8216;filter&#8217; => FILTER_VALIDATE_INT, &#8216;flags&#8217; => FILTER_REQUIRE_ARRAY, &#8216;options&#8217; => array(&#8216;min_range&#8217; => 1, &#8216;max_range&#8217; => 10) ), &#8216;versions&#8217; => FILTER_SANITIZE_ENCODED, &#8216;doesnotexist&#8217; => FILTER_VALIDATE_INT, &#8216;testscalar&#8217; => array( &#8216;filter&#8217; => FILTER_VALIDATE_INT, &#8216;flags&#8217; => FILTER_REQUIRE_SCALAR, ), &#8216;testarray&#8217; => array( &hellip; <a href=\"http:\/\/www.tech.dimprash.com\/?p=238\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Input Validation<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-238","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=238"}],"version-history":[{"count":7,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/238\/revisions"}],"predecessor-version":[{"id":240,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/238\/revisions\/240"}],"wp:attachment":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=238"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}