title: When/how do I know response is NOT redirect in ZF1 and WordPress
body: <p>I expect this question is too vague to be answerable as phrased in the title, so I'm expanding it to explain what I'm really trying to accomplish.</p>
  
  <p>What I meant by the title question is: I'm in a ZF1 MVC application processing a request, and I want to find out if my response is NOT to redirect (and similarly for a WordPress request). Kind of doesn't make sense without a crystal ball.</p>
  
  <p><strong>This is what I'm trying to do that made me phrase the question that way</strong></p>
  
  <p>My ZF1 MVC application requires users to register and login, and it incorporates WordPress functionality such that a logged-in MVC user is automatically logged-in as a corresponding WordPress user. In general, one user cannot see another user's data unless there is some relationship between the users (a la facebook friends) or unless the user owning the data has marked it publicly visible.</p>
  
  <p>I want to implement a button that a user can press to say "show me the current page the way someone else would see it".</p>
  
  <p>My initial/current thought was to go into the class holding the logged-in identity and add a notion of <em>temporary viewing identity</em>. My button would then set the <em>temporary viewing identity</em>, and redirect to the current url. The class holding the logged-in identity would simply report the <em>temporary viewing identity</em>, if it was set, as the logged-in identity. And presto, the user would see the current page as a different user would see it :-). But then the issue becomes how to unset the <em>temporary viewing identity</em>.</p>
  
  <p>My thought was that I would unset the <em>temporary viewing identity</em> whenever displaying the "final result" of a request, which to me meant whenever I can determine that my response is not a redirect. That's how I arrived at the question.</p>
  
  <p>I'm hoping that someone can either point me at the way to answer my question, or point me at a different/better approach to accomplishing what I'm trying to do. Left to my own devices, I'm thinking along these lines, and I'm not entirely sure of the assumptions I'm making.</p>
  
  <ol>
  <li>Every request gets a response, the response starts with headers, and the first header will indicate whether or not there's a redirect.</li>
  <li>Search the sources of ZF1 and WordPress to find all calls to header().</li>
  <li>At each such call, add a call to headers_list, and check the first header. If it doesn't specify a redirect, then unset any <em>temporary viewing identity</em></li>
  </ol>
  
  <p>Sounds fairly awful, but perhaps not totally crazy. I'd love to be steered in a better direction!</p>
  
  <p><strong>EDIT 1:</strong>
  Hmm, looks like calling header_register_callback() during bootstrap would avoid the need to track down occurrences of calls to header() in the sources. The callback function could call headers_list() to see if there's a redirect, and if not, clear the <em>temporary viewing identity</em></p>
  
  <p><strong>EDIT 2:</strong>
  Incorporating the comment from @DanFromGermany
  To process the array returned by headers_list() to determine efficiently and reliably whether or not there's a redirection I'd do something like:</p>
  
  <pre><code>$redirect = false;
  foreach(headers_list() as $header) {
      if (0 === stripos($header, 'location')) {
          // Skip whitespace and verify ":" is next
          $colon = ltrim(substr($header, strlen('location')), " \t");
          if ($colon[0] == ':') {
              $redirect = true;
              break;
          }
  }
  if (! $redirect) {
      clear_temporary_identity();
  }
  </code></pre>
  
  <p><strong>EDIT 3:</strong>
  Unfortunately, I just noticed that header_register_callback() was introduced in PHP 5.4, and I'm stuck at PHP 5.3 (because I want a properly-working APC with Apache mod-PHP, and as far as I can tell I can't get that combo after 5.3.1, as in XAMPP 1.7.3). So now I'm back to searching for calls to header() in the source, and trying to backtrack to places in the source where I could add code to check for redirect. I guess I could simplify a bit by searching for "redirect", but I know I've occasionally seen code that explicitly calls header("Location:...") and exits. So besides a different approach altogether, I'm back to looking for the best way to determine if a response is going to redirect...</p>
  
  <p><strong>EDIT 4</strong>
  For the ZF1 MVC application, I'm thinking that instead of searching through code to find paths that don't redirect, maybe I could just add the call to clear_temporary_identity() to the page layouts. If a request is going to redirect, it isn't going to generate the page layout first (I don't think!). That would sure simplify things. But for WordPress, I'm really not sure, I don't understand its control-flow at all, with html interspersed with php code seemingly willy-nilly. I'll have to search for redirects there to see if I can find some pattern.</p>