GC_Http_Cookie::__construct()
最后更新于:2021-12-01 11:13:11
GC_( string|array$data, string$requested_url=”)Sets up this cookie object.
参数
- $data
-
(string|array) (Required) Raw cookie data as header string or data array.
-
‘name’
(string) Cookie name. -
‘value’
(mixed) Value. Should NOT already be urlencoded. -
‘expires’
(string|int|null) Optional. Unix timestamp or formatted date. Default null. -
‘path’
(string) Optional. Path. Default ‘/’. -
‘domain’
(string) Optional. Domain. Default host of parsed $requested_url. -
‘port’
(int) Optional. Port. Default null. -
‘host_only’
(bool) Optional. host-only storage flag. Default true.
-
‘name’
- $requested_url
-
(string) (Optional) The URL which the cookie was set on, used for default $domain and $port values.
Default value: ”
源文件
文件: gc-includes/class-gc-http-cookie.php
public function __construct( $data, $requested_url = '' ) {
if ( $requested_url ) {
$arrURL = parse_url( $requested_url );
}
if ( isset( $arrURL['host'] ) ) {
$this->domain = $arrURL['host'];
}
$this->path = isset( $arrURL['path'] ) ? $arrURL['path'] : '/';
if ( '/' !== substr( $this->path, -1 ) ) {
$this->path = dirname( $this->path ) . '/';
}
if ( is_string( $data ) ) {
// Assume it's a header string direct from a previous request.
$pairs = explode( ';', $data );
// Special handling for first pair; name=value. Also be careful of "=" in value.
$name = trim( substr( $pairs[0], 0, strpos( $pairs[0], '=' ) ) );
$value = substr( $pairs[0], strpos( $pairs[0], '=' ) + 1 );
$this->name = $name;
$this->value = urldecode( $value );
// Removes name=value from items.
array_shift( $pairs );
// Set everything else as a property.
foreach ( $pairs as $pair ) {
$pair = rtrim( $pair );
// Handle the cookie ending in ; which results in a empty final pair.
if ( empty( $pair ) ) {
continue;
}
list( $key, $val ) = strpos( $pair, '=' ) ? explode( '=', $pair ) : array( $pair, '' );
$key = strtolower( trim( $key ) );
if ( 'expires' === $key ) {
$val = strtotime( $val );
}
$this->$key = $val;
}
} else {
if ( ! isset( $data['name'] ) ) {
return;
}
// Set properties based directly on parameters.
foreach ( array( 'name', 'value', 'path', 'domain', 'port', 'host_only' ) as $field ) {
if ( isset( $data[ $field ] ) ) {
$this->$field = $data[ $field ];
}
}
if ( isset( $data['expires'] ) ) {
$this->expires = is_int( $data['expires'] ) ? $data['expires'] : strtotime( $data['expires'] );
} else {
$this->expires = null;
}
}
}