开发者

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://si开发者_C百科te.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?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