php 重定向和查询字符串

发布于 2024-09-03 14:13:17 字数 353 浏览 2 评论 0原文

我有脚本,

    <?php
$to = $_GET["to"];

header("Location: $to");
?> 

如果我调用参数 $ 中的脚本

out.php?to=http://site.ru/page.php?param1=1&param2=2

只是 http://site.ru/page.php?param1=1&

如何修复?我想要 $to = http://site.ru/page.php?param1=1&param2=2

i have script

    <?php
$to = $_GET["to"];

header("Location: $to");
?> 

if i call script such

out.php?to=http://site.ru/page.php?param1=1¶m2=2

in param $to be only http://site.ru/page.php?param1=1&

how to fix? i want that $to = http://site.ru/page.php?param1=1¶m2=2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

作业与我同在 2024-09-10 14:13:17

您可以在调用 out.php 的站点上转义 URL:

<a href="out.php?to=<?PHP echo htmlspecialchars(urlencode($to)); ?>">Go to $to</a>

You can escape the URL at the site calling out.php:

<a href="out.php?to=<?PHP echo htmlspecialchars(urlencode($to)); ?>">Go to $to</a>
时常饿 2024-09-10 14:13:17

&URI 中的保留字符。当您访问此 URL 时,¶m2=2
被解释为属于当前 URL,属于 to 的值。
如果你想按字面意思传输它,你必须用 % 编码它26

http://site.ru/page.php?param1=1%26param2=2

大多数编程语言都提供了这样做的函数。 (例如 JavaScriptPHP)。最好的办法是对整个 URL 进行编码。

& is a reserved character in an URI. When you access this URL, ¶m2=2
is interpreted as belonging to the current URL and not to the value of to.
If you want to transmit it literally, you have to encode it with %26:

http://site.ru/page.php?param1=1%26param2=2

Most programming languages provide a function to do so. (e.g. JavaScript, PHP). The best thing is to encode the whole URL.

灼疼热情 2024-09-10 14:13:17

$to 必须是 urlencoded,但请注意,您向任何人提供了重定向脚本,因此任何网络钓鱼者都可以使用它.
因此,最好将 url 存储在数据库中并仅传递标识符。

$to must be urlencoded, but note that you giving a redirect script to anyone, so, any phisher can use it.
So, it would be better to store urls in the database and pass only an identifier.

心病无药医 2024-09-10 14:13:17

尝试使用base64对to URL进行编码,然后在您展示的示例中将其解码,然后再将其传递到标头:)

try encoding the to URL in base64 and then in the example that u have shown decode it before you pass it to the header :)

玩世 2024-09-10 14:13:17

对它进行编码

urlencode($to)

urlencode it

urlencode($to)
不羁少年 2024-09-10 14:13:17

我之前遇到过同样的问题,这就是我所做的:

$arr=explode('?to=',$_SERVER['REQUEST_URI'],2);
$new_to=$arr[1];

现在您可以使用 $new_to 变量。
当然,如果您将其用于生产环境,我建议按照其他答案的建议对网址进行编码。我用它来测试curl脚本。以这种方式获取变量有很多缺陷,所以要小心。

I ran into the same problem before, this is what I did:

$arr=explode('?to=',$_SERVER['REQUEST_URI'],2);
$new_to=$arr[1];

Now you can use the $new_to variable.
Of course if you're using this for production environment, I would recommend encoding the url as the other answers advised. I was using it for testing curl script. getting the variable this way has lots of flaws, so be careful.

樱桃奶球 2024-09-10 14:13:17

您可以使用名为“html_entity_decode”的函数

单击此处了解更多信息关于此函数

或使用md5函数对URL进行加密,然后在将其放入变量时对其进行解密。

我希望这可以帮助你

You can use a Function called "html_entity_decode"

Click Here for more information about this function

or use md5 function to encrypt the URL and then decrypt it when you put it into a varriable.

I hope this can help you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文