将PHP会话信息传递给cURL

时间:2012-06-28 07:29:56

标签: php jquery session curl

我在网站和cURL请求之间传递会话数据时遇到问题。

代码:
此代码在demo.xyz.com上,它在www.abc.com上对数据库的用户进行了验证,并在成功登录时创建了一个会话变量“SESSION_USER_ID”。
登录后,它使用jQuery .load()函数向“www.abc.com/dashboard.php”发起cURL请求。

$("#dash").load("get-rem-page.php?p=http://www.abc.com/dashboard.php").html("Loading...");

get-rem-page.php包含以下代码:

<?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;
}
?>

www.abc.com上的dashboard.php代码如下:

<?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>

DIV(id:破折号)中的输出是

我在dashboard.php&amp; sessionid =


欢迎会员nmber

问题:
dashboard.php中的会话变量为空。需要传递此会话值,而不对dashboard.php进行任何更改。用CURLOPT_COOKIE尝试了很多选项..但似乎都没有用。专家公开是必需的。

1 个答案:

答案 0 :(得分:0)

我用卷曲和饼干玩了一次,最后的工作代码是这样的:

    $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()));