Solved

parse url with embedded url?

Posted on 2011-09-08
3
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 110

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

730 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