Initial project push.
This commit is contained in:
89
src/Services/Request.php
Normal file
89
src/Services/Request.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace Origo\Services;
|
||||
|
||||
class Request {
|
||||
|
||||
/**
|
||||
* Get a POST parameter.
|
||||
*
|
||||
* @param string $key
|
||||
* The key.
|
||||
* @param mixed $default
|
||||
* The default value.
|
||||
* @return mixed
|
||||
* The value.
|
||||
*/
|
||||
public function post(string $key, $default = NULL) {
|
||||
$input_stream = file_get_contents('php://input');
|
||||
$input = [];
|
||||
if ($input_stream) {
|
||||
$parsed = parse_str($input_stream, $input);
|
||||
}
|
||||
else {
|
||||
$input = $_POST;
|
||||
}
|
||||
return $input[$key] ?? $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a GET parameter.
|
||||
*
|
||||
* @param string $key
|
||||
* The key.
|
||||
* @param mixed $default
|
||||
* The default value.
|
||||
* @return mixed
|
||||
* The value.
|
||||
*/
|
||||
public function get(string $key, $default = NULL) {
|
||||
return $_GET[$key] ?? $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the request method.
|
||||
*
|
||||
* @return string
|
||||
* The method.
|
||||
*/
|
||||
public function method(): string {
|
||||
return $_SERVER['REQUEST_METHOD'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the request IP.
|
||||
*
|
||||
* @return string
|
||||
* The IP.
|
||||
*/
|
||||
public function ip(): string {
|
||||
// Check common proxy headers first
|
||||
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
||||
$ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
|
||||
return trim($ips[0]);
|
||||
}
|
||||
|
||||
if (!empty($_SERVER['HTTP_X_REAL_IP'])) {
|
||||
return $_SERVER['HTTP_X_REAL_IP'];
|
||||
}
|
||||
|
||||
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
|
||||
return $_SERVER['HTTP_CLIENT_IP'];
|
||||
}
|
||||
|
||||
// Fallback to REMOTE_ADDR
|
||||
return $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a cookie.
|
||||
*
|
||||
* @param string $key
|
||||
* The key.
|
||||
* @return string
|
||||
* The value.
|
||||
*/
|
||||
public function getCookie(string $key): string {
|
||||
return $_COOKIE[$key] ?? '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user