添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
从容的炒面  ·  PHP: ...·  5 小时前    · 
成熟的匕首  ·  PHP7以上 ...·  5 小时前    · 
性感的灯泡  ·  Call to undefined ...·  5 小时前    · 
没有腹肌的碗  ·  PHP Fatal error: ...·  5 小时前    · 
勤奋的风衣  ·  AAAI ISEF Awards - AAAI·  3 月前    · 
成熟的柳树  ·  威廉·莫里斯-抖音百科·  4 月前    · 
大气的木瓜  ·  pgsql 删除索引 - CSDN文库·  4 月前    · 

I recently had the need to extract the value of a Set-Cookie response header in PHP. Google lead me http_parse_cookie . Unfortunately, http_parse_cookie requires pecl_http which isn’t available with PHP out of box, and is a pain to install.

Other Google results suggest defining your own function .

After a bit of research, I found the SetCookie class in Guzzle . The implementation is really clean and is the best option for parsing Set-Cookie headers in PHP in my opinion.

Here’s a quick overview of how to use it…

How To Use It

Usage in pretty simple. The easiest way to use Guzzle is via composer . Once you’ve autoloaded the library, you pass the Set-Cookie header as an argument to GuzzleHttp\Cookie\SetCookie ’s fromString method as follows…

use GuzzleHttp\Cookie\SetCookie as CookieParser;
$cookieParser = new CookieParser;
$cookie = $cookierParser->fromString('Set-Cookie: key=value');

Now you can conveniently interact with the $cookie

echo $cookie->getName(); // key
echo $cookie->getValue(); // value

You can also access many other aspects of the cookie

echo $cookie->getExpires();
echo $cookie->getHttpOnly();
echo $cookie->getSecure();

Check the Set-Cookie class for a full list of available methods…

Conclusion

I hope this post came in useful for some people. If you have any questions or comments, feel free to drop a note below, or, as always, you can reach me on Twitter as well.

Hi, I'm Max!

I'm a software developer who mainly works in PHP, but loves dabbling in other languages like Go and Ruby. Technical topics that interest me are monitoring, security and performance. I'm also a stickler for good documentation and clear technical writing.

During the day I lead a team of developers and solve challenging technical problems at Rightpoint where I mainly work with the Magento platform. I've also spoken at a number of events.

In my spare time I blog about tech, work on open source and participate in bug bounty programs.

If you'd like to get in contact, you can find me on Twitter and LinkedIn.