Unable to store Session Data in CURL

I call http://localhost/test.php using CURL from http://localhost/invoke.php. In test.php i store some data in session. But when i try to access those session data from invoke.php, don’t get those session data. How i get those values?

I set following options in CURL.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost/test.php');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);    
curl_setopt($ch, CURLOPT_TIMEOUT, 30);

You can have a look at http://curl.haxx.se/mail/curlphp-2001-10/0019.html

It seems to be very close to your problem

Can you provide any example?

Content of invloke.php



include_once ('session.php');

$handles = array();

$urlArray = array('http://localhost/test.php' );  
  
  foreach($urlArray as $url){	
  
    // create a new single curl handle
    $ch = curl_init();
  
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    
    // add this handle to the multi handle
    curl_multi_add_handle($mh,$ch);
    
    // put the handles in an array to loop this later on
    $handles[] = $ch;
    
  }
  
  // execute the multi handle
  $running=null;
  do {
    curl_multi_exec($mh,$running);
    // added a usleep for 0.25 seconds to reduce load
    usleep (250000);
  } while ($running > 0);
  
  // get the content of the urls (if there is any)
  for($i=0;$i<count($handles);$i++)
  {
   
    // get the content of the handle
   // $output.= curl_multi_getcontent($handles[$i]);
   
  	
    // remove the handle from the multi handle
    curl_multi_remove_handle($mh,$handles[$i]);
            
  }


echo SessionHandler :: getData('DATA');

Content of test.php


include_once ('session.php');

echo SessionHandler :: setData('DATA', 'HELLO WORLD');


may we show yours source code … ?

Now modified code is as below.



include_once ('session.php');
$handles = array();
$urlArray = array('http://localhost/test.php' );    

  foreach($urlArray as $url){    

    // create a new single curl handle
    $ch = curl_init();  

    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_HEADER, 0);

    curl_setopt($ch, CURLOPT_COOKIESESSION, 1);

    curl_setopt($ch, CURLOPT_COOKIE,session_name().'='.session_id());  

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_TIMEOUT, 30);    

    // add this handle to the multi handle
    curl_multi_add_handle($mh,$ch);   

    // put the handles in an array to loop this later on
    $handles[] = $ch;   

  }
  
  // write session data and end session 
   session_write_close();

  // execute the multi handle
  $running=null;

  do {
    curl_multi_exec($mh,$running);

    // added a usleep for 0.25 seconds to reduce load
    usleep (250000);

  } while ($running > 0);

  

  // get the content of the urls (if there is any)

  for($i=0;$i<count($handles);$i++)  {   

    // get the content of the handle

   // $output.= curl_multi_getcontent($handles[$i]);

    // remove the handle from the multi handle

    curl_multi_remove_handle($mh,$handles[$i]);           

  }



session_start();

echo SessionHandler :: getData('DATA'); 

I’m glad you solved your problem, and thanks for posting the solution :slight_smile:

I able to solve this problem. I set

curl_setopt($ch, CURLOPT_COOKIE,session_name().'='.session_id());

to use same session id and use php’s session_write_close() function to write session data.

There is something like CURLOPT_COOKIE that stores session data (I think).