How to Detect the Server Protocol for PHP

You can use PHP to extract details of a server's protocol so that you can enable code specific to the server's version. A common use is to detect if the page is running standard HTTP or secure HTTPS. Information about the server's protocol is stored in the superglobal variable array "$_SERVER," which you can access anywhere in your script.

Instructions

    • 1

      Open your PHP source file in a text editor, such as Windows Notepad.

    • 2

      Use "$_SERVER['SERVER_NAME'];" to access the name of the server host that PHP is running. For example, "echo $_SERVER['SERVER_NAME'];" may display a string such as "www.myserver.com."

    • 3

      Use "$_SERVER['SERVER_SOFTWARE'];" to access the server identification string, which appears in headers. For example, "echo $_SERVER['SERVER_SOFTWARE'];" may display a string such as "Apache/2.2.3 (CentOS)."

    • 4

      Use "$_SERVER['SERVER_PROTOCOL'];" to access the revision and name of the information protocol used for the requested page. For example, "echo $_SERVER['SERVER_PROTOCOL'];" may display a string such as "HTTP/1.1."

    • 5

      Save the PHP file and load it on your server to make sure it works properly.

Tips & Warnings

  • PHP code must be contained within "<?php" and "?>" tags.

  • Depending on the server, "$_SERVER" may not provide meaningful values for many of its indices, especially if PHP is run from the command line.

Related Searches:

References

Comments

You May Also Like

  • How to Identify PHP Protocol

    When a Web server runs a PHP script, or serves an HTML Web page with snippets of PHP code interspersed throughout the...

  • How to Track an IP Address in PHP

    Tracking Internet Protocol (IP) addresses of your web visitors is an important technique for validating sessions and gathering useful data about your...

Related Ads

Featured