2

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

3
  • You are not printing the result in A.php. Commented Jun 7, 2013 at 10:58
  • Because you didn't specify any cookie before executing the cURL? Commented Jun 7, 2013 at 10:59
  • When you load B.php from your browser, any cookies would be stored/read on your local browser session. When loading B.php via cURL through A.php, B would look for cookies on the browser session than made the request (cURL on localhost) not your local browser session. Commented Jun 7, 2013 at 13:49

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.