详解WordPress中过滤链接与过滤SQL语句的方法
<p><strong>esc_url()(过滤链接)</strong><br>
很多 URL 会有一些小错误,用 esc_url() 函数可以屏蔽或者修正这些错误,并且可以拒绝不安全的协议。</p>
<p>
esc_url() 函数的工作内容:</p>
<p>
默认拒绝不是下面协议的 URL:defaulting to http、https、ftp、ftps、mailto、news、irc、gopher、nntp、feed 和 telnet<br>
删除无效字符和危险的字符<br>
将字符转换成 HTML 实体字符<br>
使用方法</p>
<div>
<div>
<div id="highlighter_993052">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>esc_url( </code><code>$url</code><code>, </code><code>$protocols</code><code>, </code><code>$_context</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
参数</p>
<p>
$url</p>
<p>
(字符串)(必须)要被过滤的 URL.</p>
<p>
默认值:None</p>
<p>
$protocols</p>
<p>
(数组)(可选)可以接收协议的数组,如果没有设置,则默认为:defaulting to http、https、ftp、ftps、mailto、news、irc、gopher、nntp、feed 和 telnet.</p>
<p>
默认值:None</p>
<p>
$_context</p>
<p>
(字符串)(可选)如何返回 URL.</p>
<p>
默认值:(字符串)display</p>
<p>
返回值</p>
<p>
(字符串)返回过滤后的链接。</p>
<p>
例子</p>
<div>
<div>
<div id="highlighter_11439">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code><?php </code><code>echo</code> <code>esc_url( </code><code>'www.endskin.com'</code> <code>);</code><code>//输出:http://www.endskin.com ?></code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
更多</p>
<p>
此函数位于:wp-includes/formatting.php</p>
<p>
<br><strong>esc_sql()(过滤 Sql 语句)</strong><br>
esc_sql() 用来过滤准备添加到 Sql 语句里边的字符串,防止 Sql 注入和 Sql 语句被数据干扰出现异常。</p>
<p>
用法</p>
<div>
<div>
<div id="highlighter_553502">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>esc_sql( </code><code>$data</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
参数</p>
<p>
$data</p>
<p>
(字符串)(必须)要过滤的字符串。</p>
<p>
默认值:None</p>
<p>
返回值</p>
<p>
(字符串)返回过滤后的字符串,可以直接添加到 Sql 语句里。</p>
<p>
例子</p>
<div>
<div>
<div id="highlighter_960986">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
<div>
2</div>
<div>
3</div>
</td>
<td>
<div>
<div>
<code>$name</code> <code>= esc_sql( </code><code>$name</code> <code>);</code>
</div>
<div>
<code>$status</code> <code>= esc_sql( </code><code>$status</code> <code>);</code>
</div>
<div>
<code>$wpdb</code><code>->get_var( </code><code>"SELECT something FROM table WHERE foo = '$name' and status = '$status'"</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
更多</p>
<p>
此函数位于:wp-includes/formatting.php</p>
頁:
[1]