title: Remove duplicate "Set-Cookie" header from PHP response
body: <p>This is an example script from a larger application, but shows the general process of what I'm trying to do.  If I have the following script:</p>
  
  <pre><code>&lt;?php
  ob_start();
  
  setcookie('test1', 'first');
  setcookie('test1', 'second');
  setcookie('test1', 'third');
  setcookie('test2', 'keep');
  
  //TODO remove duplicate test1 from headers
  
  ob_end_clean();
  die('end test');
  </code></pre>
  
  <p>I get the following response (as viewed via Fiddler):</p>
  
  <pre><code>HTTP/1.1 200 OK
  Date: Tue, 25 Apr 2017 21:54:45 GMT
  Server: Apache/2.4.17 (Win32) OpenSSL/1.0.2d PHP/5.5.30
  X-Powered-By: PHP/5.5.30
  Set-Cookie: test1=first
  Set-Cookie: test1=second
  Set-Cookie: test1=third
  Set-Cookie: test2=keep
  Content-Length: 8
  Keep-Alive: timeout=5, max=100
  Connection: Keep-Alive
  Content-Type: text/html
  
  end test
  </code></pre>
  
  <p>The problem is that <code>Set-Cookie: test1</code>... exists 3 different times, therefore increasing the header size unnecessarily. (Again, this is a simplified example -
   in reality, I'm dealing with ~10 duplicate cookies in the ~800-byte range.)</p>
  
  <p><strong>Is there anything I can write in place of the <code>TODO</code> that would get rid of the header either completely or so it only shows once?</strong> i.e. the following is my end goal:</p>
  
  <pre><code>HTTP/1.1 200 OK
  Date: Tue, 25 Apr 2017 21:54:45 GMT
  Server: Apache/2.4.17 (Win32) OpenSSL/1.0.2d PHP/5.5.30
  X-Powered-By: PHP/5.5.30
  Set-Cookie: test1=third
  Set-Cookie: test2=keep
  Content-Length: 8
  Keep-Alive: timeout=5, max=100
  Connection: Keep-Alive
  Content-Type: text/html
  
  end test
  </code></pre>
  
  <p>though the <code>Set-Cookie: test1=third</code> could not exist too and that's fine, but <code>Set-Cookie: test2=keep</code> needs to remain.  When I try <code>setcookie('test1', '', 1);</code> to delete the cookie, it adds an additional header to mark it as expired:</p>
  
  <pre><code>Set-Cookie: test1=first
  Set-Cookie: test1=second
  Set-Cookie: test1=third
  Set-Cookie: test2=keep
  Set-Cookie: test1=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0
  </code></pre>
  
  <p>And if I try removing the header like:</p>
  
  <pre><code>if (!headers_sent()) {
      foreach (headers_list() as $header) {
          if (stripos($header, 'Set-Cookie: test1') !== false) {
              header_remove('Set-Cookie');
          }
      }
  }
  </code></pre>
  
  <p>it removes all <code>Set-Cookie</code> headers when I only want <code>test1</code> removed.</p>