2

I have a page (in Wordpress) which contains an embedded database report form. The form is configured to filter the results based upon the query string of the page 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. What I would now like to do is reload the page using that url+query, BUT without getting stuck in an endless redirect loop!

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.

<?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.

Melanie Palen
  • 2,645
  • 6
  • 31
  • 50
Willby
  • 21
  • 2
  • Your question could use more context. `here` would do the trick. If you have the url: `http://localhost:3000/?this=that` you can use `echo $_GET['this']` to output `that` – admcfajn Oct 07 '17 at 16:02

1 Answers1

0

You could just check the current URL first to see if it matches before sending the redirect header.

<?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);
      }

?>
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
  • 1
    Thanks Jeff - I can't believe I hadn't thought of that before! I couldn't see the wood for the trees. An elegant solution. – Willby Oct 08 '17 at 15:47