title: How to connecting to the remote site with CURL and header location
body: <p>I am logging from site A through CURL on site B, I get the cookies from the connection and write to file called cookie.txt.</p>
  
  <p>But when I pass the cookie data to do the final header redirection ('Location: <a href="http://examplesiteb.com" rel="nofollow noreferrer">http://examplesiteb.com</a>'), it returns disconnected.</p>
  
  <p>The code I am using for connection is the same as in this other post, but with my modifications following the @ramrider user's suggestion that I suggested passing the cookies in the header, but I'm not sure how this should be done.</p>
  
  <p><a href="https://stackoverflow.com/questions/33463793/transfer-cookies-session-from-curl-to-header-location/48235350#48235350">Transfer cookies &amp; session from CURL to header location</a></p>
  
  <pre><code>  $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_COOKIESESSION, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, 10);
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
  
    // Download the given URL, and return output
    $output = curl_exec($ch);
  
    // Cookie Match
    preg_match_all('/^Set-Cookie:\s*([^\r\n]*)/mi', $output, $ms);
  
    $cookies = array();
    foreach ($ms[1] as $m) {
      list($name, $value) = explode('=', $m, 2);
      $cookies[$name] = $value;
  
      header('Set-Cookie: '.rawurlencode($name).'='.rawurlencode($value));
    }
  
    //print_r($cookies);
  
    $redirect = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
  
    header("Location: $redirect");
  
    //Close Match
    curl_close($ch);
  </code></pre>
  
  <p>The redirect happens but does not keep logged in.</p>
  
  <p>Below the example cookie file</p>
  
  <pre><code># Netscape HTTP Cookie File
  # http://curl.haxx.se/docs/http-cookies.html
  # This file was generated by libcurl! Edit at your own risk.
  
  #HttpOnly_example.com   FALSE   /   FALSE   0   ASP.NET_SessionId   10gtonkebkteuazx24sajlh2
  #HttpOnly_example.com   FALSE   /   TRUE    1515800212  DTE 898EC9C0EF0BA3985E402046547931EAA55808E14A2DE469F11F6F6C0CF9A28871C8704BE794885CDF7D3EE1E8B06698166F86C184C5B53FE61FA53CA13682C562E17BCB7B2FA16D7A7180E6EA973735
  </code></pre>
  
  <p>The users @martijn-pieters and @waqas-bukhary if they can help I thank you, because you deleted my answer in the other post, and I was completing with additional information for others that I can not find a solution.</p>
  
  <p>Thanks</p>