Tuesday 19 March 2013
Sending $_SESSION variables with cURL
If you need to maintain a PHP session while using cURL, make sure you put the line
session_write_close();
before your cURL call. PHP usually locks the session, so will not allow the new URL access to it. Closing the session for writing means that the session is unlocked, and the new URL will be able to access it.
Your code should then look like this:
session_write_close();
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_COOKIE, "PHPSESSID=".$_COOKIE['PHPSESSID']);
curl_setopt($c, CURLOPT_URL, "http://example.com/test.php");
$contents = curl_exec($c);
curl_close($c);
You should then maintain session state for both pages.
Next article: 2013 Events (02 April 2013)
Next Websites article: Custom alert and confirm functions using jQuery and CSS (14 May 2013)
--- Posted in Websites and viewed 8,791 times ---