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.

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

报告相同问题?

悬赏问题

  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 Mac(标签-IDE|关键词-File) idea
  • ¥15 潜在扩散模型的Unet特征提取
  • ¥15 iscsi服务无法访问,如何解决?
  • ¥15 感应式传感器制作的感应式讯响器
  • ¥15 如何使用SC92F8003固件库解析私有协议数据?
  • ¥15 如何在音频中嵌入字符串(水印)信息进行传递
  • ¥30 plc怎么以设计说明书申请软著
  • ¥15 硬盘识别不了,需要初始化,可我的数据怎么办
  • ¥15 lvm2被mask了,怎么unmask都没用(标签-ubuntu|关键词-apt)