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 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同