Search code examples
phpurluriparse-url

parse_url() problem when one of parameters is url


I call a php script http://site.com/process.php that takes a url as one of its parameters. for=

http://site.com/process.php?for=http://www.anotherwebsite.com

I then do this and try to parse_url() but parse_url() gives a parse error.

$uri = $_SERVER['REQUEST_URI']; // /process.php?for=http://www.anotherwebsite.com
parse_url($uri);

How can I encode the for parameter either on the sending side (in the url) or on the receiving side (php) so that parse_url() understands that it's just a parameter that happens to look like a url?


Solution

  • Well, first you must urlencode() the for= parameter, then in process.php, you can simply do

    $url = $_GET["for"];
    $url = urldecode($url); // http://www.anotherwebsite.com
    

    Here are the functions: http://php.net/manual/en/function.urlencode.php http://php.net/manual/en/function.urldecode.php