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)
}
?>