【问题标题】:Get Argument And Print It获取参数并打印它
【发布时间】:2010-08-05 18:30:22
【问题描述】:

我想对 URL 上的参数执行 print,如下所示(在本例中是 Google):

http://bitoffapp.org/?http://google.com/

我想得到它并像这样打印它:

print("Go <a href='$url'>back</a> to the page you was before the login);

我该怎么做?

【问题讨论】:

    标签: php url redirect


    【解决方案1】:
    $div = explode("?", $_SERVER['REQUEST_URI'], 2);
    $arg = end($div);
    //like Sarfraz says, you can also use directly $_SERVER['QUERY_STRING']
    
    echo sprintf(
        'Go <a href="%s">back</a> to the page...',
        htmlspecialchars(urldecode($arg), ENT_QUOTES));
    

    在实践中,您需要验证 URL 以查看

    • 如果它确实是一个有效的 URL(参见 FILTER_VALIDATE_URL
    • 如果您可以在那里找到您的站点前缀(检查 substr($url, 0, strlen($prefix)) === $prefix) 就足够了;$url 是 urldecoded 查询字符串)

    进行这些检查的原因是,否则攻击者可能会诱骗您的用户访问 URL,尽管这些 URL 以您的域为前缀,但实际上会将它们转发到恶意网站或受害者浏览器中易受攻击的注册协议。

    【讨论】:

      【解决方案2】:

      如果它必须来自 url,你应该把它 urlencoded 并作为一个 GET 变量。

      $prevUrl = urlencode("http://google.com/");
      http://bitoffapp.org/?url=$prevUrl;
      

      然后从 GET 全局将其读入您的 php 函数

      $url = urldecode($_GET['url']);
      print("Go <a href='$url'>back</a> to the page you was before the login");
      

      但是,我实际上会考虑从 _SERVER 全局获取他们以前的 url。 _SERVER Global.

      【讨论】:

        【解决方案3】:

        做:

        var_export($_REQUEST);
        

        获取:

        array ( 'http://www_google_com' => '', )
        

        如需正确参考,请使用 ?url=http://google.com/

        然后你可以在这些地方找到它::

        $_REQUEST['url']; // All POST, GET, ... variables
        $_GET['url'];
        

        【讨论】:

          【解决方案4】:

          如果您的网址是 http://bitoffapp.org/script.php?url={my_url},您可以这样做:

          echo '<a href="' . $_GET['url'] . '">Go back to the page you were on before login</a>';
          

          【讨论】:

            【解决方案5】:
            parse_url($url,PHP_URL_QUERY);
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2017-12-10
              • 2020-01-16
              • 2013-04-06
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多