I have been trying to get the /forgot_password part from the url https://lucasfrank.devlock/forgot_password?done=1. I created the function below:
function getBaseURI(): string
{
$basePath = implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) . '/';
// Get the current Request URI and remove rewrite base path from it
// (= allows one to run the router in a sub folder)
$uri = substr(rawurldecode($_SERVER['REQUEST_URI']), strlen($basePath));
// Don't take query params into account on the URL
if (str_contains($uri, '?')) {
$uri = substr($uri, 0, strpos($uri, '?'));
}
// Remove trailing slash + enforce a slash at the start
return '/' . trim($uri, '/');
}
It worked locally but seems to be an issue on a domain hosting. I later came across the $_SERVER['SCRIPT_URL'] global variable that worked as perfect as the function above. I used the global variable and worked fine on the domain, but seems not to be valid locally.
I expected that this works on both ends but seems that is not possible. The global variable seems to be on and off, not sure when it’s accessible or not. I get this error accessing it on localhost:
Warning: Undefined array key “SCRIPT_URL” in C:laragonwwwadmin_dashboardfunctionsfunctions.php on line 13
I had to change to the old function, lost on when to or not to use. I don’t know what my function lacks or what I might be misdoing.