Information Systems

Calling url with curl & PHP with same session

Sometimes you need to use a currently opened session to call another url in the same site from a php script.

$url = 'myrul;
$cr = curl_init($url);
if (session_status() == PHP_SESSION_NONE)
$cookies = 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/';
$cookies .= ";othercookie=".$_COOKIE["othercookie"]; // add more cookies as desired
session_write_close(); // needed because PHP doesn't like one open session calling same session in another page.
curl_setopt( $cr, CURLOPT_COOKIE, $cookies);
curl_setopt($cr, CURLOPT_RETURNTRANSFER, true);
$n = curl_exec($cr);