【问题标题】:PHP - Proper Protocol for storing URLs in $_GETPHP - 在 $_GET 中存储 URL 的正确协议
【发布时间】:2012-11-14 18:16:01
【问题描述】:

在您自己的 php 页面的 URL 中存储其他网站 URL 的正确协议是什么?

例如,如果我想存储 google 这很容易。

MySite.com/test.php?url=http://www.google.com


但是比http://www.google.com 更复杂的URL 怎么样,比如我想存储另一个网站的PHP 页面。

(例如http://www.AnotherSite.com/page1.php?key=value&key2=value2&key3=value3

那么我将如何将它存储在我的 PHP 页面的 URL 中?您是否将所有“&”符号替换为“\&”短语?有没有更好的方法?这甚至一开始就有效吗?


谢谢!!!

【问题讨论】:

    标签: php url get store external-url


    【解决方案1】:

    只需encode 网址。为此,您可以使用 PHP 函数 urlencode()urldecode()

    【讨论】:

      【解决方案2】:

      如果你不想考虑:

      $url = 'mysite.com/test.php?' . http_build_query(array(
          'url' => $anything_you_want,
      ));
      

      否则:

      $url = 'mysite.com/test.php?url=' . urlencode($anything_you_want);
      

      您也可以使用rawurlencode()。显着的区别是它遵循RFC 3986

      进一步阅读:

      http_build_query() urlencode() rawurlencode()

      【讨论】:

      • 我不确定谁先回答,但我很喜欢你的进一步阅读!谢谢!
      【解决方案3】:

      如果您想将整个 URL 用作一个参数,只需使用 urlencode() 对其进行编码:

      http://us3.php.net/manual/en/function.urlencode.php

      例子:

      $url = 'http://www.AnotherSite.com/page1.php?key=value&key2=value2&key3=value';
      echo 'http://MySite.com/test.php?url='.urlencode($url);
      

      【讨论】:

        【解决方案4】:

        您可以对网址使用编码。 PHP 内置了非常简单的方法来做到这一点。

        http://php.net/manual/en/function.urlencode.php

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-11-30
          • 2013-05-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-17
          • 2015-06-05
          相关资源
          最近更新 更多