WP_Session_Tokens::create() WordPress Method

The WP_Session_Tokens::create() method is used to create a new session token for the current user. This is typically used when a user logs in or registers for a new account.

WP_Session_Tokens::create( int $expiration ) #

Generates a session token and attaches session information to it.


Description

A session token is a long, random string. It is used in a cookie to link that cookie to an expiration time and to ensure the cookie becomes invalidated when the user logs out.

This function generates a token and stores it with the associated expiration time (and potentially other session information via the ‘attach_session_information’ filter).


Top ↑

Parameters

$expiration

(int)(Required)Session expiration timestamp.


Top ↑

Return

(string) Session token.


Top ↑

Source

File: wp-includes/class-wp-session-tokens.php

	final public function create( $expiration ) {
		/**
		 * Filters the information attached to the newly created session.
		 *
		 * Can be used to attach further information to a session.
		 *
		 * @since 4.0.0
		 *
		 * @param array $session Array of extra data.
		 * @param int   $user_id User ID.
		 */
		$session               = apply_filters( 'attach_session_information', array(), $this->user_id );
		$session['expiration'] = $expiration;

		// IP address.
		if ( ! empty( $_SERVER['REMOTE_ADDR'] ) ) {
			$session['ip'] = $_SERVER['REMOTE_ADDR'];
		}

		// User-agent.
		if ( ! empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
			$session['ua'] = wp_unslash( $_SERVER['HTTP_USER_AGENT'] );
		}

		// Timestamp.
		$session['login'] = time();

		$token = wp_generate_password( 43, false, false );

		$this->update( $token, $session );

		return $token;
	}


Top ↑

Changelog

Changelog
VersionDescription
4.0.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.