how to maintain session in cURL in php?

Here is the best way i found to do this link :

the text bellow is a ‘remixed’ version of the content of the blogpost :

 $useragent = $_SERVER['HTTP_USER_AGENT'];
 $strCookie="PHPSESSID=" . $_COOKIE['PHPSESSID'] . '; path=/';

session_write_close();

$ch = curl_init();
$ch = curl_init($rssFeedLink); 
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_USERAGENT, $useragent);
curl_setopt( $ch, CURLOPT_COOKIE, $strCookie );

$response = curl_exec($ch); 
curl_close($ch); 

What does session_write_close() do? It, ends the current session and store session data. Apparently, PHP does not like when multiple scripts play around with the session, so, it locks it. Putting session_write_close makes sure that your current session is stored so you can retrieve it and use it.

if you don’t use session_write_close() a new session id will be generated instead of using the current session id.

Also PHPSESSID should be replaced by the name of the session variable. According to OWSAP recommandations it should be something more general like anId.

Sometimes you will need to send a user agent with the post so i included the CURLOPT_USERAGENT parameter.

Leave a Comment