$cookies
$cookies : array<mixed,\GuzzleHttp\Cookie\SetCookie>
Persists cookies in the client session
$cookies : array<mixed,\GuzzleHttp\Cookie\SetCookie>
shouldPersist(\GuzzleHttp\Cookie\SetCookie $cookie, boolean $allowSessionCookies = false) : boolean
Evaluate if this cookie should be persisted to storage that survives between requests.
\GuzzleHttp\Cookie\SetCookie | $cookie | Being evaluated. |
boolean | $allowSessionCookies | If we should persist session cookies |
None found |
getCookieByName(string $name) : \GuzzleHttp\Cookie\SetCookie|null
Finds and returns the cookie based on the name
string | $name | cookie name to search for |
cookie that was found or null if not found
None found |
None found |
clear(string $domain = null, string $path = null, string $name = null) : \GuzzleHttp\Cookie\CookieJarInterface
Remove cookies currently held in the cookie jar.
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.
string | $domain | Clears cookies matching a domain |
string | $path | Clears cookies matching a domain and path |
string | $name | Clears cookies matching a domain, path, and name |
None found |
clearSessionCookies()
Discard all sessions cookies.
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.
None found |
setCookie(\GuzzleHttp\Cookie\SetCookie $cookie) : boolean
Sets a cookie in the cookie jar.
\GuzzleHttp\Cookie\SetCookie | $cookie | Cookie to set. |
Returns true on success or false on failure
None found |
None found |
None found |
extractCookies(\Psr\Http\Message\RequestInterface $request, \Psr\Http\Message\ResponseInterface $response)
Extract cookies from an HTTP response and store them in the CookieJar.
\Psr\Http\Message\RequestInterface | $request | Request that was sent |
\Psr\Http\Message\ResponseInterface | $response | Response that was received |
None found |
withCookieHeader(\Psr\Http\Message\RequestInterface $request) : \Psr\Http\Message\RequestInterface
Create a request with added cookie headers.
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.
\Psr\Http\Message\RequestInterface | $request | Request object to modify. |
returns the modified request.
None found |
None found |
None found |
None found |
getCookiePathFromRequest(\Psr\Http\Message\RequestInterface $request) : string
Computes cookie path following RFC 6265 section 5.1.4
\Psr\Http\Message\RequestInterface | $request |
None found |
removeCookieIfEmpty(\GuzzleHttp\Cookie\SetCookie $cookie)
If a cookie already exists and the server asks to set it again with a null value, the cookie must be deleted.
\GuzzleHttp\Cookie\SetCookie | $cookie |
None found |
<?php
namespace GuzzleHttp\Cookie;
/**
* Persists cookies in the client session
*/
class SessionCookieJar extends CookieJar
{
/** @var string session key */
private $sessionKey;
/** @var bool Control whether to persist session cookies or not. */
private $storeSessionCookies;
/**
* Create a new SessionCookieJar object
*
* @param string $sessionKey Session key name to store the cookie
* data in session
* @param bool $storeSessionCookies Set to true to store session cookies
* in the cookie jar.
*/
public function __construct($sessionKey, $storeSessionCookies = false)
{
$this->sessionKey = $sessionKey;
$this->storeSessionCookies = $storeSessionCookies;
$this->load();
}
/**
* Saves cookies to session when shutting down
*/
public function __destruct()
{
$this->save();
}
/**
* Save cookies to the client session
*/
public function save()
{
$json = [];
foreach ($this as $cookie) {
/** @var SetCookie $cookie */
if (CookieJar::shouldPersist($cookie, $this->storeSessionCookies)) {
$json[] = $cookie->toArray();
}
}
$_SESSION[$this->sessionKey] = json_encode($json);
}
/**
* Load the contents of the client session into the data array
*/
protected function load()
{
if (!isset($_SESSION[$this->sessionKey])) {
return;
}
$data = json_decode($_SESSION[$this->sessionKey], true);
if (is_array($data)) {
foreach ($data as $cookie) {
$this->setCookie(new SetCookie($cookie));
}
} elseif (strlen($data)) {
throw new \RuntimeException("Invalid cookie data");
}
}
}