Security method for PHP to output English time and date (in RFC 1123 format)

  • 2021-06-29 10:42:17
  • OfStack

To write a client for an rest service in a recent project, signature verification requires an Date in HTTP Header format in RFC 1123.

This is a good solution. You can simply read through the PHP document and use the gmstrftime function to solve this problem.


string gmstrftime ( string $format [, int $timestamp = time() ] )

The code called is as follows:

echo gmstrftime("%a, %d %b %Y %T %Z",time());
// Output: Tue, 01 Apr 2014 16:16:07 GMT

During the debugging process, it was found that on the other computer, the output was not as expected, and then the Chinese language was produced:

2, 01  4 2014 16:20:02 GMT

Continuing to read the document, we found that the comment in the document indicated that the result of this function was affected by the setlocale result and by the default language of the current system.Use the following command to view the currently installed languages on your system:

locale -a

Based on the results just analyzed, it is OK to force setlocale to be designated as English with the following code:

setlocale(LC_TIME, 'en_US');
echo gmstrftime("%a, %d %b %Y %T %Z",time());

This article should have ended by now, but unfortunately it was on the Ubuntu machine for testing because it was the Live CD version and there happened to be no en_US is a language, but there is one en_US.UTF-8.At this point, it seems unsafe, I can't be sure if there is en_on the client running the codeUS or en_US.UTF-8.Fortunately, there is an comments in the document that can be replaced by gmdate, which is not affected by the result of setlocale:

gmdate('D, d M Y H:i:s') . ' GMT';

Case closed~Although the demand is small, it is not easy to write well, and more efforts will be needed in the future.


Related articles: