FileCookieJar
extends CookieJar
in package
Persists non-session cookies using a JSON formatted file
Table of Contents
Properties
- $cookies : array<string|int, SetCookie>
- $filename : string
- $storeSessionCookies : bool
- $strictMode : bool
Methods
- __construct() : mixed
- Create a new FileCookieJar object
- __destruct() : mixed
- Saves the file when shutting down
- clear() : void
- Remove cookies currently held in the cookie jar.
- clearSessionCookies() : void
- Discard all sessions cookies.
- count() : int
- extractCookies() : void
- Extract cookies from an HTTP response and store them in the CookieJar.
- fromArray() : self
- Create a new Cookie jar from an associative array and domain.
- getCookieByName() : SetCookie|null
- Finds and returns the cookie based on the name
- getIterator() : ArrayIterator<int, SetCookie>
- load() : void
- Load cookies from a JSON formatted file.
- save() : void
- Saves the cookies to a file.
- setCookie() : bool
- Sets a cookie in the cookie jar.
- shouldPersist() : bool
- Evaluate if this cookie should be persisted to storage that survives between requests.
- toArray() : array<string|int, mixed>
- Converts the cookie jar to an array.
- withCookieHeader() : RequestInterface
- Create a request with added cookie headers.
- getCookiePathFromRequest() : string
- Computes cookie path following RFC 6265 section 5.1.4
- removeCookieIfEmpty() : void
- If a cookie already exists and the server asks to set it again with a null value, the cookie must be deleted.
Properties
$cookies
private
array<string|int, SetCookie>
$cookies
= []
Loaded cookie data
$filename
private
string
$filename
filename
$storeSessionCookies
private
bool
$storeSessionCookies
Control whether to persist session cookies or not.
$strictMode
private
bool
$strictMode
Methods
__construct()
Create a new FileCookieJar object
public
__construct(string $cookieFile[, bool $storeSessionCookies = false ]) : mixed
Parameters
- $cookieFile : string
-
File to store the cookie data
- $storeSessionCookies : bool = false
-
Set to true to store session cookies in the cookie jar.
Tags
__destruct()
Saves the file when shutting down
public
__destruct() : mixed
clear()
Remove cookies currently held in the cookie jar.
public
clear([string|null $domain = null ][, string|null $path = null ][, string|null $name = null ]) : void
Invoking this method without arguments will empty the whole cookie jar. If given a $domain argument only cookies belonging to that domain will be removed. If given a $domain and $path argument, cookies belonging to the specified path within that domain are removed. If given all three arguments, then the cookie with the specified name, path and domain is removed.
Parameters
- $domain : string|null = null
-
Clears cookies matching a domain
- $path : string|null = null
-
Clears cookies matching a domain and path
- $name : string|null = null
-
Clears cookies matching a domain, path, and name
clearSessionCookies()
Discard all sessions cookies.
public
clearSessionCookies() : void
Removes cookies that don't have an expire field or a have a discard field set to true. To be called when the user agent shuts down according to RFC 2965.
count()
public
count() : int
Return values
intextractCookies()
Extract cookies from an HTTP response and store them in the CookieJar.
public
extractCookies(RequestInterface $request, ResponseInterface $response) : void
Parameters
- $request : RequestInterface
-
Request that was sent
- $response : ResponseInterface
-
Response that was received
fromArray()
Create a new Cookie jar from an associative array and domain.
public
static fromArray(array<string|int, mixed> $cookies, string $domain) : self
Parameters
- $cookies : array<string|int, mixed>
-
Cookies to create the jar from
- $domain : string
-
Domain to set the cookies to
Return values
selfgetCookieByName()
Finds and returns the cookie based on the name
public
getCookieByName(string $name) : SetCookie|null
Parameters
- $name : string
-
cookie name to search for
Return values
SetCookie|null —cookie that was found or null if not found
getIterator()
public
getIterator() : ArrayIterator<int, SetCookie>
Return values
ArrayIterator<int, SetCookie>load()
Load cookies from a JSON formatted file.
public
load(string $filename) : void
Old cookies are kept unless overwritten by newly loaded ones.
Parameters
- $filename : string
-
Cookie file to load.
Tags
save()
Saves the cookies to a file.
public
save(string $filename) : void
Parameters
- $filename : string
-
File to save
Tags
setCookie()
Sets a cookie in the cookie jar.
public
setCookie(SetCookie $cookie) : bool
Parameters
- $cookie : SetCookie
-
Cookie to set.
Return values
bool —Returns true on success or false on failure
shouldPersist()
Evaluate if this cookie should be persisted to storage that survives between requests.
public
static shouldPersist(SetCookie $cookie[, bool $allowSessionCookies = false ]) : bool
Parameters
- $cookie : SetCookie
-
Being evaluated.
- $allowSessionCookies : bool = false
-
If we should persist session cookies
Return values
booltoArray()
Converts the cookie jar to an array.
public
toArray() : array<string|int, mixed>
Return values
array<string|int, mixed>withCookieHeader()
Create a request with added cookie headers.
public
withCookieHeader(RequestInterface $request) : RequestInterface
If no matching cookies are found in the cookie jar, then no Cookie header is added to the request and the same request is returned.
Parameters
- $request : RequestInterface
-
Request object to modify.
Return values
RequestInterface —returns the modified request.
getCookiePathFromRequest()
Computes cookie path following RFC 6265 section 5.1.4
private
getCookiePathFromRequest(RequestInterface $request) : string
Parameters
- $request : RequestInterface
Tags
Return values
stringremoveCookieIfEmpty()
If a cookie already exists and the server asks to set it again with a null value, the cookie must be deleted.
private
removeCookieIfEmpty(SetCookie $cookie) : void
Parameters
- $cookie : SetCookie