Re: [php] How to get request url string and server hostname
| From: | Erik Price | Date: | Tue, 25 Jun 2002 19:26:14 +0000 |
| Subject: | Re: [php] How to get request url string and server hostname | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-103825@lists.php.net to get a copy of this message | ||
On Tuesday, June 25, 2002, at 03:22 PM, Erik Price wrote:
However, I don't like using $_SERVER['QUERY_STRING'] because if there are empty GET variables, it grabs them. So, if you prefer, you can do this: $getVarsArr = array(); foreach ($_GET as $getVarName => $getVarVal) { $getVars[] = $getVarName . "=" . $getVarVal; } $getVarsStr = implode("&", $getVars); $request = "http://" . $_SERVER['HTTP_HOST'] . "/" . $_SERVER['PHP_SELF'] . "?" . $getVarsStr;Doh, I forgot the part where you test for empty -- amend the above in the following fashion: $getVarsArr = array(); foreach ($_GET as $getVarName => $getVarVal) { if (!empty($getVarVal)) { $getVars[] = $getVarName . "=" . $getVarVal; } } $getVarsStr = implode("&", $getVars); $request = "http://" . $_SERVER['HTTP_HOST'] . "/" . $_SERVER['PHP_SELF'] . "?" . $getVarsStr; Why would you care if there were empty GET vars? Well, if you had a big search engine or some other form where the user can specify a bunch of criteria or just a little bit, it's up to you. It's nice if you have to grab the GET data for whatever reason (repopulating links, etc) and you don't waste time with huge querystrings made up of mostly-empty variables. Erik ---- Erik Price Web Developer Temp Media Lab, H.H. Brown pricee@hhbrown.com