title: cURL login session
body: <p>I need to use a website as a logged user. The code below does the job well, however it has to log in every time the script is called. I'd like to log in just once and from then on use session cookies.</p>
  
  <pre><code>$c=curl_init();
  $url='http://www.site.com/login/';              
  $opt=array(CURLOPT_URL=&gt;$url,CURLOPT_COOKIEJAR=&gt;'cookie.txt',CURLOPT_POST=&gt;1,CURLOPT_RETURNTRANSFER=&gt;1,CURLOPT_POSTFIELDS=&gt;'username=usr123&amp;password=pass&amp;submit='); //submit=1 works too
  curl_setopt_array($c,$opt);
  curl_exec($c);
  $url='http://www.site.com/next';
  $opt2=array(CURLOPT_URL=&gt;$url,CURLOPT_RETURNTRANSFER=&gt;1);
  curl_setopt_array($c,$opt2);
  $out=curl_exec($c);
  echo $out;
  curl_close($c);
  </code></pre>
  
  <p>I tried using session, but for some reason the user login doesn't work.</p>
  
  <pre><code>$c=curl_init();
  $ag='Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7 (.NET CLR 3.5.30729)';
  if(!file_exists('cookie.txt')){
    $url='http://www.site.com/login/';
    $opt=array(CURLOPT_URL=&gt;$url,
    CURLOPT_COOKIEJAR=&gt;'cookie.txt',
    CURLOPT_COOKIEFILE=&gt;'cookie.txt',
    CURLOPT_POST=&gt;0,
    CURLOPT_RETURNTRANSFER=&gt;1,
    CURLOPT_FOLLOWLOCATION=&gt;1,
    CURLOPT_USERPWD=&gt;'usr123:pass,
    CURLOPT_SSL_VERIFYPEER=&gt;false,
    CURLOPT_SSL_VERIFYHOST=&gt;0,
    CURLOPT_USERAGENT=&gt;$ag);
    curl_setopt_array($c,$opt);
    curl_exec($c);
  }  
  
  $url='http://www.site.com/next/';
  $opt2=array(CURLOPT_URL=&gt;$url,
  CURLOPT_COOKIEJAR=&gt;'cookie.txt',
  CURLOPT_COOKIEFILE=&gt;'cookie.txt',
  CURLOPT_RETURNTRANSFER=&gt;1,
  CURLOPT_FOLLOWLOCATION=&gt;1,
  CURLOPT_SSL_VERIFYPEER=&gt;false,
  CURLOPT_SSL_VERIFYHOST=&gt;2,
  CURLOPT_USERAGENT=&gt;$ag);
  curl_setopt_array($c,$opt2);
  $out=curl_exec($c);
  echo $out;
  curl_close($c);
  </code></pre>