<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Rodrigo Javornik</title>
    <description>The latest articles on Forem by Rodrigo Javornik (@rodrigojavornik).</description>
    <link>https://forem.com/rodrigojavornik</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F715003%2Fd8e0e081-bccc-4d64-8c79-9bafbef3e776.jpeg</url>
      <title>Forem: Rodrigo Javornik</title>
      <link>https://forem.com/rodrigojavornik</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rodrigojavornik"/>
    <language>en</language>
    <item>
      <title>XSS Attack - Why strip_tags is not enough</title>
      <dc:creator>Rodrigo Javornik</dc:creator>
      <pubDate>Wed, 13 Sep 2023 20:05:20 +0000</pubDate>
      <link>https://forem.com/rodrigojavornik/xss-attack-why-striptags-is-not-enough-5gmo</link>
      <guid>https://forem.com/rodrigojavornik/xss-attack-why-striptags-is-not-enough-5gmo</guid>
      <description>&lt;p&gt;In PHP, it is common to use the &lt;code&gt;strip_tags()&lt;/code&gt; function as a way to prevent XSS intrusion. However, this function does not even work to mitigate this type of attack, giving a false sense of security. But why?&lt;/p&gt;

&lt;h2&gt;
  
  
  What is XSS?
&lt;/h2&gt;

&lt;p&gt;XSS (Cross-Site Scripting) is a form of attack that occurs when an attacker exploits a vulnerability in a web application to insert malicious scripts into its pages. These scripts are executed in the browsers of the application's users and can compromise sensitive information, allow session theft, redirect to other sites, etc.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffeo4fipn297d3w3yq0yj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffeo4fipn297d3w3yq0yj.png" alt="how xss attack works"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why strip_tags don't work?
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;strip_tags()&lt;/code&gt; function is commonly used to remove HTML and PHP tags from a string. However, it is not designed to handle all forms of malicious input that can lead to XSS (Cross-Site Scripting) attacks. &lt;/p&gt;

&lt;p&gt;Here are some reasons why &lt;code&gt;strip_tags()&lt;/code&gt; falls short in mitigating XSS attacks:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Attribute-based attacks:&lt;/strong&gt; XSS attacks can occur through attributes such as onmouseover or onclick, which can execute JavaScript code when triggered. &lt;code&gt;strip_tags()&lt;/code&gt; does not remove or sanitize these attributes, allowing potential XSS vulnerabilities to remain.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tag obfuscation:&lt;/strong&gt; Attackers can obfuscate the HTML tags and their attributes to bypass &lt;code&gt;strip_tags()&lt;/code&gt;. They can use techniques such as mixing case variations, HTML entity encoding, or JavaScript-based obfuscation. &lt;code&gt;strip_tags()&lt;/code&gt; alone cannot effectively handle these obfuscated tags.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Context-awareness:&lt;/strong&gt; XSS vulnerabilities can vary depending on the context in which the user input is displayed. &lt;code&gt;strip_tags()&lt;/code&gt; does not have knowledge of the specific context and may allow certain tags or attributes that can still lead to XSS attacks.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;An example of malicious string that can be used in an XSS attack is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this is a XSS attack &amp;lt;script&amp;gt;alert(“hello world”)&amp;lt;script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If we apply the &lt;code&gt;strip_tags()&lt;/code&gt; function, we obtain the following result:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this is a XSS attack alert(“hello world”)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Okay, in this case, it was indeed possible to clean the malicious code from the string. However, the attacker can use the following code:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this is a XSS attack &amp;amp;lt;script&amp;amp;gt; alert('oi') &amp;amp;lt;/script&amp;amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The &lt;code&gt;strip_tags()&lt;/code&gt; function will not sanitize the string in a way that prevents the injection of code into the page.&lt;/p&gt;
&lt;h2&gt;
  
  
  How to prevent it?
&lt;/h2&gt;

&lt;p&gt;The good way to deal with untrusted data is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Filter on input, escape on output&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This means that you handle the received data (filter), but only transform it (escape or encode) when you send it as output to another system that requires encoding.&lt;/p&gt;

&lt;p&gt;There is no way around it. In the data sanitization phase, the only way to effectively prevent XSS attacks is by using a specific library, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/voku/anti-xss" rel="noopener noreferrer"&gt;AntiXSS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://htmlpurifier.org/" rel="noopener noreferrer"&gt;HTML Purifier&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These libraries provide robust mechanisms for preventing XSS attacks by sanitizing and properly handling user input and output.&lt;/p&gt;

&lt;p&gt;Here, we are going to use the AntiXSS library.&lt;br&gt;
Now we can sanitize our strings in a much safer way:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;voku\helper\AntiXSS&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;require_once&lt;/span&gt; &lt;span class="k"&gt;__DIR__&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;'/vendor/autoload.php'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$antiXss&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AntiXSS&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nv"&gt;$xssString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"this is a XSS attack &amp;amp;lt;script&amp;amp;gt; alert('oi') &amp;amp;lt;/script&amp;amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$clearString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$antiXss&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;xss_clean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$xssString&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//this is a XSS attack&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$clearString&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In the phase of outputting data, you can use template engines like &lt;a href="https://twig.symfony.com" rel="noopener noreferrer"&gt;Twig&lt;/a&gt; or &lt;a href="https://laravel.com/docs/10.x/blade" rel="noopener noreferrer"&gt;Blade&lt;/a&gt; or &lt;a href="https://www.php.net/manual/en/function.htmlspecialchars.php" rel="noopener noreferrer"&gt;htmlspecialchars&lt;/a&gt; function.&lt;/p&gt;

&lt;p&gt;Great! Now we have a good way to sanitize XSS.&lt;/p&gt;

&lt;p&gt;It's worth mentioning that sanitization is just one of the steps in preventing XSS. But that is a topic for another text...&lt;/p&gt;


&lt;h3&gt;
  
  
  Do you like data sanitization? Then take a look at my PHP data sanitization library!
&lt;/h3&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/rodrigojavornik" rel="noopener noreferrer"&gt;
        rodrigojavornik
      &lt;/a&gt; / &lt;a href="https://github.com/rodrigojavornik/PHPCleanup" rel="noopener noreferrer"&gt;
        PHPCleanup
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      A PHP Sanitation Library
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;PHP Cleanup&lt;/h1&gt;
&lt;/div&gt;

&lt;div class="markdown-heading"&gt;
&lt;h4 class="heading-element"&gt;A powerful sanitization library for PHP and Laravel. No dependencies&lt;/h4&gt;
&lt;/div&gt;

&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Installation&lt;/h2&gt;
&lt;/div&gt;

&lt;div class="highlight highlight-source-shell notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;composer require rodrigojavornik/php-cleanup&lt;/pre&gt;

&lt;/div&gt;

&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Usage&lt;/h2&gt;

&lt;/div&gt;

&lt;div class="highlight highlight-text-html-php notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;&lt;span class="pl-k"&gt;use&lt;/span&gt; &lt;span class="pl-v"&gt;PHPCleanup&lt;/span&gt;\&lt;span class="pl-v"&gt;Sanitize&lt;/span&gt;;

