1

Right now there is a page redirection using

header("Location: {$_SERVER['HTTP_REFERER']}");

but the URL that the page is redirecting to is something like:

http://localhost:5110/page.php?1st=2&2nd=140413&3rd=547859

how can I remove a part of the URL of the redirection? the URL should be like :

http://localhost:5110/page.php?1st=2&3rd=547859

If you have another suggestions for this let me know... thanks.

4

4 回答 4

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");
于 2013-04-14T17:11:47.420 回答
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;
于 2013-04-14T16:54:14.600 回答
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

于 2013-04-14T16:54:46.743 回答
-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
于 2013-04-14T16:49:06.507 回答