Documentation

Request

The Request class represents an HTTP request handler, providing methods to manage and sanitize request data from various sources, such as GET, POST, SERVER, COOKIE, and FILES. It offers methods to check for the existence of parameters, retrieve their values, sanitize inputs, and handle redirections. Below is a detailed overview of the class and its methods.

All values returned by methods of the Request class are already sanitized.

Usage Example

# Request::init();
if (Request::hasPostParam('username')) { // POST param
    $username = Request::getPostParam('username');
    echo "Hello, $username!";
}
if (Request::hasQueryParam('article-id')) { // url GET params
    $article_id = Request::getQueryParam('article-id');
    echo "This is the Article ID: $article_id";
}
print_r(Request::getHeaderArray());

Request::init() is typically invoked by the Environment controller. You can remove the call to Request::init() since it is invoked by Boostack\Environment::init() during application startup.

Methods

  • public static function init()
    Initializes the request by registering data from global variables.
    • This method returns void: It sets up the internal state of the Request class.
  • public static function hasPostParam(string $param): bool
    Checks if a POST parameter exists.
    • $param: The name of the POST parameter.
    • This method returns bool: True if the parameter exists, false otherwise.
  • public static function getPostParam(string $param)
    Retrieves a POST parameter value and sanitizes it.
    • $param: The name of the POST parameter.
    • This method returns mixed: The sanitized parameter value or null if it doesn't exist.
  • public static function getPostArray()
    Retrieves all POST parameters and sanitizes them.
    • This method returns array|string: The sanitized POST parameters.
  • public static function hasQueryParam(string $param): bool
    Checks if a QUERY parameter exists.
    • $param: The name of the QUERY parameter.
    • This method returns bool: True if the parameter exists, false otherwise.
  • public static function getQueryParam(string $param)
    Retrieves a QUERY parameter value and sanitizes it.
    • $param: The name of the QUERY parameter.
    • This method returns mixed: The sanitized parameter value or null if it doesn't exist.
  • public static function getQueryArray()
    Retrieves all QUERY parameters and sanitizes them.
    • This method returns array|string: The sanitized QUERY parameters.
  • public static function hasServerParam(string $param): bool
    Checks if a SERVER parameter exists.
    • $param: The name of the SERVER parameter.
    • This method returns bool: True if the parameter exists, false otherwise.
  • public static function getServerParam(string $param)
    Retrieves a SERVER parameter value and sanitizes it.
    • $param: The name of the SERVER parameter.
    • This method returns mixed: The sanitized parameter value or null if it doesn't exist.
  • public static function getServerArray()
    Retrieves all SERVER parameters and sanitizes them.
    • This method returns array|string: The sanitized SERVER parameters.
  • public static function hasHeaderParam(string $param): bool
    Checks if a HEADER parameter exists.
    • $param: The name of the HEADER parameter.
    • This method returns bool: True if the parameter exists, false otherwise.
  • public static function getHeaderParam(string $param)
    Retrieves a HEADER parameter value and sanitizes it.
    • $param: The name of the HEADER parameter.
    • This method returns mixed: The sanitized parameter value or null if it doesn't exist.
  • public static function getHeaderArray()
    Retrieves all HEADER parameters and sanitizes them.
    • This method returns array|string: The sanitized HEADER parameters.
  • public static function hasCookieParam(string $param): bool
    Checks if a COOKIE parameter exists.
    • $param: The name of the COOKIE parameter.
    • This method returns bool: True if the parameter exists, false otherwise.
  • public static function getCookieParam(string $param)
    Retrieves a COOKIE parameter value and sanitizes it.
    • $param: The name of the COOKIE parameter.
    • This method returns mixed: The sanitized parameter value or null if it doesn't exist.
  • public static function getCookieArray()
    Retrieves all COOKIE parameters and sanitizes them.
    • This method returns array|string: The sanitized COOKIE parameters.
  • public static function hasRequestParam(string $param): bool
    Checks if a REQUEST parameter exists.
    • $param: The name of the REQUEST parameter.
    • This method returns bool: True if the parameter exists, false otherwise.
  • public static function getRequestParam(string $param)
    Retrieves a REQUEST parameter value and sanitizes it.
    • $param: The name of the REQUEST parameter.
    • This method returns mixed: The sanitized parameter value or null if it doesn't exist.
  • public static function getRequestArray()
    Retrieves all REQUEST parameters and sanitizes them.
    • This method returns array|string: The sanitized REQUEST parameters.
  • public static function hasFilesParam(string $param): bool
    Checks if a FILES parameter exists.
    • $param: The name of the FILES parameter.
    • This method returns bool: True if the parameter exists, false otherwise.
  • public static function getFilesParam(string $param)
    Retrieves a FILES parameter value and sanitizes it.
    • $param: The name of the FILES parameter.
    • This method returns mixed: The sanitized parameter value or null if it doesn't exist.
  • public static function getFilesArray()
    Retrieves all FILES parameters and sanitizes them.
    • This method returns array|string: The sanitized FILES parameters.
  • public static function goToMaintenance()
    Redirects to the maintenance page.
    • This method returns void: It exits the script after redirection.
  • public static function getFriendlyUrl($virtualPath)
    Generates a friendly URL based on the virtual path provided.
    • $virtualPath: The virtual path.
    • This method returns string: The friendly URL.
  • public static function goToUrl($URL)
    Redirects the user to the specified URL.
    • $URL: The URL to redirect to.
    • This method returns void: It exits the script after redirection.
  • public static function goToHome()
    Redirects the user to the home page.
  • public static function goToError(int $status_code = NULL)
    Redirects the user to the error page.
    • $status_code: Optional. The HTTP status code to be used for the error page.
    • This method returns void: It redirects the user to the error page specified by the HTTP status code.
  • public static function goToLogout()
    Redirects the user to the logout page.
    • This method has no input variables.
    • This method returns void: It redirects the user to the logout page.
  • public static function getUserAgent()
    Retrieves the User-Agent string from the request headers.
    • This method has no input variables.
    • This method returns string|array: The User-Agent string from the request headers.
  • public static function getIpAddress()
    Retrieves the IP address of the client.
    • This method has no input variables.
    • This method returns string|false|array: The IP address of the client.
  • public static function sanitizeInput($data, $encoding = 'UTF-8')
    Sanitizes input data to prevent XSS attacks.
    • $data: The input data to be sanitized, can be an array or string.
    • $encoding: Optional. The character encoding (default is 'UTF-8').
    • This method returns array|string: The sanitized input data.
  • public static function generateCookieHash()
    Generates a hash for the remember-me cookie based on current time, IP address, and user agent.
    • This method has no input variables.
    • This method returns string: The generated cookie hash.
  • public static function checkCookieHashValidity($cookieValue)
    Checks the validity of the remember-me cookie hash.
    • $cookieValue: The value of the remember-me cookie.
    • This method returns bool: True if the cookie hash is valid, false otherwise.
  • public static function checkAcceptedTimeFromLastRequest($timeLastRequest)
    Checks if the time since the last request is within the accepted time limit.
    • $timeLastRequest: The time of the last request.
    • This method returns bool: True if the time since the last request is within the accepted time limit, false otherwise.