开发者

PHP unset get parameter?

 function getUrlCurrently() {
    $pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";

if ($_SERVER["SERVER_PORT"] != "80")  {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
}  else  {
   $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}

I'm using this function to determine开发者_开发技巧 the current URL of the page. I want to know if it is possible to extend this function to unset a pre-determined $_GET parameter.

All of my $_GET values are stored in an array. So I can access the specific values by using

$my_array[0]

Is it expensive and not realistic to use my suggested logic to accomplish this task?

EDIT: I only want to print the URL to use it as a link.

My url has GET parameters in it.


Not sure what you really want to do with this, but $_GET (and other super-globals) are not read-only :

  • You can add values into them,
  • You can overide values,
  • And, of course, you can unset() values.

Note, though, that modifying $_GET is often not considered as good-practice : when one reads some code, he expects what's in $_GET to come from the parameters in the URL -- and not from your code.


For instance, you can absolutely do something like this :

unset($_GET['my_item']);


Update to your function:

function getUrlCurrently($filter = array()) {
    $pageURL = isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on" ? "https://" : "http://";

    $pageURL .= $_SERVER["SERVER_NAME"];

    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= ":".$_SERVER["SERVER_PORT"];
    }

    $pageURL .= $_SERVER["REQUEST_URI"];


    if (strlen($_SERVER["QUERY_STRING"]) > 0) {
        $pageURL = rtrim(substr($pageURL, 0, -strlen($_SERVER["QUERY_STRING"])), '?');
    }

    $query = $_GET;
    foreach ($filter as $key) {
        unset($query[$key]);
    }

    if (sizeof($query) > 0) {
        $pageURL .= '?' . http_build_query($query);
    }

    return $pageURL;
}

// gives the url as it is
echo getUrlCurrently();

// will remove 'foo' and 'bar' from the query if existent
echo getUrlCurrently(array('foo', 'bar'));


To assemble a link with GET parameters in an array try:

unset($my_array['key']);
$url = getUrlCurrently() . '?' . http_build_query($my_array);

See http://www.php.net/manual/en/function.http-build-query.php


This has nthg to do with $_GET. You can just use the existing global data $_SERVER, or getenv, like this :

function GetCurrentUrl($debug=FALSE) {

    $pageURL = (strtolower($_SERVER["HTTPS"]) == "on") ? "https://" : "http://";

    if ($_SERVER["SERVER_PORT"] != "80")    {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    }
    else {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }

    // DEBUG
    if ($debug) {
        $msg = "DEBUG MODE: current URL= ".$pageURL ;
        if (function_exists('debug_msg')) {
            debug_msg($msg , $debug) ;
        }else {
            echo $msg ;
        }

    }

    return $pageURL;
}

EDIT: but I see where you are coming from with your $_GET statement. You mean the URI contents some parameters. You'll get them by $_SERVER['REQUEST_URI'], or as better suggested, using http_build_query

EDIT2: On top of that, with regards to one point of your question, you can also add a work around to setup a "rewriting"-like function as described in this php manual interesting example.


Wouldn't it be easier to user $_SERVER['SCRIPT_URI']?
It returns the full url without query parameters.


//your query string is ?a=1&b=2&c=3
function unset_get($param){
    //sets string to (array) $query_string 
    parse_str($_SERVER['QUERY_STRING'],$query_string);

    //removes array element defined by param 
    unset($query_string[$param]);

    //returns modified array as a string 
    return http_build_query($query_string);
}
print unset_get( 'b');
//returns "a=1&c=3"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