【问题标题】:How to send query params as query string?如何将查询参数作为查询字符串发送?
【发布时间】:2016-06-27 10:02:31
【问题描述】:

我有这种情况。我正在为我的应用程序付款,网址类似于http://example.com/payment?referenceId=123。在此之前,我需要检查身份验证,如果用户未登录,则需要使用 redirect_url 概念。我有这个用于 redirect_url 的 url:http://example.com/login?redirect_url=URL 由于我在源 url 中也有查询参数,所以我不能将该 url 用作 redirect_url。我该怎么做?

【问题讨论】:

  • 你能解释一下“由于我在源 url 中也有查询参数,我不能将该 url 用作 redirect_url。我该怎么做?”
  • 在源 url 我有这个查询参数:referenceId=123。如果我把所有东西放在一起,它就像http://example.com/login?redirect_url=http://example.com/payment?referenceId=123。如果我们访问它,我们将丢失 referenceId 并且浏览器将其重定向到 http://example.com/login?redirect_url=http://example.com/payment。但我也需要referenceId

标签: php redirect uri


【解决方案1】:

执行此任务的一种可能方法是在重定向到登录页面时将http://example.com/payment?referenceId=123 存储在会话中,当登录完成时,检查会话是否具有重定向值,然后从会话重定向到该 URL。

【讨论】:

    【解决方案2】:

    检查身份验证存储 referenceId 作为会话变量。在您的身份验证页面中启动会话并检查 url-referenceId 是否等于该特定会话的存储 userId。

    session_start(); 
    
    $_SESSION['referenceId'] = 123; 
    
    if((url_ReferenceId)=$_SESSION['referenceId'])
    {
    
    //Check for authentication
    }
    

    【讨论】:

      【解决方案3】:

      对于网址http://example.com/login?redirect_url=http://example.com/payment?referenceId=12‌​3

      使用$redirect_url = $_GET['redirect_url']; // $redirect_url 包含http://example.com/payment?referenceId=12‌​3

      然后使用php的inbuild函数parse_url从$redirect_url中过滤掉'referenceId'

      <?php
      $url = '//www.example.com/path?googleguy=googley';
      
      var_dump(parse_url($url));
      ?>
      
      array(3) {
        ["host"]=>
        string(15) "www.example.com"
        ["path"]=>
        string(5) "/path"
        ["query"]=>
        string(17) "googleguy=googley"
      }
      

      查看更多信息http://php.net/manual/en/function.parse-url.php

      这部分googleguy=googley 将实际保存您所需的数据

      【讨论】:

        猜你喜欢
        • 2019-04-09
        • 1970-01-01
        • 1970-01-01
        • 2014-06-29
        • 2012-05-12
        • 2016-09-25
        • 1970-01-01
        • 2020-10-10
        • 2020-04-16
        相关资源
        最近更新 更多