Title: Cookie::path_matches
Published: March 29, 2023
Last modified: November 8, 2023

---

# Cookie::path_matches( string $request_path ): boolean

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/wporg-requests-cookie/path_matches/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/classes/wporg-requests-cookie/path_matches/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wporg-requests-cookie/path_matches/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wporg-requests-cookie/path_matches/?output_format=md#source)

Check if a cookie is valid for a given path

## 󠀁[Description](https://developer.wordpress.org/reference/classes/wporg-requests-cookie/path_matches/?output_format=md#description)󠁿

From the path-match check in RFC 6265 section 5.1.4

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wporg-requests-cookie/path_matches/?output_format=md#parameters)󠁿

 `$request_path`stringrequired

Path to check

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wporg-requests-cookie/path_matches/?output_format=md#return)󠁿

 boolean Whether the cookie is valid for the given path

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wporg-requests-cookie/path_matches/?output_format=md#source)󠁿

    ```php
    public function path_matches($request_path) {
    	if (empty($request_path)) {
    		// Normalize empty path to root
    		$request_path = '/';
    	}

    	if (!isset($this->attributes['path'])) {
    		// Cookies created manually; cookies created by Requests will set
    		// the path to the requested path
    		return true;
    	}

    	if (is_scalar($request_path) === false) {
    		return false;
    	}

    	$cookie_path = $this->attributes['path'];

    	if ($cookie_path === $request_path) {
    		// The cookie-path and the request-path are identical.
    		return true;
    	}

    	if (strlen($request_path) > strlen($cookie_path) && substr($request_path, 0, strlen($cookie_path)) === $cookie_path) {
    		if (substr($cookie_path, -1) === '/') {
    			// The cookie-path is a prefix of the request-path, and the last
    			// character of the cookie-path is %x2F ("/").
    			return true;
    		}

    		if (substr($request_path, strlen($cookie_path), 1) === '/') {
    			// The cookie-path is a prefix of the request-path, and the
    			// first character of the request-path that is not included in
    			// the cookie-path is a %x2F ("/") character.
    			return true;
    		}
    	}

    	return false;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/requests/src/cookie.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/Requests/src/Cookie.php#L234)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/Requests/src/Cookie.php#L234-L273)

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwporg-requests-cookie%2Fpath_matches%2F)
before being able to contribute a note or feedback.