Home >Backend Development >PHP Tutorial >Analyze the PHP functions that control user login and determine user login in WordPress_php skills

Analyze the PHP functions that control user login and determine user login in WordPress_php skills

WBOY
WBOYOriginal
2016-05-16 19:57:591415browse

Login function: wp_signon()

Function introduction:
The wp_signon() function is used to authorize users to log in to WordPress and remember the user name. This function replaces wp_login. Enabled from WordPress version 2.5.

Function usage:

<&#63;php wp_signon( $credentials, $secure_cookie ) &#63;> 

Parameter description:

  • $credentials
  • (array) (optional) Login user information.
  • Default: None
  • $secure_cookie
  • (boolean) (optional) Determines whether to use secure cookies.
  • Default: None

Note: If you do not provide $credentials, wp_signon uses the $_POST parameter (the key values ​​are "log", "pwd" and "rememberme").

Function return value:
(object)
The object WP_Error is returned if the login fails, and WP_User

is returned if the login is successful.

Function example:

$creds = array();
$creds['user_login'] = 'example'; //wordperss后台用户名称
$creds['user_password'] = '123456'; //wordperss后台用户密码
$creds['remember'] = true;
$user = wp_signon( $creds, false );
if ( is_wp_error($user) )
  echo $user->get_error_message();

Source file:
wp_signon() is located in wp-includes/user.php.

Function to determine whether the user is logged in: is_user_logged_in()

Function introduction:
The is_user_logged_in() function determines whether the user is logged in. If the user is logged in, it returns True otherwise it returns False.

Function usage:

<&#63;php if ( is_user_logged_in() ) { ... } &#63;> 

Function parameters:
This function does not accept any parameters.

Return value:
(boolean)
Returns True if logged in, otherwise returns False.

Function example:
The following example shows the content displayed by logged in users or non-logged in users:

<&#63;php
if ( is_user_logged_in() ) {
  echo 'Welcome, registered user!';
} else {
  echo 'Welcome, visitor!';
}
&#63;>

Source file:
is_user_logged_in() is located in the file wp-includes/pluggable.php.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn