简体   繁体   中英

How to get Cookies in CURL request using PHP?

I have two files: A.php and B.php.

Contents of A.php:

<?php
$ch = curl_init();
curlsetopt($ch,CURLOPT_URL,'localhost/b.php');
curl_exec($ch);
?>

Contents of B.php:

<?php
print_r($_COOKIE);
?>

it isn't printing COOKIES when loading A.php but printing when loading b.php directly.please help thanks

cURL requests don't send cookies by default. If you want to pass all of the $_COOKIE s from script a.php to b.php do this:

<?php

$cookie = array();

foreach ($_COOKIE as $key => $value) {
    $cookie[] = "{$key}={$value}";
};

$cookie = implode('; ', $cookie);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'localhost/b.php');
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
curl_exec($ch);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM