简体   繁体   English

PHP如何在页面加载之前将查询字符串附加到URL

[英]PHP How to append a query string to a URL before page load

I have a page (in Wordpress) which contains an embedded database report form. 我有一个页面(在Wordpress中),其中包含嵌入式数据库报表。 The form is configured to filter the results based upon the query string of the page URL. 表单被配置为基于页面URL的查询字符串来过滤结果。 The query string is specific to the user so that each user sees results that are applicable to them. 查询字符串是特定于用户的,因此每个用户都可以查看适用于他们的结果。

I have written a PHP script in the page template which produces the correct URL, with query string added. 我在页面模板中编写了一个PHP脚本,该脚本可生成正确的URL,并添加了查询字符串。 What I would now like to do is reload the page using that url+query, BUT without getting stuck in an endless redirect loop! 我现在想做的是使用该url + query重新加载页面,但不会陷入无尽的重定向循环!

The code below does what I want, EXCEPT that it (obviously) gets stuck in a loop! 下面的代码完成了我想要的,除了(显然)陷入了循环! I have tried using exit or die commands, but that doesn't seem to help. 我尝试使用exitdie命令,但这似乎无济于事。

<?php

      $user_info = get_userdata(1);

      $url = esc_url(add_query_arg( array(
          'tbl_invites_username' => $user_info->user_number,
          'tbl_events_event_id' => '1'),
          'http://www.example.com/' ));

      header("location: " .$url);

?>

I am new to this, so I appreciate that there may be an entirely different way to achieve the same result - I am open to suggestions! 我对此并不陌生,因此我很高兴可能有完全不同的方式来获得相同的结果-我愿意接受建议!

Basically, when the user clicks on a link to the page containing the form, I want them to see a form with results filtered for them. 基本上,当用户单击包含该表单的页面的链接时,我希望他们看到具有针对其筛选结果的表单。

You could just check the current URL first to see if it matches before sending the redirect header. 您可以先检查当前URL ,然后在发送重定向标头之前查看其是否匹配。

<?php

      $user_info = get_userdata(1);

      $url = esc_url(add_query_arg( array(
          'tbl_invites_username' => $user_info->user_number,
          'tbl_events_event_id' => '1'),
          'http://www.example.com/' ));

      // https://stackoverflow.com/a/6768831/4233593
      if ($url != "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]") {
          header("location: " .$url);
      }

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM