1
 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.

3
  • Not sure what you are driving at as your function doesn't do anything with GET parameters. Commented Jul 26, 2011 at 11:10
  • My url has multiple GET paremeters in it. Commented Jul 26, 2011 at 11:12
  • That does not make it any clearer. Commented Jul 26, 2011 at 11:14

6 Answers 6

7

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']);
Sign up to request clarification or add additional context in comments.

Comments

2

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'));

1 Comment

Thank you. This is what I was aiming for.
2

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

Comments

1

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.

Comments

0

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

Comments

0
//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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.