Wednesday, November 19, 2008

cURL Alternative to PHP Include (with SESSIONs)

This is followup to my previous post about cURL Alernative to PHP Include.  Basically, the method I posted before didn't copy over the current $_SESSION in PHP to the page that you were calling.  This mean, that page didn't know the session (i.e. if someone was logged in).

The following code over comes this problem and passes along PHP's $_SESSION information to the called page.  This has been tested for pages on the same server, I'm not sure how it will work if calling a page from another server.

<!-- save $_SESSION to use in call -->
session_start();
$strCookie = 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/';
session_write_close();

<!-- Begin cURL call-->
$curl_handle = curl_init('enter_external_url_here');
curl_setopt( $curl_handle, CURLOPT_COOKIE, $strCookie );
curl_exec($curl_handle);
curl_close($curl_handle);

No comments: