html拿php中cookie的值,php file_get_contents()请求页面时获取cookie值

c58265e72272c8461c5092b8cc4f6095.gif

帝国CMS技术互助群:540946827 一帮大神带你装逼带你飞!还有漂亮的妹纸陪你玩哦!

a0f609b288c7de88131b6f6ba37c81b3.png

使用的file_get_contents获取页面内容后,我们可以使用$ http_response_header获取响应头信息,响应头信息里面一般包括饼干变量的值,具体实现代码如下:

file_get_contents('http://example.org');

$cookies = array();

foreach ($http_response_header as $hdr) {

if (preg_match('/^Set-Cookie:\s*([^;]+)/', $hdr, $matches)) {

parse_str($matches[1], $tmp);

$cookies += $tmp;

}

}

/* http://www.pouyao.com/article/1429.html */

print_r($cookies);

例外一种方法可以使用stream_get_meta_data()得到实现:

if (false !== ($f = fopen('http://www.example.org', 'r'))) {

$meta = stream_get_meta_data($f);

$headers = $meta['wrapper_data'];

$contents = stream_get_contents($f);

fclose($f);

}

// $headers now contains the same array as $http_response_header

具体代码如下:

$opts = array('http' => array('header'=> 'Cookie: ' . $_SERVER['HTTP_COOKIE']."\r\n"));

$context = stream_context_create($opts);

/* http://www.manongjc.com/article/1430.html */

$contents = file_get_contents('http://example.com/test1.txt', false, $context);

echo $contents;

?>

如对本文有疑问,请提交到交流社区,广大热心网友会为你解答!! 点击进入社区

打赏一下,我们会为大家提供更多优质资源!

91e7dcaaa1a0bd41a6bdf0bdc709f045.png

您的打赏将全部用来搜集更多优质资源!

weipayimg.jpg

扫码打赏,你说多少就多少

cc16f7477eabc8420c6a2b3eb63a44b8.png

cbb949506e1f1b1153ebda7bc4c213c4.png

打开微信扫一扫,即可进行扫码打赏哦

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值