如何使用 PHP 在 URL 中传递 URL(作为 GET 参数)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15254112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:54:25  来源:igfitidea点击:

How to pass URL in URL (as GET parameter) using PHP?

phpapacheurlparametersget

提问by Ramon K.

I'm having some problems passing URL's as GET parameter. When I try to access:

我在将 URL 作为 GET 参数传递时遇到了一些问题。当我尝试访问时:

http://www.linkebuy.com.br/linkebuy/parceiro?url=http%3A%2F%2Fwww.google.com

http://www.linkebuy.com.br/linkebuy/parceiro?url=http%3A%2F%2Fwww.google.com

I get the following message:

我收到以下消息:

Unexpected error.

意外的错误。

However, if I go for:

但是,如果我去:

http://www.linkebuy.com.br/linkebuy/parceiro?url=123

http://www.linkebuy.com.br/linkebuy/parceiro?url=123

Everything works just fine (it redirects to an inexistent site - 123 -, of course, but it does the expected). By elimination I can say there's something wrong with the urlparameter, but what is it?

一切正常(它重定向到一个不存在的站点 - 123 - 当然,但它符合预期)。通过消除,我可以说url参数有问题,但它是什么?

OBS:I'm using rawurlencode()to encode the URL.

OBS:我正在rawurlencode()对 URL 进行编码。

EDIT:Code you asked...

编辑:你问的代码......

In the first view, where the link is (http://www.linkebuy.com.br/notebook/detalhe?id=5):

在第一个视图中,链接在哪里(http://www.linkebuy.com.br/notebook/detalhe?id=5):

<!-- url() function just completes the right URL (production or development) -->
<a href="<?php echo url('linkebuy/parceiro/?url=' . rawurlencode($l->getUrl()), true) ?>" class="<?php echo $leadClass ?> oferta" target="_blank">
    <?php echo $l->getNomeFantasia() ?>
</a>

When clicked the link redirects to an action (/linkebuy/parceiro), where happens the following (basicly nothing, just keeping in the framework):

当点击链接重定向到一个动作 ( /linkebuy/parceiro) 时,会发生以下情况(基本上没有,只是保留在框架中):

public function execute($request, $response) {
    $response->addParameter('url', rawurldecode($request->getParameter('url', ''))); //This creates $url in the view
    $response->setTemplate('site/linkebuy/lead-parceiro.php'); //Forwards to the view
}

It includes the view, lead-parceiro.php(above on the question, I link to this page), where the head contains:

它包括视图lead-parceiro.php(在问题上方,我链接到此页面),其中头部包含:

<script type="text/javascript">
    setInterval(function(){ window.location = '<?php echo $url ?>'; },3000);
</script>

采纳答案by Ateszki

If you can't get rid of the restriction you can pass the url in 2 parts like this

如果你不能摆脱限制,你可以像这样分两部分传递 url

http://www.linkebuy.com.br/linkebuy/parceiro?protocol=http&url=www.google.com

And then parse it on your code to make the full url for the redirect.

然后在您的代码上解析它以制作重定向的完整网址。

回答by RiaD

You should use urlencodewhen you pass anything as URL parameter

urlencode当您将任何内容作为 URL 参数传递时,您应该使用