&lt;span class="pl-v"&gt;Sanitize&lt;/span&gt;::&lt;span class="pl-en"&gt;input&lt;/span&gt;()-&amp;gt;&lt;span class="pl-en"&gt;sanitize&lt;/span&gt;(&lt;span class="pl-s"&gt;'&lt;span class="pl-s"&gt; &amp;lt;h1&amp;gt;Hello World&amp;lt;/h1&amp;gt; &lt;/span&gt;'&lt;/span&gt;);&lt;span class="pl-c"&gt;//Hello World&lt;/span&gt;
&lt;span class="pl-v"&gt;Sanitize&lt;/span&gt;::&lt;span class="pl-en"&gt;trim&lt;/span&gt;()-&amp;gt;&lt;span class="pl-en"&gt;captalize&lt;/span&gt;()-&amp;gt;&lt;span class="pl-en"&gt;sanitize&lt;/span&gt;(&lt;span class="pl-s"&gt;'&lt;span class="pl-s"&gt; string    &lt;/span&gt;'&lt;/span&gt;);&lt;span class="pl-c"&gt;//String&lt;/span&gt;
&lt;span class="pl-v"&gt;Sanitize&lt;/span&gt;::&lt;span class="pl-en"&gt;trim&lt;/span&gt;()-&amp;gt;&lt;span class="pl-en"&gt;lowercase&lt;/span&gt;()-&amp;gt;&lt;span class="pl-en"&gt;sanitize&lt;/span&gt;(&lt;span class="pl-s"&gt;'&lt;span class="pl-s"&gt; MY name IS    &lt;/span&gt;'&lt;/span&gt;);&lt;span class="pl-c"&gt;//my name is&lt;/span&gt;
&lt;span class="pl-v"&gt;Sanitize&lt;/span&gt;::&lt;span class="pl-en"&gt;onlyNumbers&lt;/span&gt;()-&amp;gt;&lt;span class="pl-en"&gt;sanitize&lt;/span&gt;(&lt;span class="pl-s"&gt;'&lt;span class="pl-s"&gt; abc1234&lt;/span&gt;'&lt;/span&gt;);&lt;span class="pl-c"&gt;//1234&lt;/span&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Available filters&lt;/h2&gt;

&lt;/div&gt;


&lt;ul&gt;

&lt;li&gt;

&lt;a href="https://github.com/rodrigojavornik/PHPCleanup#captalize" rel="noopener noreferrer"&gt;captalize&lt;/a&gt;: Capitalize a string;&lt;/li&gt;

&lt;li&gt;

&lt;a href="https://github.com/rodrigojavornik/PHPCleanup#captalizeall" rel="noopener noreferrer"&gt;captalizeAll&lt;/a&gt;: Capitalize all string;&lt;/li&gt;

&lt;li&gt;

&lt;a href="https://github.com/rodrigojavornik/PHPCleanup#datetime" rel="noopener noreferrer"&gt;dateTime&lt;/a&gt;: Transform a string in DateTime object;&lt;/li&gt;

&lt;li&gt;

&lt;a href="https://github.com/rodrigojavornik/PHPCleanup#email" rel="noopener noreferrer"&gt;email&lt;/a&gt;: Removes all characters not allowed in an email address;&lt;/li&gt;

&lt;li&gt;

&lt;a href="https://github.com/rodrigojavornik/PHPCleanup#escape" rel="noopener noreferrer"&gt;escape&lt;/a&gt;: Applies htmlspecialchars to value;&lt;/li&gt;

&lt;li&gt;

&lt;a href="https://github.com/rodrigojavornik/PHPCleanup#formatnumber" rel="noopener noreferrer"&gt;formatNumber&lt;/a&gt;: Format a number with grouped thousands;&lt;/li&gt;

&lt;li&gt;

&lt;a href="https://github.com/rodrigojavornik/PHPCleanup#input" rel="noopener noreferrer"&gt;input&lt;/a&gt;: Strip one whitespace from the beginning and end of a string and remove any HTML and PHP tags;&lt;/li&gt;

&lt;li&gt;

&lt;a href="https://github.com/rodrigojavornik/PHPCleanup#keys" rel="noopener noreferrer"&gt;keys&lt;/a&gt;:  applies sanitaze to elements of an array;&lt;/li&gt;

&lt;li&gt;…&lt;/li&gt;

&lt;/ul&gt;
&lt;/div&gt;
&lt;br&gt;
  &lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/rodrigojavornik/PHPCleanup" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;





