Cookie{}

在本文中

Cookie存储对象

方法

姓名描述
Cookie::__construct创建新的cookie对象
Cookie::__toString获取cookie值
Cookie::域匹配检查cookie是否对给定域有效
Cookie::format_for_header为cookie标头格式化cookie
Cookie::format_for_set_Cookie设置Set-cookie头的cookie格式
Cookie::已过期检查cookie是否过期。
Cookie::正常化规范化cookie和属性
Cookie::normalize_attribute分析单个cookie属性
Cookie::解析将cookie字符串解析为cookie对象
Cookie::parse_from_headers从请求标头解析所有Set-Cookie标头
Cookie::path_matches检查cookie对于给定路径是否有效
Cookie::uri_matches检查cookie对于给定URI是否有效

来源

类Cookie{/***Cookie名称。**@var字符串*/公共$name;/***Cookie值。**@var字符串*/公共$value;/***Cookie属性**有效密钥为“path”、“domain”、“expires”、“max-age”、“secure”和*“httponly”。**@var\WpOrg\Requests\Utility\CaseInsensitiveDictionary|数组类数组对象*/公共$attributes=[];/***Cookie标志**有效密钥为“创建”、“最后访问”、“持久”和“主机专用”。**@var数组*/公共$flags=[];/***相对计算的参考时间**在计算Max-Age过期时间和*检查时间有效性。**@变量int*/public$reference_time=0;/***创建新的cookie对象**@param string$name cookie的名称。*@param string$value cookie的值。*@param array |\WpOrg\Requests\Utility\CaseInsensitiveDictionary$attributes属性数据关联数组*@param array$flags cookie的标志。*有效密钥为“创建”、“上次访问”、,*“持久”和“仅主机”。*@param int | null$reference_time相对计算的参考时间。**@throws\WpOrg\Requests\Exception\InvalidArgument当传递的$name参数不是字符串时。*@throws\WpOrg\Requests\Exception\InvalidArgument当传递的$value参数不是字符串时。*@throws\WpOrg\Requests\Exception\InvalidArgument当传递的$attributes参数不是数组或具有数组访问权限的可迭代对象时。*@throws\WpOrg\Requests\Exception\InvalidArgument当传递的$flags参数不是数组时。*@throws\WpOrg\Requests\Exception\InvalidArgument当传递的$reference_time参数不是整数或null时。*/公共函数__construct($name、$value、$attributes=[]、$flags=[],$reference_time=null){if(is_string($name)===false){throw InvalidArgument::create(1,'$name','string',gettype($name));}if(is_string($value)===false){throw InvalidArgument::create(2,'$value','string',gettype($value));}if(InputValidator::has_array_access($attributes)===false||InputValidator::is_iterable($atteries)===false){throw InvalidArgument::create(3,'$attributes','array|ArrayAccess&Traversable',gettype($attrimates));}if(is_array($flags)===false){throw InvalidArgument::create(4,'$flags','array',gettype($flags));}if($referencetime!==null&&isint($referrencetime)===false){throw InvalidArgument::create(5,'$reference_time','integer|null',gettype($reference _time));}$this->name=$name;$this->值=$value;$this->属性=$attributes;$default_flags=[“创建”=>time(),'上次访问'=>时间(),“持久”=>false,“仅主机”=>true,];$this->flags=array_merge($default_flags,$flags);$this->reference_time=time();if($referencetime!==null){$this->reference_time=$reference.time;}$this->normalize();}/***获取cookie值**可以通过方法访问属性和其他数据。*/公共函数__toString(){返回$this->值;}/***检查cookie是否过期。**对照$this->reference_time检查年龄以确定cookie是否*已过期。**@return boolean如果过期则为True,如果时间有效则为false。*/公共函数is_expired(){//RFC6265第4.1.2.2节://如果cookie同时具有Max-Age和Expires属性,则最大值-//Age属性具有优先级,并控制//饼干。if(isset($this->attributes['max-age']){$max_age=$this->属性['max-age'];返回$max_age<$this->reference_time;}if(isset($this->attributes['expires'])){$expires=$this->属性['expires'];return$expires<$this->reference_time;}返回false;}/***检查cookie对于给定URI是否有效**@param\WpOrg\Requests\Iri$uri要检查的uri*@return boolean cookie是否对给定URI有效*/公共函数uri_matches(Iri$uri){if(!$this->domain_matches($uri->host)){返回false;}if(!$this->path_matches($uri->path)){返回false;}return empty($this->attributes['secure'])||$uri->scheme==='https';}/***检查cookie对于给定域是否有效**@param string$domain要检查的域*@return boolean cookie是否对给定域有效*/公共函数domain_matches($domain){if(is_string($domain)==false){返回false;}if(!isset($this->attributes['domain']){//手动创建的Cookie;请求创建的cookie将设置//将域设置为请求的域返回true;}$cookie_domain=$this->属性['domain'];if($cookie_domain===$domain){//cookie域和传递的域是相同的。返回true;}//如果cookie被标记为仅主机,而我们没有确切的//匹配,拒绝cookieif($this->flags['hostonly']===true){返回false;}if(字符串($domain)<=字符串($cookie_domain)){//由于明显的原因,如果传递的域//比cookie域短返回false;}if(substr($domain,-1*strlen($cookie_domain))!==$厨师_域){//cookie域应该是传递域的后缀。返回false;}$prefix=子项($domain,0,strlen($domino)-strlen($cookie_domain));if(substr($前缀,-1)!=“.”){//未包含在//域字符串应为%x2E(“.”)字符。返回false;}//传递的域应该是主机名(即,不是IP地址)。返回!预匹配('#^(.+\.)\d{1,3}\。\d{1,3}\。\d{1,3}\。\d{1,3}$#’,$domain);}/***检查cookie对于给定路径是否有效**根据RFC 6265第5.1.4节中的路径匹配检查**@param string$request_path要检查的路径*@return boolean cookie是否对给定路径有效*/公共函数path_matches($request_path){if(空($request_path)){//规范化指向根的空路径$request_path=“/”;}if(!isset($this->attributes['path']){//手动创建的Cookie;请求创建的cookie将设置//请求路径的路径返回true;}if(is_scalar($request_path)===false){返回false;}$cookie_path=$this->属性['path'];if($cookie_path===$request_path){//烹饪路径和请求路径是相同的。返回true;}if(strlen($request_path)>strlen{if(substr($cookie_path,-1)==='/'){//cookie路径是请求路径的前缀,最后一个//cookie路径的字符为%x2F(“/”)。返回true;}if(substr($request_path,strlen($cookie_path),1)==='/'){//cookie-path是请求路径的前缀//中未包含的请求路径的第一个字符//cookie-path是一个%x2F(“/”)字符。返回true;}}返回false;}/***规范化cookie和属性**@return boolean cookie是否成功规范化*/公共函数normalize(){foreach($this->attributes作为$key=>$value){$orig_value=$value;if(is_string($key)){$value=$this->normalize_attribute($key,$value);}if($value===null){unset($this->属性[$key]);继续;}if($value!==$orig_value){$this->属性[$key]=$value;}}返回true;}/***分析单个cookie属性**处理从cookie值分析单个属性。**@param string$name属性名*@param string | int | bool$value属性值(字符串/整数值,如果为空,则为true/flag)*@return mixed Value(如果可用),或者null(如果属性值无效)(应该跳过)*/受保护的函数normalize_attribute($name,$value){开关(strtolower($name)){案例“过期”://根据RFC 6265第5.2.1节进行到期解析if(is_int($value)){返回$value;}$expiry_time=字符串时间($value);if($expiry_time===false){返回null;}返回$expiry_time;案例“最大年龄”://根据RFC 6265第5.2.2节进行到期解析if(is_int($value)){返回$value;}//检查我们是否有有效年龄if(!preg_match('/^-?\d+$/',$value)){返回null;}$delta_seconds=(int)$value;如果($delta_seconds<=0){$expiry_time=0;}其他{$expiry_time=$this->reference_time+$delta_seconds;}返回$expiry_time;案例“域”://根据RFC 6265第5.2.3节,不需要域if(空($value)){返回null;}//根据RFC 6265第5.2.3节进行域规范化if($value[0]==='.'){$value=子项($value,1);}返回$value;违约:返回$value;}}/***为cookie标头格式化cookie**这是在向服务器发送cookie时使用的。**@return字符串Cookie格式化为Cookie头*/公共函数format_for_header(){返回sprintf(“%s=%s”,$this->name,$this->value);}/***设置Set-cookie头的cookie格式**这是在向客户端发送cookie时使用的。这不是真的*适用于客户端使用,但可能便于调试。**@return字符串Cookie格式化为Set-Cookie头*/公共函数format_for_set_cookie(){$header_value=$this->format_for_header();if(!空($this->attributes)){$parts=[];foreach($this->attributes作为$key=>$value){//忽略非关联属性if(is_numeric($key)){$parts[]=$value;}其他{$parts[]=sprintf(“%s=%s”,$key,$value);}}$header_value.=';'。内爆(';',$parts);}返回$header_value;}/***将cookie字符串解析为cookie对象**基于Firefox和相关项目中Mozilla的解析代码*是对RFC 2109和RFC 2616的故意偏离。副本请求6265*指定了一些这种处理,但不是以彻底的方式。**@param string$cookie_header cookie标头值(来自Set cookie标头)*@param字符串$name*@param int | null$reference_time*@return\WpOrg\Requests\Cookie解析Cookie对象**@throws\WpOrg\Requests\Exception\InvalidArgument当传递的$cookie_header参数不是字符串时。*@throws\WpOrg\Requests\Exception\InvalidArgument当传递的$name参数不是字符串时。*/公共静态函数解析($cookie_header,$name='',$reference_time=null){if(is_string($cookie_header)===false){throw InvalidArgument::create(1,'$cookie_header','string',gettype($cookie _header));}if(is_string($name)===false){throw InvalidArgument::create(2,'$name','string',gettype($name));}$parts=爆炸(';',$cookie_header);$kvparts=数组移位($parts);if(!空($name)){$value=$cookie_header;}elseif(strpos($kvparts,'=')===false){//某些站点可能只有一个没有等号分隔符的值。//偏离RFC 6265并假装它实际上是一个空白名称//(`=foo`)////https://bugzilla.mozilla.org/show_bug.cgi?id=169091$名称=“”;$value=$kvparts;}其他{列表($name,$value)=爆炸('=',$kvparts,2);}$name=修剪($name);$value=修剪($value);//属性键是区分大小写处理的$attributes=新的CaseInsensitiveDictionary();if(!空($parts)){foreach($parts作为$part){if(strpos($part,'=')===false){$part_key=$part;$part_value=真;}其他{列表($part_key,$part_value)=爆炸('=',$part,2);$part_value=修剪($part_value);}$part_key=修剪($part_key);$属性[$part_key]=$part_value;}}返回新的static($name,$value,$attributes,[],$reference_time);}/***从请求标头中分析所有Set Cookie标头**@param\WpOrg\Requests\Response\Headers$Headers要从中解析的标头*@param\WpOrg\Requests\Iri|null$origin URI用于比较cookie源*@param int | null$time过期计算的参考时间*@return数组*/公共静态函数parse_from_headers(头$headers,Iri$origin=null,$time=null){$cookie_headers=$headers->getValues('Set-cookie');if(空($cookieheaders)){返回[];}$cookies=[];foreach($cookie_headers作为$header){$parsed=self::parse($header,“”,$time);//默认域/路径属性if(空($parsed->attributes['domain'])&&!空($origin){$parsed->attributes['domain']=$origin->host;$parsed->flags['hostonly']=true;}其他{$parsed->flags['hostonly']=false;}$path_is_valid=(!empty($parsed->attributes['path'])&&$parsed->attributes['path'][0]=='/');if(!$pathis_valid&&!空($origin)){$path=$origin->path;//根据RFC 6265第5.1.4节的默认路径规范化if(substr($path,0,1)!=='/'){//如果uri-path为空或的第一个字符//uri-path不是%x2F(“/”)字符,输出//%x2F(“/”)并跳过其余步骤。$path=“/”;}elseif(substr_count($path,'/')===1){//如果uri-path包含不超过一个%x2F(“/”)//字符,输出%x2F(“/”)并跳过剩余的//步骤。$path=“/”;}其他{//从第一个输出uri-path的字符//最右边的字符,但不包括//%x2F(“/”)。$path=子($path,0,strrpos($patch,'/'));}$parsed->attributes['path']=$path;}//拒绝无效的cookie域if(!empty($origin)&&$已解析->域匹配($origin->主机)){继续;}$cookie[$parsed->name]=$parsed;}返回$cookies;}}

用户贡献的笔记

你必须登录在能够发表注释或反馈之前。