title: Curl HTTP Post Login and Redirect
body: <p>I am trying to create a page to log in to a local router automatically. I am using CURL currently to log in to the page and authenticate. This part of the the code appears to be working correctly. The issue I am having is that once CURL has authenticated, I need to then redirect the user to this page so that they can navigate, however, I will also need to use the cookies collected by CURL.</p>
  
  <p>Here is my code as it stands at the moment</p>
  
  <pre><code>$data = array(
  'username' =&gt; 'admin',
  'password' =&gt; 'admin',
  );
  $ch = @curl_init();
  curl_setopt($ch, CURLOPT_URL,'http://192.168.69.1:65080/login.cgi');
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
  curl_setopt($ch, CURLOPT_HEADER, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_COOKIEFILE, 'public_html/cookie.txt');
  curl_setopt($ch, CURLOPT_COOKIEJAR, 'public_html/cookie.txt');
  
  $result = curl_exec ($ch);
  $info = curl_getinfo($ch);
  curl_close ($ch); 
  
  print_r($result);
  print_r($info);
  
  
  //Working until this point
  preg_match('/^Set-Cookie:\s*([^;]*)/mi', $result, $m);
  parse_str($m[1], $cookies);
  
  foreach($cookies as $key=&gt;$cookie)
  {
       setcookie($key, $cookie, time() + 60*60*24*30, '/');
  }
  
  header("location:".$info['redirect_url']);
  </code></pre>
  
  <p>As you can see I found a snippet to loop through the <code>$result</code> info and then set them as cookies before redirecting, however, this is not working correctly and I am redirected to the login page not the index page.</p>
  
  <p>If I do a further call before I close CURL, using the redirect url as the url, I do get a partial print of the index page, however, the important images etc are not displayed. But I need to be able to access the page and navigate rather than simply printing the page.</p>
  
  <p>Here is a print of <code>$result</code></p>
  
  <pre><code>HTTP/1.1 302 Found
  Location: /index.cgi
  Set-cookie: show_security_warning=deleted; expires=Sunday, 09-Jun-13 10:54:00 GMT
  Set-cookie: ui_language=en_US; expires=Tuesday, 19-Jan-38 03:14:07 GMT
  Content-Type: text/html
  Transfer-Encoding: chunked
  Date: Mon, 09 Jun 2014 10:54:01 GMT
  Server: lighttpd/1.4.31
  </code></pre>
  
  <p>Here is a print of <code>$info</code></p>
  
  <pre><code>Array
  (
      [url] =&gt; http://192.168.69.1:65080/login.cgi
      [content_type] =&gt; text/html
      [http_code] =&gt; 302
      [header_size] =&gt; 314
      [request_size] =&gt; 251
      [filetime] =&gt; -1
      [ssl_verify_result] =&gt; 0
      [redirect_count] =&gt; 0
      [total_time] =&gt; 0.484
      [namelookup_time] =&gt; 0
      [connect_time] =&gt; 0
      [pretransfer_time] =&gt; 0
      [size_upload] =&gt; 255
      [size_download] =&gt; 0
      [speed_download] =&gt; 0
      [speed_upload] =&gt; 526
      [download_content_length] =&gt; -1
      [upload_content_length] =&gt; 255
      [starttransfer_time] =&gt; 0
      [redirect_time] =&gt; 0
      [certinfo] =&gt; Array
          (
          )
  
      [redirect_url] =&gt; http://192.168.69.1:65080/index.cgi
  )
  </code></pre>
  
  <p>Here is my cookie.txt</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.
  
  192.168.69.1    FALSE   /   FALSE   0   AIROS_SESSIONID d19e097a07b7b76fd7d90267a8e1f4d2
  192.168.69.1    FALSE   /   FALSE   1370775278  show_security_warning   deleted
  192.168.69.1    FALSE   /   FALSE   2147483647  ui_language en_US
  </code></pre>
  
  <p>Finally here is a print of <code>$cookies</code></p>
  
  <pre><code>Array
  (
      [show_security_warning] =&gt; deleted
  )
  </code></pre>
  
  <p>If anyone can point me in the right direction of how to achieve the next step, I would be most grateful.</p>