User Contributed Notes: parse_url
sbedberg@ucdavis.edu
14-Jul-1999 10:23
At least for PHP 3.0.6 (WinNT), parse_url() does not deal with mailto: URLs well. For example, $a = parse_url('mailto:abc@xyz.com') resulted in a 2-element array, both of which were empty.
pablo@shark.ib.usp.br
22-Sep-1999 12:22
Example:
$url=parse_url(http://www.php.net/download-php.php3?csel=br);
$url[scheme] = http
$url[host] = www.php.net
$url[path] = /download-php.php3
$url[query] = csel=br
mogmios@excite.com
18-Nov-1999 11:49
Oddly enough it seems to always return an array of blank strings to me. At least when I loop through the array echo'ing the elements it gives me blanks.
norlab1@yahoo.com
15-Dec-1999 12:03
This seems to work if you know what the url is. What do you do if you are trying to get the url that has been passed to the current page and get a variable from the querystring????
dhosek@quixote.com
18-Jan-2000 12:06
I imagine that one could use
parse_url($REQUEST_URI)
to handle decoding the query string, but there's GOT to be something predefined or is PHP really brain-dead in this respect. Even ASP gets this one right.
assassin74@hotmail.com
19-Jan-2000 04:49
Only available if variable tracking has been turned on via either the track_vars configuration directive or the <?php_track_vars?> directive.
halcyon@wtp.net
24-Jan-2000 12:34
to dhosek@quixote.com
Why not just use $QUERY_STRING ?
it works just fine for me.
dmiles@vpnx.com
28-Jan-2000 08:14
I'm missing something here.<br>
URL = http://www.domain.com?foo=bar<br>
ASP syntax:<br>
foo = Request.QueryString("foo")<br>
Result: foo = "bar"<br>
Does PHP have syntax that I can simply pull 'foo' from the URL and get 'bar'?
bigdan@kreft.net
28-Jan-2000 04:10
Chances are, if you're using parse_url(), you're missing the point of PHP and one of its coolest features--automatic parsing of variables and assignment of values. I'm sure there is some esoteric need for parse_url(), but not if you're just trying to pass data from one page to another.
<p>
If your URL is http://foo.com/?var1=foo&var2=bletch and you want to print those values within your script, all you need is this:
<?
echo "var1 = $var1<br>",
"var2 = $var2<p>";
?>
This will print:
var1 = foo
var2 = bletch
You can also, for special cases, use the PATH_INFO environment variable. If you want to pass in information without the ugly ?key=value& assignments, you can use PATH_INFO on a URL like [http://foo.com/file.php/555/867-5309?name=Jenny] to do something like this:
<?
$argv = split("/", $PATH_INFO);
while (list($key,$val) = each($argv) ) {
printf("%s : %s<br>", $key, $val);
}
printf("%s's phone number is (%s) %s.<p>",
$name, $argv[1], $argv[2]);
?>
Thus passing this script a url like [http://domain.com/file.php/555/867-5309?name=jenny] will print: "jenny's phone number is (555) 867-5309". But if you can't think of a reason why you'd want to use PATH_INFO, don't sweat it--most people don't need it and likely never will--it's just nice to have in your arsenal for "special occassions."
<p>
Hope this helps.
michael@webontap.com
08-Feb-2000 02:57
All this is great for values past the / in a URL... what if I want to get the value of the secondary domain name? In this case:
my.foo.bar/parser.php3
...how do I get $foo to equal "my"?
markd@markdonline.com
11-Feb-2000 05:55
to request that part of the server name, michael, try requesting $server_name this will return the "www.something.net" part of the url. You can then parse it to return everything up untill the first "." By the way: Does anyone know where there might be a list of such variables like $sever_name, etc... ?
nklem@chello.nl
22-Feb-2000 03:10
A list of predefined apache variables can be found here:
http://www.php.net/manual/language.variables.predefined.php3
utirpan@surfre.com
03-Apr-2000 08:41
pars_url("http://") returns this message "Warning: unable to parse url (http://)"
This is an user entry, is there a way not see this message on the page ?
phpmaster@hotmail.com
11-Apr-2000 12:23
If you wanna stop people from sending 'http://' through parse_url() just check the url string being entered:
if($usr_url == "http://")
{
echo "Hey you didn't enter a URL!";
}else{
parse_url($usr_url);
}
rhianna@wwa.com
10-May-2000 09:18
To bigdan@kreft.net. Your reply was helpful for printing the value of the variable assigned from the URL, but I can't seem to use the variable for comparisons or calculations. For example: I want to use an IF statement to qualify the value of the variable and respond based on that. ASP is simple this way - once a variable is defined such as foo = cInt(Request("foo")) and the value from the URL string is "1" then foo = 1 and foo can be used in the way that I need to use it.
However, with PHP I keep receiving a parse error when I try to do anything but output the value of the variable. See below for the sample code.
<?php
echo $sh; // writes the value of $sh to the page, in this case = 1
echo $debug; // writes the value of $debug to the page, in this case = 1
if ($sh) = 1;
require ( 'include/file.inc' );
endif;
?>
Any input is welcomed!