Hello,
I'm having issues with one of my projects, and I've come to a conceptual solution, but am not sure how to go about implementing it.
My project exists on a Virtual Dedicated Server run by GoDaddy. This is normally fine for most purposes, but since it only appears to be my own box and is actually shared with others this means there are certain things I can't do - such as resetting the system clock. Their clock is running currently about 8 minutes behind, and although they said they'd fix it, I need to think of a more long-term secure solution as I have many php scripts that require the current time much more accurately.
This got me thinking of a way to deal with this. I know in C/C++ there are ways to override pre-defined functions with your own definitions. If I was able to override the time() function in my scripts, and then was able to create a function that checks with an external synch source on the net for the time as opposed to the system time (defaulting to the system time if the synch-source is unavailable), then I would have a solution to my problems here.
So I basically have two questions here. The first is how I would go about overriding said function. Is there a way to do this globally so that all php scripts on my system would reference my custom function, or would I have to define the override in an include to be included in all of my pages? How would I go about doing either?
The second question is what external synch points are there to synch to, what is the protocol by which I would access them, and how would I go about getting php to pull in their data? I know such synch points exist; after all, most modern computer clocks have a "Set date and time automatically" option (my apple synchs to time.apple.com for example).
Any advice would be greatly appreciated.
Thanks,
Andy
Overriding the time() function?
Moderator: General Moderators
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: Overriding the time() function?
The only way I know of to override PHPs built-in functions is using the APD module
The alternative is to write your own time() function calling it myTime() or similar
The alternative is to write your own time() function calling it myTime() or similar
Code: Select all
<?php
function query_time_server ($timeserver, $port) {
$timevalue = null;
$fp = fsockopen($timeserver, $port, $err, $errstr, 5);
if ($fp) {
fputs($fp,"\n");
$timevalue = fread($fp,49);
fclose($fp);
}
$ret = array( $timevalue,
$err, // error code
$errstr // error text
);
return($ret);
} // function query_time_server()
$timeserver = "time-C.timefreq.bldrdoc.gov";
$timercvd = query_time_server($timeserver,13);
if (!$timercvd[1]) {
$timevalue = $timercvd[0];
echo 'Time check from time server ',$timeserver,' : [<font color="red">',$timevalue,'</font>]';
} else {
echo 'Unfortunately, the time server $timeserver could not be reached at this time. ';
echo $timercvd[1].' '.$timercvd[2];
}
?>