【问题标题】:How to pass URL with parameter, as a parameter? [duplicate]如何将带参数的 URL 作为参数传递? [复制]
【发布时间】:2014-07-26 11:12:32
【问题描述】:

我正在尝试将 URL 作为参数传递,但该 URL 也有参数。当然,这些参数很重要,必须传输并正确解释。

例如: http://example.com/myclicktrackingscript.php?source=sidebar&url=http://example.com/redirect_to.php?site=site1

(是的,我知道,我可以在一个文件中一次单击跟踪和重定向,但在某些特定情况下,我就是不能:有时我需要传递带有参数的任何 URL 作为参数)

到目前为止,我已经使用了 URL 重写: http://example.com/redirect_to.php?site=site1 => http://example.com/redirect/site1/

.. 但这不是很方便(我需要更多灵活性的特定情况太多)

有更好的方法吗?

我的猜测可能是对参数 URL 进行哈希处理:

例如:http://example.com/myclicktrackingscript.php?source=sidebar$url=REJREJ12333232rerereE

...但是如何正确地“去散列”呢?

我从来没有使用过这种技术,有没有人关于如何使用的示例/提示/建议?

谢谢

【问题讨论】:

  • 只需使用 base64 编码和解码,使用 base64 对您的 url 进行编码,并在需要时对其进行解码。
  • @GTSSoft。然而,base64 编码将失去任何形式的“人类可读性”——这可能好也可能不好
  • 但是你为什么要允许用户阅读你的查询字符串呢?有什么具体要求吗?
  • 实际上,对于我正在从事的项目,我喜欢让它“非人类可读”的想法。我想我会使用 base64_encode,但我也明白了 urlencode 的意思,谢谢大家,这快速有效(在这里像往常一样)
  • 虽然我在这里读到使用 base64_encode 可能不安全? stackoverflow.com/questions/1374753/… => 我应该在 base64_encode 之上使用 urlencode 吗?

标签: php get


【解决方案1】:

你应该只对参数进行编码:

urlencode('http://example.com/redirect_to.php?site=site1')

编码后的值为:http%3A%2F%2Fexample.com%2Fredirect_to.php%3Fsite%3Dsite1

【讨论】:

  • 这么简单,谢谢。之后我需要对其进行 urldecode 吗?
  • @Baptiste 不,它会自动为你解码
【解决方案2】:

你可以使用urlencode

 $parmeter_url = "http://example.com/redirect_to.php?site=site1";
 $parmeter_url = urlencode($parmeter_url);
 $url = "http://example.com/myclicktrackingscript.php?source=sidebar&url=".$parmeter_url;
 echo $url;

Demo

【讨论】:

    【解决方案3】:

    虽然urlencode 可以工作,但默认情况下它也会导致此任务的混乱(看起来,IMOHO)输出。但是,可以清理!

    这是因为some "special characters" are allowed in a query string,所以我们可以修复过度编码,让它看起来更漂亮。

    $encoded = urlencode('http://target.com/foo_bar.php?a=1&b=2');
    // Remove unneeded encoding
    $encoded = str_replace('%3A', ':', $encoded);
    $encoded = str_replace('%2F', '/', $encoded);
    $encoded = str_replace('%3F', '?', $encoded);
    $encoded = str_replace('%3D', '=', $encoded);
    // There are several additional characters that need not be encoded in
    // the query string; refer to the link.
    

    $encoded 用于 URI 的查询部分时,此方法有效。

    $url = "http://blah/?url=$encoded";
    

    并且应该会产生一个可读性很强的 URL,如下所示。请注意,嵌入 URL 中的 & 仍然被编码,因为它表示值对之间的终止。

    http://blah/?url=http://target.com/foo_bar.php?a=1%26b=2
    

    如果不需要人类可读性,还有一些方法,例如对 URL 进行 base64 编码(must still be made URI-safe)或使用后端提供 URL 缩短服务(这将允许“http://blah.com/?afjnD”样式的 URL) .

    但是,散列本身是行不通的,因为(除非映射被持久化)散列函数是单向运算,原始数据不可恢复。

    【讨论】:

      猜你喜欢
      • 2015-11-08
      • 2011-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-06
      • 1970-01-01
      • 2015-10-29
      相关资源
      最近更新 更多