现在使用
进行页面重定向标题(“位置:{$ _SERVER ['HTTP_REFERER']}”);
但页面重定向到的URL类似于:
http://localhost:5110/page.php?1st=2&2nd=140413&3rd=547859
如何删除重定向网址的一部分? URL应该是:
http://localhost:5110/page.php?1st=2&3rd=547859
如果您对此有其他建议,请告诉我...... 感谢。
答案 0 :(得分:1)
$referer = parse_url($_SERVER['HTTP_REFERER']);
parse_str($referer['query'], $query);
unset($query['2nd']); // unset the desired element
$referer['query'] = http_build_query($query);
$url = '';
if (array_key_exists('scheme', $referer)) { $url .= "{$referer['scheme']}://"; }
if (array_key_exists('host', $referer)) { $url .= $referer['host']; }
if (array_key_exists('port', $referer)) { $url .= ":{$referer['port']}"; }
if (array_key_exists('path', $referer)) { $url .= $referer['path']; }
if (array_key_exists('query', $referer)) { $url .= "?{$referer['query']}"; }
if (array_key_exists('fragment', $referer)) { $url .= "#{$referer['fragment']}"; }
header("Location: $url");
答案 1 :(得分:0)
$ref = explode("?",$_SERVER['HTTP_REFERER']);
parse_str($ref[1], $qs);
unset($qs['query param to remove']);
$qs = http_build_query($qs);
$ref = $ref[0].'?'.$qs;
答案 2 :(得分:0)
试试这个:
$str = 'http://localhost:5110/page.php?1st=2&2nd=140413&3rd=547859';
echo remove_qs_key($str,"2nd");
function remove_qs_key($url, $key) {
$url = preg_replace('/(?:&|(\?))' . $key . '=[^&]*(?(1)&|)?/i', "$1", $url);
return $url;
}
结果:http://localhost:5110/page.php?1st=2&3rd=547859
答案 3 :(得分:-1)
$server = $_SERVER['SERVER_NAME']; //Returns the server name(localhost:5110)
$file = $_SERVER['REQUEST_URI']; //Returns the script name and path(/page.php)
echo $server.$file //Returns localhost:5110/page.php