title: How to login with cURL with POST and Cookie
body: <p>The server successfully receives login, password and CAPTCHA. How do I do it with a cookie?</p>
  
  <p>I don't know what info is needed for help i'll show all:</p>
  
  <p>Login Form:</p>
  
  <pre><code>&lt;div id="loginForm"&gt;
   &lt;div id="logo"&gt;&lt;img src="logo.png" border="0" /&gt;&lt;/div&gt;
   &lt;div class="loginBar"&gt;Login&lt;/div&gt;
    &lt;form action="index.php" method="post"&gt;
      &lt;input type="hidden" name="p" value="login" /&gt;
      &lt;div class="line"&gt;
        &lt;label&gt;Captcha&lt;/label&gt;
        &lt;img src="captcha/securimage_show.php" border="0" /&gt;
      &lt;/div&gt;
      &lt;div class="line"&gt;
        &lt;label&gt;&amp;nbsp;&lt;/label&gt;
        &lt;input type="text" name="captcha" value=""/&gt;                
      &lt;/div&gt;
      &lt;div class="line"&gt;
        &lt;label&gt;&amp;nbsp;&lt;/label&gt;
        &lt;input type="submit" value="Login" /&gt;
      &lt;/div&gt;
    &lt;/form&gt;                        
   &lt;hr/&gt;
   &lt;a href="index.php?p=register" class="register" title="Click to register"&gt;Register&lt;/a&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>cURL code:</p>
  
  <pre><code>    extract($_POST);
  
      //set POST variables
      $proxy = '127.0.0.1:8118';
      $url = 'http://example.com/index.php';
      $fields_string= 'p=login&amp;user=' . $user . '&amp;pass=' . $passwd . '&amp;captcha=' . $_POST['captcha'] . '&amp;submit=Login';
  
          //open connection
          $ch = curl_init();
  
          //set the url, number of POST vars, POST data
          curl_setopt($ch,CURLOPT_URL, $url);
          curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
          curl_setopt($ch,CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
          curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
          curl_setopt($ch,CURLOPT_PROXY, $proxy);
          curl_setopt($ch,CURLOPT_COOKIEJAR, "cookie.txt");
          curl_setopt($ch,CURLOPT_COOKIEFILE, "cookie.txt");
      //curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
          //execute post
          $result = curl_exec($ch);
  
          print_r(curl_error($ch));
          print_r(curl_getinfo($ch));
          print_r(curl_errno($ch));
  
          //close connection
          curl_close($ch);
  </code></pre>
  
  <p>curl getinfo:</p>
  
  <pre><code>[url] =&gt; http://example.com/index.php 
  [content_type] =&gt; text/html 
  [http_code] =&gt; 200 
  [header_size] =&gt; 415 
  [request_size] =&gt; 325 
  [filetime] =&gt; -1 
  [ssl_verify_result] =&gt; 0 
  [redirect_count] =&gt; 0 
  [total_time] =&gt; 1.549389 
  [namelookup_time] =&gt; 3.7E-5 
  [connect_time] =&gt; 0.000138 
  [pretransfer_time] =&gt; 0.000142 
  [size_upload] =&gt; 62 
  [size_download] =&gt; 1585 
  [speed_download] =&gt; 1022 
  [speed_upload] =&gt; 40 
  [download_content_length] =&gt; 1585 
  [upload_content_length] =&gt; 0 
  [starttransfer_time] =&gt; 1.27051 
  [redirect_time] =&gt; 0 
  [certinfo] =&gt; Array ( ) 
  [redirect_url] =&gt;
  </code></pre>
  
  <p>Real cookie:</p>
  
  <pre><code>Host:                 example.com
  Name:                 PHPSESSID
  Path:                 /
  Content:              7qk7bb17nr030g5j59h2gq3nq6
  Content raw:          7qk7bb17nr030g5j59h2gq3nq6
  Expires:              At end of session
  Expires raw:          0
  Send for:             Any type of connection
  Send for raw:         false
  Created:              Fri 25 Nov 2011 10:37:24 PM EET
  Created raw:          1322253443569272
  Last accessed:        Sat 26 Nov 2011 11:06:02 AM EET
  Last accessed raw:    1322298361723991
  HTTP only:            No
  HTTP only raw:        false
  This domain only:     No
  This domain only raw: false
  Policy:               no information available
  Policy raw:           0
  Status:               no information available
  Status raw:           0
  ---
  </code></pre>
  
  <p>Gerenated file by libcurl in cookie.txt:</p>
  
  <pre><code># Netscape HTTP Cookie File
  # http://curl.haxx.se/rfc/cookie_spec.html
  # This file was generated by libcurl! Edit at your own risk.
  
  example.com FALSE   /   FALSE   0   PHPSESSID   crs9cm100agdfsujsncr964jg7
  </code></pre>
  
  <p>When i run this code, Account, Passwd and Captcha successfull is received by server, but have error with cookie:</p>
  
  <blockquote>
    <p>Make sure you've cookies enabled</p>
  </blockquote>
  
  <p>Also i saw that real cookie Content raw is different by generated with libcurl.</p>
  
  <p>Also i saw that from my form i successful catch the webserver cookie.</p>
  
  <p>PS!! I receive cookie from this link in login form:</p>
  
  <pre><code>  &lt;img src="captcha/securimage_show.php" border="0" /&gt;
  </code></pre>