Solved

parse url with embedded url?

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

717 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