41 lines
965 B
PHP
41 lines
965 B
PHP
<?php
|
|
namespace Novaconium;
|
|
/**
|
|
* Use
|
|
* $redirect->url('/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();
|
|
}
|
|
}
|
|
}
|