PHP Security method for outputting English time and date (RFC1123 format) _ php instance

Source: Internet
Author: User
This article mainly introduces the security method for PHP to output an English time and date. The time and date mentioned in this article is in RFC1123 format, and summarizes a gmdate function not affected by setlocale, for more information, see write a client for a rest service in the latest project. the signature verification must generate a Date in RFC 1123 format in the HTTP Header.

This problem is better solved. I simply read the PHP document and use the gmstrftime function to solve it.

The code is as follows:


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


The call code is as follows:

The code is as follows:


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


During debugging, it was found that on another computer, the output was not as expected, and then a Chinese character was generated:

The code is as follows:


2, 01 4 2014 16:20:02 GMT


Continue to read the document and find that the comments in the document indicate that the result of this function is affected by the setlocale result, which is influenced by the default language of the current system. Run the following command to view the language installed on the current system:

The code is as follows:


Locale-


Based on the analysis result, it is okay to set setlocale to English. the code is as follows:

The code is as follows:


Setlocale (LC_TIME, 'en _ us ');
Echo gmstrftime ("% a, % d % B % Y % T % Z", time ());


So far the article should have ended, but unfortunately on the Ubuntu machine for testing, because it is Live CD version, there is no en_US language, but there is a en_US.UTF-8. At this time the psychological drum, this approach seems not safe, I can not determine the client running the code, in the end there is en_US or there is a en_US.UTF-8. The comments section in the document can be replaced by gmdate. This function is not affected by the setlocale result:

The code is as follows:


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 made in the future.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.