dongzi8191 2017-11-16 12:44
浏览 53
已采纳

php - 跨curl请求维护会话

I have been able to use my current session in the process created by my curl request using the following code -

$query = http_build_query(array(
   "pcode"=>strtolower($code)
));
$url = $_SERVER['REQUEST_SCHEME'].":".PUBLIC_ROOT."xyz.php"."?".$query;

session_write_close();

$curl = curl_init($url);
$useragent = $_SERVER['HTTP_USER_AGENT'];
$strCookie = 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/';
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_GET, true);
curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
curl_setopt($curl, CURLOPT_COOKIE, $strCookie );

$json_response = curl_exec($curl);
curl_close($curl);

I used the following answer for the above code - https://stackoverflow.com/a/15374966/815929

Now the issue is that in the new process (created by the curl request), I need to set some session variables. However these session variables do not reflect in the calling process after curl_close().

I had some session vars in my calling script. I was able to use these session vars in the new process started by the curl request. This new process also set some new session vars. However when we are back to the calling process these new session vars are not reflected.

Are there additional curl options that I need to set to make this work?

  • 写回答

1条回答 默认 最新

  • doujun5009 2017-11-16 15:02
    关注

    I'm struggling to imagine how this runs at all.

    However these session variables do not reflect in the calling process after curl_close()

    Using the default session handler, a second script should just timeout if it is called from something like....

    session_start();
    ...
    curl_exec(); // at this point session file is still locked
    ...
    [session_close();]
    

    $strCookie = 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/';

    eh? You don't specify a path for a cookie in an HTTP request (you do in a response). This will tell the script that the client has a cookie named "path".

    Really this should be....

    $strCookie = session_name() . "=" . session_id();

    However these session variables do not reflect in the calling process after curl_close()

    Did you reload the session from storage? (see also previous note about session file locking). Your code should do something like....

    session_start();
    ...
    session_close();
    ...
    curl_exec();
    ...
    session_start();
    

    it would be cleaner to switch to a non-blocking handler, or better yet don't pass state via the session - POST the data to the second script read the response from the output stream.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程