⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 php_filter.asp

📁 W3Schools tutorial..web designing
💻 ASP
📖 第 1 页 / 共 2 页
字号:
<h2>What is a PHP Filter? </h2>
<p>A PHP filter is used to validate and filter data coming from insecure sources.</p>
<p>To test, validate and filter user input or custom data is an important part 
of any web application.</p>
<p>The PHP filter extension is designed to make data 
filtering easier and quicker.</p>
<hr />

<h2>Why use a Filter?</h2>
<p>Almost all web applications depend on external input. Usually this comes from 
a user or another application (like a web service). By using filters you can be 
sure your application gets the correct input type.</p>
<p><b>You should always filter all external data!</b></p>
<p>Input filtering is one of the most important application security issues.</p>
<p>What is external data?</p>
<ul>
	<li>Input data from a form</li>
	<li>Cookies</li>
	<li>Web services data</li>
	<li>Server variables</li>
	<li>Database query results</li>
</ul>
<hr />

<h2>Functions and Filters</h2>
<p>To filter a variable, use one of the following filter functions:</p>
<ul>
	<li>filter_var() - Filters a single variable with a specified filter</li>
	<li>filter_var_array() - Filter several variables with the same or different 
	filters</li>
	<li>filter_input - Get one input variable and filter it</li>
	<li>filter_input_array - Get several input variables and filter them with 
	the same or different filters</li>
</ul>
<p>In the example below, we validate an integer using the filter_var() function:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table13"><tr><td>
<pre>&lt;?php
$int = 123;</pre>
<pre>if(!filter_var($int, FILTER_VALIDATE_INT))
 {
 echo(&quot;Integer is not valid&quot;);
 }
else
 {
 echo(&quot;Integer is valid&quot;);
 }
?&gt;</pre>
</td></tr></table>

<p>The code above uses the &quot;FILTER_VALIDATE_INT&quot;&nbsp; filter to filter the 
variable. Since the integer is valid, the output of the code above will be: 
&quot;Integer is valid&quot;.</p>
<p>If we try with a variable that is not an integer (like &quot;123abc&quot;), the output 
will be: &quot;Integer is not valid&quot;.</p>
<p>For a complete list of functions and filters, visit our
<a href="php_ref_filter.asp">PHP Filter Reference.</a><br />
</p>
<hr />

<h2>Validating and Sanitizing</h2>
<p>There are two kinds of filters:</p>
<p>Validating filters:</p>
<ul>
	<li>Are used to validate user input</li>
	<li>Strict format rules (like URL or E-Mail validating)</li>
	<li>Returns the expected type on success or FALSE on failure</li>
</ul>

<p>Sanitizing filters:</p>
<ul>
	<li>Are used to allow or disallow specified characters in a string</li>
	<li>No data format rules</li>
	<li>Always return the string</li>
</ul>
<hr />

<h2>Options and Flags</h2>
<p>Options and flags are used to add additional filtering options to the 
specified filters.</p>
<p>Different filters have different options and flags. </p>
<p>In the example below, we validate an integer using the filter_var() and the &quot;min_range&quot; 
and &quot;max_range&quot; options:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table14"><tr><td>
<pre>&lt;?php
$var=300;</pre>
<pre>$int_options = array(
&quot;options&quot;=&gt;array
 (
 &quot;min_range&quot;=&gt;0,
 &quot;max_range&quot;=&gt;256
 )
);</pre>
<pre>if(!filter_var($var, FILTER_VALIDATE_INT, $int_options))
 {
 echo(&quot;Integer is not valid&quot;);
 }
else
 {
 echo(&quot;Integer is valid&quot;);
 }
?&gt;</pre>
</td></tr></table>

<p>Like the code above, options must be put in an associative array with the 
name &quot;options&quot;. If a flag is used it does not need to be in an array.</p>
<p>Since the integer is &quot;300&quot; it is not in the specified range, and the output of the code above will be: 
&quot;Integer is not valid&quot;.</p>
<p>For a complete list of functions and filters, visit our
<a href="php_ref_filter.asp">PHP Filter Reference.</a> Check each filter to see 
what options and flags are available.</p>
<hr />

<h2>Validate Input</h2>
<p>Let's try validating input from a form.</p>
<p>The first thing we need to do is to confirm that the input data we are 
looking for exists.</p>
<p>Then we filter the input data using the filter_input() function.</p>
<p>In the example below, the input variable &quot;email&quot; is sent to the PHP page:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table3"><tr><td>
<pre>&lt;?php
if(!filter_has_var(INPUT_GET, &quot;email&quot;))
 {
 echo(&quot;Input type does not exist&quot;);
 }
else
 {
 if (!filter_input(INPUT_GET, &quot;email&quot;, FILTER_VALIDATE_EMAIL))
  {
  echo &quot;E-Mail is not valid&quot;;
  }
 else
  {
  echo &quot;E-Mail is valid&quot;;
  }
 }
?&gt;</pre>
</td></tr></table>

<h2>Example Explained</h2>
<p>The example above has an input (email) sent to it using the &quot;GET&quot; method:</p>
<ol>
	<li>Check if an &quot;email&quot; input variable of the &quot;GET&quot; type exist</li>
	<li>If the input variable exists, check if it is a valid e-mail address</li>
</ol>
<hr />

<h2>Sanitize Input</h2>
<p>Let's try cleaning up an URL sent from a form.</p>
<p>First we confirm that the input data we are 
looking for exists.</p>
<p>Then we sanitize the input data using the filter_input() function.</p>
<p>In the example below, the input variable &quot;url&quot; is sent to the PHP page:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table5"><tr><td>
<pre>&lt;?php
if(!filter_has_var(INPUT_POST, &quot;url&quot;))
 {
 echo(&quot;Input type does not exist&quot;);
 }
else
 {
 $url = filter_input(INPUT_POST, 
 &quot;url&quot;, FILTER_SANITIZE_URL);
 }
?&gt;</pre>
</td></tr></table>
<h2>Example Explained</h2>
<p>The example above has an input (url) sent to it using the &quot;POST&quot; method:</p>
<ol>
	<li>Check if the &quot;url&quot; input of the &quot;POST&quot; type exists</li>
	<li>If the input variable exists, sanitize (take away invalid characters) 
	and store it in the $url variable</li>
</ol>
<p>If the input variable is a string like this 
&quot;http://www.W3邋Sch

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -