sani
sani

Reputation: 137

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

Upvotes: 2

Views: 1753

Answers (1)

chrislondon
chrislondon

Reputation: 12031

cURL requests don't send cookies by default. If you want to pass all of the $_COOKIEs 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);

Upvotes: 1

Related Questions