wp_generate_auth_cookie

Generate authentication cookie contents.

Description

(string) wp_generate_auth_cookie( (int) $user_id, (int) $expiration, (string) $scheme = 'auth', (string) $token = '' ); 

Returns (string)

Authentication cookie contents. Empty string if user does not exist.

Parameters (4)

0. $user_id (int)
The user id.
1. $expiration (int)
The time the cookie expires as a UNIX timestamp.
2. $scheme — Optional. (string) => 'auth'
The cookie scheme to use: auth, secure_auth, or logged_in
3. $token — Optional. (string) => ''
User's session token to use for this cookie

Usage

  1. if ( !function_exists( 'wp_generate_auth_cookie' ) ) { 
  2. require_once ABSPATH . WPINC . '/pluggable.php'; 
  3.  
  4. // The user id. 
  5. $user_id = -1; 
  6.  
  7. // The time the cookie expires as a UNIX timestamp. 
  8. $expiration = -1; 
  9.  
  10. // Optional. The cookie scheme to use: auth, secure_auth, or logged_in 
  11. $scheme = 'auth'; 
  12.  
  13. // User's session token to use for this cookie 
  14. $token = ''; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = wp_generate_auth_cookie($user_id, $expiration, $scheme, $token); 
  18.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/pluggable.php  
  1. function wp_generate_auth_cookie( $user_id, $expiration, $scheme = 'auth', $token = '' ) { 
  2. $user = get_userdata($user_id); 
  3. if ( ! $user ) { 
  4. return ''; 
  5.  
  6. if ( ! $token ) { 
  7. $manager = WP_Session_Tokens::get_instance( $user_id ); 
  8. $token = $manager->create( $expiration ); 
  9.  
  10. $pass_frag = substr($user->user_pass, 8, 4); 
  11.  
  12. $key = wp_hash( $user->user_login . '|' . $pass_frag . '|' . $expiration . '|' . $token, $scheme ); 
  13.  
  14. // If ext/hash is not present, compat.php's hash_hmac() does not support sha256. 
  15. $algo = function_exists( 'hash' ) ? 'sha256' : 'sha1'; 
  16. $hash = hash_hmac( $algo, $user->user_login . '|' . $expiration . '|' . $token, $key ); 
  17.  
  18. $cookie = $user->user_login . '|' . $expiration . '|' . $token . '|' . $hash; 
  19.  
  20. /** 
  21. * Filters the authentication cookie. 
  22. * @since 2.5.0 
  23. * @param string $cookie Authentication cookie. 
  24. * @param int $user_id User ID. 
  25. * @param int $expiration The time the cookie expires as a UNIX timestamp. 
  26. * @param string $scheme Cookie scheme used. Accepts 'auth', 'secure_auth', or 'logged_in'. 
  27. * @param string $token User's session token used. 
  28. */ 
  29. return apply_filters( 'auth_cookie', $cookie, $user_id, $expiration, $scheme, $token );