Solved

parse url with embedded url?

Posted on 2011-09-08
3
282 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 109

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php construct 5 24
UPDATE query not working in mysqli php 8 48
ebay devID, appID, certID, userToken 2 24
Number of hours between date in DB and now 8 14
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

808 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