title: Weird issue with storing and unsetting session from redirect in Google Chrome with PHP
body: <p>I'm getting a weird issue only in <strong>Google Chrome</strong>. (It's fine in Firefox, IE) The code is below to recreate this. The steps are:</p>
  
  <ol>
  <li>Navigate to a page that sets a session variable and redirects</li>
  <li>The new destination page calls a function that:
  <ol>
  <li>Reads the session variable and stores it in a local variable</li>
  <li>unsets the session variable</li>
  <li>returns the local variable</li>
  </ol></li>
  <li>Then the page reads the return value and echos it into the HTML (or tries to)</li>
  </ol>
  
  <p>I see two things:</p>
  
  <ol>
  <li><p>If I call echo before the first output of the page, it echos it into the browser <strong>but not</strong> into the actual source (i.e. "view source" will not show the text I "echo"-ed)</p></li>
  <li><p>If I call echo within the HTML, it doesn't echo it and doesn't print it into the source at all    </p></li>
  </ol>
  
  <p>The page that redirects (you can go directly to this page)</p>
  
  <pre><code>&lt;?php
  class LandingRedirector
  {
      private static $SESSION_VARIABLE = "redirectedFrom";
  
      function __construct() {}
  
      public function redirect()
      {
          //Make sure we have a session
          session_start();
  
          //Set the redirect variable
          $_SESSION[ self::$SESSION_VARIABLE ] = "red.php";
  
          session_write_close();
  
          //Redirect to correct page
          header( "Location: /end.php" );
          exit();
      }
  
      //Call this and this correctly prints the value
      public function setonly()
      {
          //Make sure we have a session
          session_start();
  
          //Set the redirect variable
          $_SESSION[ self::$SESSION_VARIABLE ] = "red.php";
  
          session_write_close();
      }
  
      public function getRedirect()
      {
          try
          {
              //Make sure we have a session
              session_start();
  
              //It's set
              if ( isset( $_SESSION[ self::$SESSION_VARIABLE ] ) )
              {
                  $redFrom = $_SESSION[ self::$SESSION_VARIABLE ];
  
                  //Unset it
                  unset( $_SESSION[ self::$SESSION_VARIABLE ] );
  
                  return $redFrom;
              }
          }
          catch (Exception $e) {}
  
          return null;
      }
  }
  
  //Create
  $redirector = new LandingRedirector();
  
  //Redirect
  if ( !isset( $stopHere ) || $stopHere != true ) $redirector-&gt;redirect();
  ?&gt;
  </code></pre>
  
  <p>The destination page</p>
  
  <pre><code>&lt;?php
      $stopHere = true;
  
      //Handles any redirect code
      require_once $_SERVER[ "DOCUMENT_ROOT" ] . "/red.php";
  
      //Were we redirected?
      $redirectOrigin = $redirector-&gt;getRedirect();
  
      //This echos into the browser but is NOT in the page when you "view source"
      if ( $redirectOrigin ) echo $redirectOrigin;
  ?&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;script&gt;
          &lt;?php
          //This does not echo into the browser AND is not visible in "view source" either!
          if ( $redirectOrigin ) echo $redirectOrigin;
          ?&gt;
      &lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;&lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>EDIT: I added another function that gets called only if you go directly to end.php instead of via a redirect. It does the same setting, retrieving and unsetting of the session variable, yet this one will work!</strong></p>
  
  <p><strong>EDIT 2: More code to show the weirdness</strong></p>
  
  <p>Compare these two functions. If you call function 1 (notice neither function checks the <code>isset()</code>), you'll get an error <code>Notice: Undefined index: redirectedFrom</code>. But if you call function 2, you won't get an error, it will exist. The only difference is the added unset call. And the error occurs on the line <strong>prior</strong> to the unset call!</p>
  
  <pre><code>    public function getRedirect1()
      {
          //Make sure we have a session
          session_start();
  
          //This should exist, but it throws an index error
          $redFrom = $_SESSION[ self::$SESSION_VARIABLE ];
  
          //Unset it
          unset( $_SESSION[ self::$SESSION_VARIABLE ] );
  
          return $redFrom;
      }
  
      public function getRedirect2()
      {
          //Make sure we have a session
          session_start();
  
          $redFrom = $_SESSION[ self::$SESSION_VARIABLE ];
  
          return $redFrom;
      }
  </code></pre>