Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

parse url with embedded url?

Posted on 2011-09-08
3
Medium Priority
?
338 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 1000 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

A few customers have recently asked my thoughts on Password Managers.  As Security is a big part of our industry I was initially very hesitant and sceptical about giving a program all of my secret passwords.  But as I was getting asked about them mo…
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…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

609 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