问题描述:

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?

网友答案:

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

网友答案:

Before including your url as a get parameter, use urlencode

$full_url = 'http://site.com/process.php?for=' . urlencode('http://www.anotherwebsite.com');

This function is convenient when encoding a string to be used in a query part of a URL, as a convenient way to pass variables to the next page.


To reverse the result of urlencode, use urldecode. As mario pointed out in a comment below, $_GET parameters are already urldecoded.

相关阅读:
Top