-1

I'm trying to get the full url together with all the query strings of this url

http://localhost/test/searchprocess.php?categ=vessel&keyword=ves&search-btn=Search&page=5

I tried to use this function but it doesn't give me all of the query strings. It only saves the first query string.

function getCurrentURL() {
        $currentURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
        $currentURL .= $_SERVER["SERVER_NAME"];
        if($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443") {
            $currentURL .= ":".$_SERVER["SERVER_PORT"];
        } 
        $currentURL .= $_SERVER["REQUEST_URI"];
        return $currentURL;
    }

When I echo getcurrentURL(); it only gives me http://localhost/test/searchprocess.php?categ=vessel

How can I be able to get the full url?

dbf
  • 3,278
  • 1
  • 24
  • 34
technoken
  • 31
  • 4
  • Possible duplicate of [Get URL query string](http://stackoverflow.com/questions/8469767/get-url-query-string) – Jonathan Jul 03 '16 at 19:05

2 Answers2

0

I think your code is almost right. Try this:

$base_url = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on' ? 'https' : 'http' ) . '://' .  $_SERVER['HTTP_HOST'];
$url = $base_url . $_SERVER["REQUEST_URI"];
Fabio Widmer
  • 529
  • 1
  • 7
  • 19
  • Still the same. I already figured out now what's the problem. Since i'm trying to pass a variable with the url to another page using GET method. Maybe the ampersand and equal signs are conflicting. I just used session now to pass it. Thanks btw! – technoken Jul 03 '16 at 11:10
0

You want the parse_url() function:

http://php.net/manual/en/function.parse-url.php

$url = 'http://username:password@hostname:9090/path?arg=value#anchor';
var_dump(parse_url($url, PHP_URL_QUERY));

which returns

string(9) "arg=value"
user151841
  • 17,377
  • 29
  • 109
  • 171