Note: Understanding Superglobals in PHP

Superglobals are special predefined variables in PHP that are accessible from any part of your script, and they carry information about the server, client, and environment.

  1. $_GET:

  2. $_POST:

  3. $_SESSION:

  4. $_COOKIE:

  5. $_SERVER:

    1. Server Variables:
      • $_SERVER['SERVER_SOFTWARE']: Contains the name and version of the server software running on the host.
      • $_SERVER['SERVER_NAME']: Represents the server host name or IP address.
      • $_SERVER['SERVER_ADDR']: Stores the IP address of the server.
      • $_SERVER['SERVER_PORT']: Holds the port number on which the server is listening for requests.
      • $_SERVER['DOCUMENT_ROOT']: Indicates the root directory of the server where the current script resides.
    2. Request Variables:
      • $_SERVER['REQUEST_METHOD']: Represents the request method used by the client (e.g., GET, POST, PUT, DELETE).
      • $_SERVER['REQUEST_URI']: Contains the URI (Uniform Resource Identifier) of the current request.
      • $_SERVER['QUERY_STRING']: Stores the query string portion of the URL.
      • $_SERVER['HTTP_HOST']: Indicates the host name provided by the client in the HTTP request header.
      • $_SERVER['HTTP_USER_AGENT']: Contains the user agent string of the client's browser.
      • $_SERVER['REMOTE_ADDR']: Stores the IP address of the client sending the request.
    3. Path Variables:
      • $_SERVER['SCRIPT_FILENAME']: Represents the absolute path of the current script being executed.
      • $_SERVER['SCRIPT_NAME']: Contains the virtual path of the current script relative to the server root.
      • $_SERVER['PHP_SELF']: Indicates the script name being executed, relative to the document root.
    4. HTTPS and Security:
      • $_SERVER['HTTPS']: Indicates whether the request is using HTTPS (secure HTTP protocol). Its value is 'on' if HTTPS is used, otherwise, it's not set.
      • $_SERVER['SSL_PROTOCOL']: Contains the SSL protocol version being used for the current request (if applicable).
    5. Authentication and Authorization:
      • $_SERVER['PHP_AUTH_USER']: Contains the username provided by HTTP authentication.
      • $_SERVER['PHP_AUTH_PW']: Holds the password provided by HTTP authentication.
      • $_SERVER['AUTH_TYPE']: Indicates the authentication method used (e.g., Basic, Digest).
    6. Additional Variables:
      • $_SERVER['REMOTE_PORT']: Contains the client's port number used for the connection.
      • $_SERVER['SERVER_PROTOCOL']: Indicates the protocol version used in the request (e.g., HTTP/1.1).
      • $_SERVER['GATEWAY_INTERFACE']: Contains the CGI (Common Gateway Interface) specification version used by the server.

    By leveraging the information provided by the $_SERVER superglobal, developers can customize their PHP scripts based on server configurations, incoming requests, and security considerations. Proper utilization of these server variables ensures robustness, security, and compatibility of web applications across different server environments.

  6. $_FILES:

  7. $_ENV:

Understanding how to properly use these superglobals is essential for building dynamic and interactive web applications in PHP. However, it's crucial to handle them securely, especially when dealing with user input, to prevent security vulnerabilities such as injection attacks and data manipulation. Always validate and sanitize user input before using it. Mastering the usage of superglobals empowers developers to build robust and secure web applications efficiently.