Solved

parse url with embedded url?

Posted on 2011-09-08
3
264 Views
Last Modified: 2012-05-12
parse_url is failing where the "url" parameter is not urlencoded.
The error is "unable to parrse url" and causes an exit and a call stack dump.

How can I parse this url without an error or if this is not possible, just
bypass it?

0
Comment
Question by:pillmill
3 Comments
 

Author Comment

by:pillmill
ID: 36506767
By "url" parameter that is not urlencoded, an example would be:

http://mysite.com?first=1&url=http://example.com&embeddedparam=1& ....
0
 
LVL 12

Accepted Solution

by:
larsrohr earned 250 total points
ID: 36508129
Try this recommendation:
 http://www.php.net/manual/en/function.parse-url.php#99973

The solution is to be preventive before the call to parse_url():
<?php
$q_index = strpos($uri, '?');
if ($q_index !== FALSE &&
  (strpos($uri, ':', $q_index) !== FALSE || strpos($uri, '/', $q_index) !== FALSE)) {
  $begin = substr($uri, 0, $q_index);
  $end = substr($uri, $q_index, strlen($uri)-$q_index);
  $end = str_replace('/', '%2F', $end);
  $end = str_replace(':', '%3A', $end);
  $uri = $begin.$end;
}
?>

... and then you can parse_url($uri).  Of course, you can handle this manual urlencoding any way that makes sense for your situation.
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 36516504
This function seems to work.   I have not tested it exhaustively.  Best regards, ~Ray
http://www.laprbass.com/RAY_temp_pillmill.php
Outputs something like:
string(72) "http://mysite.com?first=1&url=http://example.com&embeddedparam=1&foo=bar"
array(3) {
  ["scheme"]=>
  string(4) "http"
  ["host"]=>
  string(10) "mysite.com"
  ["query"]=>
  string(54) "first=1&url=http://example.com&embeddedparam=1&foo=bar"
}
<?php // RAY_temp_pillmill.php
error_reporting(E_ALL);
echo "<pre>";


// A FUNCTION TO HANDLE MALFORMED URL STRINGS
function my_parse_url($url)
{
    // BREAK IT APART TO FIND THE ARGUMENTS
    $arr = explode('?', $url);
    $dat = $arr[0];
    unset($arr[0]);
    foreach ($arr as $key => $str)
    {
        $arr[$key] = urlencode($str);
    }
    $new = implode('?', $arr);
    $arr = parse_url($dat . '?' . $new);
    $arr["query"] = urldecode($arr["query"]);
    return $arr;
}


// TEST DATA
$url = 'http://mysite.com?first=1&url=http://example.com&embeddedparam=1&foo=bar';
var_dump($url);

// SHOW THE WORK PRODUCT
$arr = my_parse_url($url);
var_dump($arr);

Open in new window

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now