doukang8949 2014-03-16 13:00
浏览 60

当我尝试使用cURL获取页面内容时无限循环(服务器关闭?)(+发送session_id)

I have script [script /inc/user_record.php] (user on server A where I have to get json data [script /inc/json.php] (which is generated by another script on server A).

1 problem is: script print json output only if user logged in (so others users or guests can't read this message). I think that if I will send current user session_id() it will work fine.

I have next code on script user_record.php:

$json_url = "$SiteHost/inc/json.php?action=get_counters_data&period=6";
try {
    $ch = curl_init($json_url);
    $ses_id = session_id(); 
    $cookie = 'PHPSESSID='.$ses_id.';';
    curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    $json = curl_exec($ch);
    curl_close($ch);
} catch (Exception $e) {
    echo 'Exception thrown: ',  $e->getMessage(), "
";
}

This code have to work fine, but when I try to open page with script user_record.php, page will not load, browser will show he is trying to load (infinity loop?). Only server restart help solving the problem (or maybe time, I didn't try).

I tried next: I upload json.php script to my-web-site.com and logged in in it. I copied session id (firebug shows it for example) and pasted it into my local user_record script user_record.php. Look this code:

$json_url = "http://my-web-site.com/inc/json.php?action=get_counters_data&period=6";
try {
    $ch = curl_init($json_url);
    $ses_id = "session identificatory copied from website";
    $cookie = 'PHPSESSID='.$ses_id.';';
    curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    $json = curl_exec($ch);
    curl_close($ch);
} catch (Exception $e) {
    echo 'Exception thrown: ',  $e->getMessage(), "
";
}

All working fine now.

json.php code:

<?ob_start();
session_start();

$action = $_GET['action'];

if($_SESSION['UserAuthorized']) {
 #generating and printing json message in switch (get_counters_data path)
}
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!