I am having problem passing session data between my site and cURL request.
Code:
This code is on demo.xyz.com, it autenticates the user from DB at www.abc.com and creates a session variable 'SESSION_USER_ID' in case of successfull login.
After login, it initiates cURL request to "www.abc.com/dashboard.php" using jQuery .load() function.
$("#dash").load("get-rem-page.php?p=http://www.abc.com/dashboard.php").html("Loading...");
The get-rem-page.php has following code:
<?php
$url = $_REQUEST['p'];
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,$url);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl_handle, CURLOPT_COOKIESESSION, true);
curl_setopt($curl_handle, CURLOPT_COOKIE,session_name().'='.session_id());
curl_setopt($curl_handle, CURLOPT_COOKIEFILE, 'cookies.txt'); // set cookie file to given file
curl_setopt($curl_handle, CURLOPT_COOKIEJAR, 'cookies.txt');
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
if (empty($buffer))
{
print "<p>Unable to load remote page</p>";
}
else
{
print $buffer;
}
?>
Code of dashboard.php on www.abc.com is as under:
<?php
session_start();
include "classes/local_info.php";
$objCon = new cD;
$objFun = new cF;
$link=$objCon->connectMYSQL();
$vCon = $objCon->connectDB($link);
if($vCon==false){
echo "DB Error";
exit;
}
echo "i am in dashboard.php & sessionid = ".$_SESSION["SESSION_USER_ID"]."<hr />";
$id = $_SESSION["SESSION_USER_ID"];
?>
<p>Welcome member number <?php echo $_SESSION["SESSION_USER_ID"];?></p>
Output in the DIV (id:dash) is
i am in dashboard.php & sessionid =
Problem:
the session variable in dashboard.php is empty. Need to pass this session value, without making anychange to dashboard.php. Have tried tons of options with CURLOPT_COOKIE.. but none seems to work. Expert openion is required.
I was playing once with curl and cookies quite much and the final working code was like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_COOKIEJAR, session_name().'='.md5(session_id()));
curl_setopt($ch, CURLOPT_COOKIE, session_name().'='.md5(session_id()));