I have a login function where you submit data in a form on pageA and that data is send to pageB which then redirects you back to pageA with the error messages as a GET.
<?php
// page B header code
if(upload_fail) {
header('Location: ' . $_SERVER['HTTP_REFERER'] . '?err=error_message');
}
?>
This works fine if you fail the login once, but the second time you fail it appends the '?err=error_message' again, which results in the url being: test.domain/pageA?err=error_message?err=error_message.
so my question is how do I trim the HTTP_REFERER so it clears whatever previous GET it contains and then append the new GET?
Hopefully I've explained myself properly, if I need to clear things up pleas ask. Thanks in advance for your comments :D
<?php
function removeQuery($url, $port = false) {
$portstring = $port ? ':'.parse_url($url)['port'] : '';
return parse_url($url)['scheme'].'://'.parse_url($url)['host'] . $portstring . parse_url($url)['path'];
}
// call it like so removeQuery($_SERVER[HTTP_REFERER]);
// If you want the port to be included for instance on a localhost
// call like so removeQuery($_SERVER[HTTP_REFERER], true);
?>
For anyone that is interested, here's my answer.
It gets the url and and returns only the core elements (Scheme, host, port[optional] and path).
input: http://localhost:8888/index.php?err=error_message
output: http://localhost:8888/index.php (if port is set to true)
I should probably check if each value is set (with isset), but for now it works.