我调用一个php脚本 http://site.com/process.php 将 url 作为其参数之一。 for=

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

然后我这样做并尝试 parse_url() 但 parse_url() 给出了解析错误。

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

我怎样才能编码 for 参数可以在发送端(在 url 中)或在接收端(php),以便 parse_url() 明白这只是一个看起来像 url 的参数吗?

有帮助吗?

解决方案

嗯,首先你必须 urlencode()for= 参数,则在 process.php, ,你可以简单地做

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

以下是功能:http://php.net/manual/en/function.urlencode.phphttp://php.net/manual/en/function.urldecode.php

其他提示

在将您的网址包含为 get 参数、用途 urlencode

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

当编码在URL查询部分中使用的字符串时,此功能很方便,作为一种将变量传递到下一页的方便方法。


反转结果 urlencode, , 使用 urldecode. 。正如马里奥在下面的评论中指出的那样, $_GET 参数已经被 urldecode 了。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top