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.

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

报告相同问题?

悬赏问题

  • ¥150 寻找王者荣耀开发作者,合作或者解答
  • ¥15 乳腺癌数据集 相关矩阵 特征选择
  • ¥15 我的游戏账号被盗取,请问我该怎么做
  • ¥15 通关usb3.0.push文件,导致usb频繁断连
  • ¥15 有没有能解决微信公众号,只能实时拍照,没有选择相册上传功能,我不懂任何技术,,有没有给我发个软件就能搞定的方法
  • ¥15 Pythontxt文本可视化
  • ¥15 如何基于Ryu环境下使用scapy包进行数据包构造
  • ¥15 springboot国际化
  • ¥15 搭建QEMU环境运行OP-TEE出现错误
  • ¥15 Minifilter文件保护