PDA

View Full Version : PHP pack() troubles (I will PayPal for expert help)


raymor
2004-08-10, 05:06 AM
'm looking for someone who is very familar with PHP
pack and unpack() magic for help with a script I'm working on.
I wrote a Perl script to query an ntp (time) server and I
need to port it to PHP. I've got most of it, but using the
named parameters with unpack() is foreign to me and
I've got some long, complicated unpacking to do. Looking
at the docs and at my code makes my head swim.

Here's my original (working) Perl script:
http://proxycheck.bettercgi.com:81/time.pl.txt
Mostly ported to PHP, with a bit of work left:
http://proxycheck.bettercgi.com:81/time.php.txt

raymor
2004-08-10, 05:08 AM
Those URLs should have been:

http://proxycheck.bettercgi.com:81/time_pl.txt
and
http://proxycheck.bettercgi.com:81/time_php.txt

Pazz
2004-08-18, 09:58 PM
Why do you need to pack and unpack?

If you just want to query a time server this does it using port13 and port37.

Thanks to google, Ralf at qrq.de and some modifications by me.

<?php
function query_time_server ($timeserver, $socket) {
$fp = fsockopen($timeserver,$socket,$err,$errstr,5);
# parameters: server, socket, error code, error text, timeout
if ($fp) {
fputs($fp,"\n");
$timevalue = fread($fp,49);
fclose($fp);
}
else {
$timevalue = " ";
}
$ret = array();
$ret[] = $timevalue;
$ret[] = $err;
$ret[] = $errstr;
return($ret);
}

$timestamp = time();
$datum = date("Y-m-d (D) H:i:s",$timestamp);
echo "Current date and local time on this server is $datum <br>\n";

$timeserver = "time-C.timefreq.bldrdoc.gov";
$timercvd = query_time_server($timeserver,37);
if (!$timercvd[1]) { # if no error from query_time_server
$timevalue = bin2hex ($timercvd[0]);
$timevalue = abs (HexDec('7fffffff') - HexDec($timevalue) - HexDec('7fffffff')) ;
$tmestamp = $timevalue - 2208988800; # convert to UNIX epoch time stamp
$datum = date("Y-m-d (D) H:i:s",$tmestamp - date("Z",$tmestamp)); /* incl time zone offset */
$doy = (date("z",$tmestamp)+1);

echo "Time check from time server <font color=\"red\">",$timeserver,"</font>.<br>\n"
."The current date and universal time is <font color=\"red\">",$datum,"</font> UTC.<br>\n"
."<font color=\"red\">",$timevalue,"</font> seconds since 1900-01-01 00:00.00.<br>\n"
."It is day <font color=\"red\">",$doy,"</font> of this year.<br>\n"
."The unix epoch time stamp is <font color=\"red\">$tmestamp.</font><br>\n";
}
else {
echo "Unfortunately, the time server $timeserver could not be reached at this time. "
."$timercvd[1] $timercvd[2].<br>\n";
}

$timercvd = query_time_server($timeserver,13);
if (!$timercvd[1]) {
$timevalue = $timercvd[0];
echo "Or if you prefer [<font color=\"red\">",$timevalue,"</font>].<br>\n";
}
else {
echo "Unfortunately, the time server $timeserver could not be reached at this time. "
."$timercvd[1] $timercvd[2].<br>\n";
}
?>

raymor
2004-08-18, 10:16 PM
Thanks. I had actually seen that but wanted to use
NTP rather than time or daytime protocols.
Most time servers only support ntp now and allso the time
formats for time and daytime and not well defined.