mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-07-11 16:02:16 +00:00
@@ -60,6 +60,30 @@ class Request {
|
|||||||
return $this->headers;
|
return $this->headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get id address
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getIp() {
|
||||||
|
return isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get referer
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getReferer() {
|
||||||
|
return isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get user agent
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getUserAgent() {
|
||||||
|
return isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get header value by name
|
* Get header value by name
|
||||||
* @param string $name
|
* @param string $name
|
||||||
@@ -69,4 +93,14 @@ class Request {
|
|||||||
return (isset($this->headers[$name])) ? $this->headers[$name] : null;
|
return (isset($this->headers[$name])) ? $this->headers[$name] : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get request input or default value
|
||||||
|
* @param string $name
|
||||||
|
* @param string $defaultValue
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getInput($name, $defaultValue) {
|
||||||
|
return (isset($_REQUEST[$name]) ? $_REQUEST[$name] : $defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -21,7 +21,7 @@ class Response {
|
|||||||
* @param string $url
|
* @param string $url
|
||||||
*/
|
*/
|
||||||
public function redirect($url) {
|
public function redirect($url) {
|
||||||
header('location: ' . $url);
|
$this->header('Location: ' . $url);
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,9 +29,59 @@ class Response {
|
|||||||
$this->redirect(url());
|
$this->redirect(url());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add http authorisation
|
||||||
|
* @param string $name
|
||||||
|
* @return self $this
|
||||||
|
*/
|
||||||
public function auth($name = '') {
|
public function auth($name = '') {
|
||||||
header('WWW-Authenticate: Basic realm="' . $name . '"');
|
$this->headers([
|
||||||
header('HTTP/1.0 401 Unauthorized');
|
'WWW-Authenticate: Basic realm="' . $name . '"',
|
||||||
|
'HTTP/1.0 401 Unauthorized'
|
||||||
|
]);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function cache($duration = 2592000) {
|
||||||
|
$this->headers([
|
||||||
|
'Cache-Control: public,max-age='.$duration.',must-revalidate',
|
||||||
|
'Expires: '.gmdate('D, d M Y H:i:s',(time()+$duration)).' GMT',
|
||||||
|
'Last-modified: '.gmdate('D, d M Y H:i:s',time()).' GMT'
|
||||||
|
]);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Json encode array
|
||||||
|
* @param array $value
|
||||||
|
* @return self $this
|
||||||
|
*/
|
||||||
|
public function json(array $value) {
|
||||||
|
$this->header('Content-type: application/json');
|
||||||
|
echo json_encode($value);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add header to response
|
||||||
|
* @param string $value
|
||||||
|
* @return self $this
|
||||||
|
*/
|
||||||
|
public function header($value) {
|
||||||
|
header($value);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add multiple headers to response
|
||||||
|
* @param array $headers
|
||||||
|
* @return self $this
|
||||||
|
*/
|
||||||
|
public function headers(array $headers) {
|
||||||
|
foreach($headers as $header) {
|
||||||
|
header($header);
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user