PHP替换'?'之后的删除字符串

问题描述 投票:0回答:1

我有一个登录功能,您可以在页面A上以表格的形式提交数据,然后将数据发送到页面B,然后页面将您重定向回页面A,并将错误消息作为GET。

<?php 
// page B header code
  if(upload_fail) {
    header('Location: ' . $_SERVER['HTTP_REFERER'] . '?err=error_message');
  }
?>

如果一次登录失败,此方法就很好,但是第二次失败时,它会再次附加'?err = error_message',这将导致URL为:test.domain / pageA?err = error_message?err = error_message。

所以我的问题是如何修剪HTTP_REFERER,以便清除它包含的所有先前GET,然后附加新的GET?

希望我能正确地解释自己,如果我需要澄清一些事情,请询问。预先感谢您的评论:D

php url get str-replace http-referer
1个回答
0
投票
<?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);
?>

对于任何有兴趣的人,这是我的答案。

它获得URL,并且仅返回核心元素(方案,主机,端口[可选]和路径)。

输入:http://localhost:8888/index.php?err=error_message

输出:http://localhost:8888/index.php(如果端口设置为true)

我可能应该检查是否设置了每个值(使用isset,但现在可以使用。)>

© www.soinside.com 2019 - 2024. All rights reserved.