解析WordPress中控制用户登陆和判断用户登陆的PHP函数
<p><strong>登陆函数:wp_signon()</strong></p>
<p>
函数介绍:<br>
wp_signon()函数用于授权给用户登陆wordpress并可记住该用户名称。该函数取代了wp_login。WordPress 2.5版本起启用。</p>
<p>
函数使用:</p>
<div>
<div>
<div id="highlighter_890421">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code><?php wp_signon( </code><code>$credentials</code><code>, </code><code>$secure_cookie</code> <code>) ?> </code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
参数说明:</p>
<ul>
<li>
$credentials</li>
<li>
(array) (可选) 登陆用户信息.</li>
<li>
Default: None</li>
<li>
$secure_cookie</li>
<li>
(boolean) (可选) 决定是否使用安全cookie。</li>
<li>
Default: None</li>
</ul>
<p>
注意:如果你没有提供 $credentials, wp_signon 使用 $_POST 参数(键值为 “log”, “pwd” 和 “rememberme”).</p>
<p>
函数返回值:<br>
(object)<br>
登陆失败返回对象WP_Error,登陆成功则返回WP_User</p>
<p>
函数实例:</p>
<div>
<div>
<div id="highlighter_542254">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
<div>
2</div>
<div>
3</div>
<div>
4</div>
<div>
5</div>
<div>
6</div>
<div>
7</div>
</td>
<td>
<div>
<div>
<code>$creds</code> <code>= </code><code>array</code><code>();</code>
</div>
<div>
<code>$creds</code><code>[</code><code>'user_login'</code><code>] = </code><code>'example'</code><code>; </code><code>//wordperss后台用户名称</code>
</div>
<div>
<code>$creds</code><code>[</code><code>'user_password'</code><code>] = </code><code>'123456'</code><code>; </code><code>//wordperss后台用户密码</code>
</div>
<div>
<code>$creds</code><code>[</code><code>'remember'</code><code>] = true;</code>
</div>
<div>
<code>$user</code> <code>= wp_signon( </code><code>$creds</code><code>, false );</code>
</div>
<div>
<code>if</code> <code>( is_wp_error(</code><code>$user</code><code>) )</code>
</div>
<div>
<code> </code><code>echo</code> <code>$user</code><code>->get_error_message();</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
源文件:<br>
wp_signon() 位于 wp-includes/user.php.</p>
<p>
</p>
<p>
<strong>判断用户是否登陆函数:is_user_logged_in()</strong></p>
<p>
函数介绍:<br>
is_user_logged_in()函数由于判断用户是否登陆,如果用户已经登陆返回True否则返回False。</p>
<p>
函数使用:</p>
<div>
<div>
<div id="highlighter_624200">
<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>if</code> <code>( is_user_logged_in() ) { ... } ?> </code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
函数参数:<br>
该函数不接受任何参数。</p>
<p>
返回值:<br>
(boolean)<br>
已登陆返回True,否则返回False。</p>
<p>
函数实例:<br>
以下实例显示已登陆用户或者未登陆用户显示的内容:</p>
<div>
<div>
<div id="highlighter_849523">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
<div>
2</div>
<div>
3</div>
<div>
4</div>
<div>
5</div>
<div>
6</div>
<div>
7</div>
</td>
<td>
<div>
<div>
<code><?php</code>
</div>
<div>
<code>if</code> <code>( is_user_logged_in() ) {</code>
</div>
<div>
<code> </code><code>echo</code> <code>'Welcome, registered user!'</code><code>;</code>
</div>
<div>
<code>} </code><code>else</code> <code>{</code>
</div>
<div>
<code> </code><code>echo</code> <code>'Welcome, visitor!'</code><code>;</code>
</div>
<div>
<code>}</code>
</div>
<div>
<code>?></code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
源文件:<br>
is_user_logged_in() 位于文件 wp-includes/pluggable.php.</p>
頁:
[1]