&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/rodrigojavornik" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftwve1hh3j8ewl5aowo7r.png" alt="Did you like this text? You can buy me a coffee."&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>php</category>
      <category>xss</category>
      <category>security</category>
      <category>programming</category>
    </item>
    <item>
      <title>Handling input data in PHP</title>
      <dc:creator>Rodrigo Javornik</dc:creator>
      <pubDate>Thu, 04 May 2023 19:08:21 +0000</pubDate>
      <link>https://forem.com/rodrigojavornik/handling-input-data-in-php-5h3a</link>
      <guid>https://forem.com/rodrigojavornik/handling-input-data-in-php-5h3a</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2oKP0ITr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q3il6hszfcf51s5q0nel.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2oKP0ITr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q3il6hszfcf51s5q0nel.jpg" alt="Coffee filter" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When I started my career as a developer, there was something I always heard from more experienced programmers, something that became a mantra that I always carried with me:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Never trust user data&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;How many times have we heard of systems that failed, either serious or not, because they didn't handle their data properly? Or of developers who wasted precious time creating specific libraries to handle GET and POST parameters?&lt;/p&gt;

&lt;p&gt;We cannot deny that we have to handle our data, and the purpose of this text is to present a simple and safe way to perform this task with PHP.&lt;/p&gt;

&lt;h2&gt;
  
  
  The filter extension
&lt;/h2&gt;

&lt;p&gt;In PHP 5.2, the filter extension was added by default. Since then, it has become easier to validate and sanitize data without needing to access the superglobals $_POST and $_GET.&lt;/p&gt;

&lt;p&gt;In a simple way, there are two types of "tasks" performed by the filtering system:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Validation: ensures that the data meets a specific expectation. It returns a Boolean value if the data does not meet the established criterion.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sanitization: removes unwanted data from the input based on a criterion and returns the sanitized data.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As it is a native feature of PHP &amp;gt;5.2, no installation is required to access the feature.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using the Filter
&lt;/h2&gt;

&lt;p&gt;To use the resources of the filter, we have to use one of the filter functions such as &lt;strong&gt;filter_input&lt;/strong&gt; or &lt;strong&gt;filter_var&lt;/strong&gt;. For didactic purposes, the examples in this text will use the &lt;strong&gt;filter_input&lt;/strong&gt; function. Once the default function is defined, let's analyze its prototype:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="n"&gt;mixed&lt;/span&gt; &lt;span class="nb"&gt;filter_input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$type&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$variable_name&lt;/span&gt; &lt;span class="p"&gt;[,&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$filter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;FILTER_DEFAULT&lt;/span&gt; &lt;span class="p"&gt;[,&lt;/span&gt; &lt;span class="n"&gt;mixed&lt;/span&gt; &lt;span class="nv"&gt;$options&lt;/span&gt; &lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that the only required parameters are &lt;strong&gt;type&lt;/strong&gt; and &lt;strong&gt;variable_name&lt;/strong&gt;. Where &lt;strong&gt;type&lt;/strong&gt; is the constant used to indicate where the external data will be searched and &lt;strong&gt;variable_name&lt;/strong&gt; is the name of the parameter to be searched. For example: &lt;code&gt;$_GET['email']&lt;/code&gt; would be like &lt;code&gt;filter_input(INPUT_GET, 'email')&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Taking into consideration that each filter is represented by a different constant, the &lt;strong&gt;filter&lt;/strong&gt; parameter indicates the filter constant we will use. If no filter is defined, no filter will be applied by default. The &lt;strong&gt;options&lt;/strong&gt; parameter adds modifiers to the filters, and its usage will be explained later.&lt;/p&gt;

&lt;p&gt;Therefore, to apply a filter to a variable, we need the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nb"&gt;filter_input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;INPUT_CONSTANT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$input_data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;FILTER_CONSTANT&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each filter in the system is represented by a constant. &lt;a href="http://php.net/manual/en/filter.filters.validate.php"&gt;Validation constants&lt;/a&gt; are found as FILTER_VALIDATE_*, and &lt;a href="https://www.php.net/manual/en/filter.filters.sanitize.php"&gt;sanitization constants&lt;/a&gt; are found as FILTER_SANITIZE_*.&lt;/p&gt;

&lt;p&gt;Now that we understand the basic concepts, let's see some practical examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;//SANITIZATION&lt;/span&gt;

&lt;span class="c1"&gt;// emailUser = ((teste@teste.com)&amp;amp;*&lt;/span&gt;
&lt;span class="c1"&gt;// return teste@teste.com&lt;/span&gt;
&lt;span class="nv"&gt;$emailUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;filter_input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;INPUT_GET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'emailUser'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;FILTER_SANITIZE_EMAIL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// age = abc1b3&lt;/span&gt;
&lt;span class="c1"&gt;// return 13&lt;/span&gt;
&lt;span class="nv"&gt;$age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;filter_input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;INPUT_GET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'age'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;FILTER_SANITIZE_NUMBER_INT&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//VALIDATION&lt;/span&gt;

&lt;span class="c1"&gt;// The parameter "email" does not exist.&lt;/span&gt;
&lt;span class="c1"&gt;// return NULL&lt;/span&gt;
&lt;span class="nb"&gt;filter_input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;INPUT_GET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'email'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;FILTER_VALIDATE_EMAIL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// email = ((teste@teste.com)&amp;amp;*&lt;/span&gt;
&lt;span class="c1"&gt;// return FALSE&lt;/span&gt;
&lt;span class="nb"&gt;filter_input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;INPUT_GET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'email'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;FILTER_VALIDATE_EMAIL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// email = teste@teste.com&lt;/span&gt;
&lt;span class="c1"&gt;// return TRUE&lt;/span&gt;
&lt;span class="nb"&gt;filter_input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;INPUT_GET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'email'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;FILTER_VALIDATE_EMAIL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Modifiers
&lt;/h2&gt;

&lt;p&gt;There are ways to modify the behavior of filters. Validation filters accept options and flags as modifiers, while sanitization filters only accept options.&lt;/p&gt;

&lt;p&gt;Let's see an example of validation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;//numberHex = 0xf0&lt;/span&gt;
&lt;span class="nv"&gt;$modificador&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s1"&gt;'options'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'default'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'min_range'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
        &lt;span class="s1"&gt;'max_range'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;240&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="s1"&gt;'flags'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;FILTER_FLAG_ALLOW_HEX&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nb"&gt;filter_input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;INPUT_GET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'numberHex'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;FILTER_VALIDATE_INT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$modificador&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, to validate the numberHex we need an integer value between 1 and 240, if these requirements are not met the function will return what is in default. The flag &lt;strong&gt;FILTER_FLAG_ALLOW_HEX&lt;/strong&gt; allows the function to also work with hexadecimal values, in this case 0xf0 is equal to 240 in decimal.&lt;/p&gt;

&lt;p&gt;Now let's see an example of sanitization:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// number = -2.3&lt;/span&gt;
&lt;span class="c1"&gt;// return -23&lt;/span&gt;
&lt;span class="nb"&gt;filter_input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;INPUT_GET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'number'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;FILTER_SANITIZE_NUMBER_FLOAT&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// number = -2.3&lt;/span&gt;
&lt;span class="c1"&gt;// return -2.3&lt;/span&gt;
&lt;span class="nb"&gt;filter_input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;INPUT_GET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'number'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;FILTER_SANITIZE_NUMBER_FLOAT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'flags'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;FILTER_FLAG_ALLOW_FRACTION&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, the same value is processed in different ways. In the first treatment, the dot is not considered. This happens because the function will only work with fractions if the &lt;strong&gt;FILTER_FLAG_ALLOW_FRACTION&lt;/strong&gt; flag is present.&lt;/p&gt;

&lt;h2&gt;
  
  
  To conclude
&lt;/h2&gt;

&lt;p&gt;The filter extension is an excellent way to handle your data within PHP without resorting to superglobals or using specific libraries. It is worth delving deeper into its functioning.&lt;/p&gt;

&lt;p&gt;You can check the complete documentation of the feature by &lt;a href="https://www.php.net/manual/en/book.filter.php"&gt;clicking here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you want a more powerful tool for data sanitization, I suggest using my &lt;a href="https://github.com/rodrigojavornik/PHPCleanup"&gt;PHPCleanup library&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Hey, did you like the text? Do you have any tips to share? Leave your comment, it will be a pleasure to interact with you.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/rodrigojavornik"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CQvhqaK6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.buymeacoffee.com/buttons/default-black.png" alt="Did you like this text? You can buy me a coffee." width="434" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>php</category>
      <category>sanitize</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
