I'm in need of help trying to figure out what is causing certain behaviour I get when using cURL. First, the code I am using:
PHP Code:
  function getData()
  {
    if (
$this->type == 'POST')
    {      
      
$this->login();
      
$data_pack $this->prepareVars($this->vars);
      
$curl = &new cURLManager($this->URL);
      
$curl->setOption(CURLOPT_HEADERtrue);
      
$curl->setOption(CURLOPT_POSTtrue);
      
$curl->setOption(CURLOPT_POSTFIELDS$data_pack);
      
$curl->setOption(CURLOPT_RETURNTRANSFERtrue);
      
$curl->setOption(CURLOPT_COOKIEFILE'cookie.txt');
      
$curl->setOption(CURLOPT_COOKIEJAR'cookie.txt');

      
$curl->setOption(CURLOPT_SSL_VERIFYPEERfalse);
      
$curl->setOption(CURLOPT_SSL_VERIFYHOSTfalse);

      
$curl->setOption(CURLOPT_REFERER'https://www.site.com/search.aspx');
      
$curl->setOption(CURLOPT_USERAGENT'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
      return 
$curl->getOutput();
    }
  }

  function 
login()
  {
    
$vars = array('...');

    
$data_pack $this->prepareVars($vars);

    
$curl = &new cURLManager('https://www.site.com/login.aspx?ReturnUrl=search.aspx');
    
$curl->setOption(CURLOPT_HEADERtrue);
    
$curl->setOption(CURLOPT_POSTtrue);
    
$curl->setOption(CURLOPT_POSTFIELDS$data_pack);
    
$curl->setOption(CURLOPT_RETURNTRANSFERtrue);

    
$curl->setOption(CURLOPT_COOKIEFILE'cookie.txt');
    
$curl->setOption(CURLOPT_COOKIEJAR'cookie.txt');

    
$curl->setOption(CURLOPT_SSL_VERIFYPEERfalse);
    
$curl->setOption(CURLOPT_SSL_VERIFYHOSTfalse);

    
$curl->setOption(CURLOPT_FOLLOWLOCATIONtrue);
    
$curl->setOption(CURLOPT_REFERER'https://www.site.com/login.aspx');
    
$curl->setOption(CURLOPT_USERAGENT'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    
$data $curl->getOutput();
  } 
These are two methods of the Request class I use to get HTML data from the site. This site requires a login to search some data on it. I send all the POST vars to search.aspx page which, if everything's ok, returns results that I need and use. For now, as you can see, I login ($this->login()) each time before POSTing data to search.aspx. After successfull login, the site redirects to search.aspx page (note the ReturnUrl var in the URL) which by default displays a search form (cURL is set to follow this redirect and it does).

I'll explain the behaviour I am getting. Let's say, I have no cookie.txt file (fresh request). After running my script (calling the getData() method) for the first time, the cookie.txt is created but I do not get any search results (only the form). Just a note, I DO know that the POST vars I send are correct as well as that login does not fail. After running the script the second time (with cookie.txt already present), I DO get the search results. I really don't get why this works after the second time only. Since the cookie is already present, I recieve whatever search results I need without any problem with each next request.

You could say, hey you got the cookie and it works when it's there, so what's the prob? The problem is, that the login session expires after 20 mins of inactivity, so, if I let the cookie rot without doing any search requests for 20 mins, I get no search results again. So I need to be able to re-login and get the search results again whenever I want, which, as I described, does not work with my code (it only works after running it twice).

When writing the code, this is the process I imagined and wanted to follow:

1. cURL logs into login.aspx and gets a fresh cookie.txt
2. It then POSTs my vars to search.aspx together with sending cookie.txt to the server and as a result gets the search results.

But for some reason it doesn't work correctly as I explained above. Do you guys have some hints regarding the problem? What should I try/correct? Any help would be nice.