parse_url when url is a paramater?

pillmill
pillmill used Ask the Experts™
on
Parse_url produces the wrong result the one of the parameters is itself a url:

For example,

parse_str($theurl['query'],$params);  

will lose all of the params after the url parameter, because the "&"
delimiting the parameters is not recognized.

How can I fix this? I need all the parameters from a URL REST query, when
one of the parameter values is itself a URL.  
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sr. System Analyst
Commented:
you have to encode/decode url parameters before adding it to query string

for example say your parameter is this

http://www.hainkurt.com?a=1&b=2

encoded version will be

http%3A%2F%2Fwww.hainkurt.com%3Fa%3D1%26b%3D2

and pass this as

somepage.php?userID=HainKurt&myUrl=http%3A%2F%2Fwww.hainkurt.com%3Fa%3D1%26b%3D2

when you get myUrl, do the reverse, decode to get the original url ith parameters...
Also found this at php.net.  Don't know if it applies to you

URL's in the query string of a relative URL will cause a problem

fails:
/page.php?foo=bar&url=http://www.example.com

parses:
http://www.foo.com/page.php?foo=bar&url=http://www.example.com 
Most Valuable Expert 2011
Top Expert 2016

Commented:
Here is the demonstration script proving that HainKurt is on the ball.
http://www.laprbass.com/RAY_temp_pillmill.php
Outputs:
http://www.hainkurt.com?a=1&b=2
http%3A%2F%2Fwww.hainkurt.com%3Fa%3D1%26b%3D2
http://www.hainkurt.com?a=1&b=2

Best to all, ~Ray
<?php // RAY_temp_pillmill.php
error_reporting(E_ALL);

$url = 'http://www.hainkurt.com?a=1&b=2';
$enc = urlencode($url);
$new = urldecode($enc);

echo "<pre>";
echo PHP_EOL . htmlentities($url);
echo PHP_EOL . htmlentities($enc);
echo PHP_EOL . htmlentities($new);

Open in new window

OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Sandeep KothariProject Lead

Commented:
well you have to explode the parse string with & and before doing that you decode the parse string...

example code :

$theurl = "http://www.example.com?h=test&o=ok&urll=http://www.example.org&helo=bye&working=fine";

$param_arr = explode('&',$theurl);

print_r($param_arr);

Open in new window

Most Valuable Expert 2011
Top Expert 2016

Commented:
No points for this, please -- the question has already been answered.  The PHP URL functions are documented here:
http://php.net/manual/en/ref.url.php

These are the PHP functions that make it easy to get this right.
http://php.net/manual/en/function.urlencode.php
http://php.net/manual/en/function.urldecode.php

Best to all, ~Ray
I concur with Ray_Paseur that HainKurt's answer is the one to use.  If the variable "query" were a URL such as "http://www.example.com?x=y&z=3", it would have to be passed through urlencode() in order to keep the values for $_GET['x'] and $_GET['z']; without that, it is simply not possible to be 100% sure that all the attached variables (except maybe the first) remain attached to it.

@kshna: please be respectful of other experts. There is no reason that the user asking the question should have to deal with such childish outbursts. Your fix does not actually fix the problem, but merely illustrates the problem: Ray_Paseur was saying "no points" for the post it was stated in, indicating that HainKurt's answer was the one that should be awarded points.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial