functions and classes added.
This commit is contained in:
40
src/Redirect.php
Normal file
40
src/Redirect.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Use
|
||||
* $redirect->to('/login');
|
||||
* to trigger a redirect
|
||||
*/
|
||||
|
||||
|
||||
class Redirect {
|
||||
private ?string $url = null;
|
||||
private int $statusCode = 303;
|
||||
|
||||
public function url(string $relativeUrl, int $statusCode = 303): void {
|
||||
$this->statusCode = $statusCode;
|
||||
|
||||
// Detect HTTPS
|
||||
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? "https" : "http";
|
||||
|
||||
// Get Hostname
|
||||
$host = $_SERVER['HTTP_HOST'];
|
||||
|
||||
// Get Base Directory
|
||||
$basePath = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/\\');
|
||||
|
||||
// Construct Absolute URL
|
||||
$this->url = "$protocol://$host$basePath/" . ltrim($relativeUrl, '/');
|
||||
}
|
||||
|
||||
public function isset(): bool {
|
||||
return !is_null($this->url);
|
||||
}
|
||||
|
||||
public function execute(): void {
|
||||
if ($this->url) {
|
||||
header("Location: " . $this->url, true, $this->statusCode);
|
||||
exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user