functions and classes added.

This commit is contained in:
2025-03-20 18:27:17 -07:00
parent 0c41ca9b65
commit 28513d367d
8 changed files with 250 additions and 72 deletions

40
src/Redirect.php Normal file
View 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();
}
}
}