Full List of Common $_SERVER Variables | PHP Course

Full List of Common $_SERVER Variables | PHP Course

$_SERVER is a superglobal array in PHP that provides information about the server environment and the current request. It contains data such as headers, paths, and script locations, which can be useful for debugging, processing requests, and configuring applications.

Here's a concise summary of the `$_SERVER` variables with short descriptions and common use cases:

 

 Full List of Common `$_SERVER` Variables

 

1. `$_SERVER['SERVER_NAME']`

    Description: The server's hostname.

    Use: To get the domain name of the server, e.g., `example.com`.

 

2. `$_SERVER['SERVER_ADDR']`

    Description: The server's IP address.

    Use: To get the IP address of the server hosting the script.

 

3. `$_SERVER['SERVER_PORT']`

    Description: The port on the server for the current request.

    Use: To determine the port used for the request, e.g., `80` for HTTP, `443` for HTTPS.

 

4. `$_SERVER['SERVER_PROTOCOL']`

    Description: The protocol used (e.g., HTTP/1.1).

    Use: To check the protocol version of the HTTP request.

 

5. `$_SERVER['REQUEST_METHOD']`

    Description: The request method used (e.g., GET, POST).

    Use: To determine if the request is GET, POST, or another method.

 

6. `$_SERVER['REQUEST_TIME']`

    Description: The timestamp of the start of the request.

    Use: To log or measure the time when the request started.

 

7. `$_SERVER['REQUEST_TIME_FLOAT']`

    Description: The timestamp of the start of the request as a float.

    Use: For highprecision time measurements.

 

8. `$_SERVER['SCRIPT_FILENAME']`

    Description: The absolute file system path to the current script.

    Use: To get the full path of the executing script.

 

9. `$_SERVER['SCRIPT_NAME']`

    Description: The path of the current script relative to the document root.

    Use: To get the script's path, useful for generating URLs.

 

10. `$_SERVER['PATH_INFO']`

     Description: Extra path information passed to the script.

     Use: To handle additional path information for routing purposes.

 

11. `$_SERVER['PATH_TRANSLATED']`

     Description: The file system path to the current script.

     Use: For file system operations, though less commonly used.

 

12. `$_SERVER['DOCUMENT_ROOT']`

     Description: The root directory of the document for the current script.

     Use: To determine the root directory for accessing files.

 

13. `$_SERVER['SERVER_ADMIN']`

     Description: The email address of the server administrator.

     Use: To get the admin contact for server issues.

 

14. `$_SERVER['CONTEXT_DOCUMENT_ROOT']`

     Description: The document root for the current context.

     Use: To get the root directory for the current web context.

 

15. `$_SERVER['CONTEXT_PREFIX']`

     Description: The virtual path for the current context.

     Use: To get the virtual path prefix for the web context.

 

16. `$_SERVER['REQUEST_URI']`

     Description: The URI of the current request, including query string.

     Use: To get the full URL path and query string.

 

17. `$_SERVER['QUERY_STRING']`

     Description: The query string of the current request.

     Use: To extract and process query parameters from the URL.

 

18. `$_SERVER['HTTP_USER_AGENT']`

     Description: The user agent string sent by the browser.

     Use: To detect the browser or device type for user agentbased logic.

 

19. `$_SERVER['HTTP_ACCEPT']`

     Description: The media types that the client can process.

     Use: To determine the content types accepted by the client.

 

20. `$_SERVER['HTTP_ACCEPT_CHARSET']`

     Description: The character sets that the client can process.

     Use: To determine the character encoding preferred by the client.

 

21. `$_SERVER['HTTP_ACCEPT_ENCODING']`

     Description: The contentencoding methods that the client can handle.

     Use: To handle content compression based on client preferences.

 

22. `$_SERVER['HTTP_ACCEPT_LANGUAGE']`

     Description: The preferred languages of the client.

     Use: To provide localized content based on client language preferences.

 

23. `$_SERVER['HTTP_CONNECTION']`

     Description: The type of connection the client prefers (e.g., keepalive).

     Use: To manage connection settings for persistent connections.

 

24. `$_SERVER['HTTP_REFERER']`

     Description: The URL of the page that referred the client to the current page.

     Use: To track where users are coming from or for referrerbased logic.

 

25. `$_SERVER['HTTP_DNT']`

     Description: The Do Not Track header indicating the client's tracking preference.

     Use: To respect the user's tracking preferences.

 

26. `$_SERVER['REMOTE_ADDR']`

     Description: The IP address of the client.

     Use: To identify the client's IP address for logging or security.

 

27. `$_SERVER['REMOTE_PORT']`

     Description: The port number on the client's machine used for the connection.

     Use: To get additional connection details for logging or security.

 

28. `$_SERVER['SERVER_SIGNATURE']`

     Description: The server version and virtual host information.

     Use: To get server details for diagnostic purposes.

 

29. `$_SERVER['SERVER_SOFTWARE']`

     Description: The server software being used.

     Use: To determine the server software version.

 

30. `$_SERVER['GATEWAY_INTERFACE']`

     Description: The CGI specification version used.

     Use: To identify the CGI specification version.

 

31. `$_SERVER['REDIRECT_STATUS']`

     Description: The status code of the redirection, if applicable.

     Use: To handle and debug redirections.

 

32. `$_SERVER['HTTP_X_REQUESTED_WITH']`

     Description: The value of the XRequestedWith header, often used to identify AJAX requests.

     Use: To differentiate between AJAX and standard requests.

 

33. `$_SERVER['HTTP_X_FORWARDED_FOR']`

     Description: The forwardedfor IP address, if the client is behind a proxy.

     Use: To get the original IP address of the client when using proxies.

 

This list provides a thorough overview of `$_SERVER` variables commonly used in PHP. For specific server configurations or PHP versions, there might be additional or fewer variables available.


Share this article







Related Posts




0 Comments



Load more Comments

Post a Comment


helllo
Ocec Copyright text of dont't copyright our content