php正则匹配返回头Set-Cookie中的有效cookie

  $response['header'] = 'Set-Cookie: pt2gguin=;Expires=Thu, 01 Jan 1970 00:00:00 GMT;Path=/;Domain=qq.com;
Set-Cookie: pt2gguin=o0xxxxxxxxx;Expires=Tue, 19 Jan 2038 03:14:07 GMT;Path=/;Domain=ptlogin2.qq.com;Secure;
Set-Cookie: p_uin=o0xxxxxxxxx;Path=/;Domain=graph.qq.com;
Set-Cookie: pt4_token=WRTEV-6cXMLgTdzx8WjTc87mvekJ1CeGyr*ncS3*qeE_;Path=/;Domain=graph.qq.com;
Set-Cookie: p_skey_forbid=;Expires=Thu, 01 Jan 1970 00:00:00 GMT;Path=/;Domain=graph.qq.com;
Set-Cookie: p_skey=dpsR1n7GeEkwAs7OX*rk5LuUOHu6b3bPNdaaRYG0Fvw_;Path=/;Domain=graph.qq.com;Secure;
Set-Cookie: pt_oauth_token=PG4mspA9BJr7i0wtdHeuRh2mpvg-UAAwjMNwU0e4ptuZSVDwjN5RNdEfDPkjrIwSu0EMHIPqL9Q_;Path=/;Domain=graph.qq.com;Secure;HttpOnly;
Set-Cookie: p_uin=;Expires=Thu, 01 Jan 1970 00:00:00 GMT;Path=/;Domain=qq.com;
Set-Cookie: p_skey=;Expires=Thu, 01 Jan 1970 00:00:00 GMT;Path=/;Domain=qq.com;
Set-Cookie: pt4_token=;Expires=Thu, 01 Jan 1970 00:00:00 GMT;Path=/;Domain=qq.com;
Set-Cookie: pt_login_type=3;Path=/;Domain=graph.qq.com;Secure;HttpOnly;SameSite=None;';
  preg_match_all('/ ([^;\s]+)=([^;]+);/is',$response['header'],$cookies);
  echo implode('', $cookies[0]); // 拼合
  print_r($cookies);die;
/ ([^;\s]+)=([^;]+);/is

此正则可排除 p_skey=; 此类空值cookie,达到只取有效值的目的

本文链接:

https://logs.ee/coding/20220128/cookie.html
1 + 1 =
快来做第一个评论的人吧~