Session not maintaining curl php remote-login

832 Views Asked by At

I am trying to remote login using curl php. I can login to site but session not maintaining in curl response. I have triedsession_write_close() as how to maintain session in cURL in php? but its not going to work. I am using cookies too but nothing.

Here is what i tried:

  $ch = curl_init();

  $params['ror_csrf_token'] = $hiddenValue;
  $params['n'] = '';
  $params['email'] = '[email protected]';
  $params['password'] = 'xx';
  $params['remember_me'] = 'on';

  $form_action_url = 'http://www.xxxxxxxx.com/go/login';
  $postData = '';
  foreach($params as $k => $v)
  {
     $postData .= $k.'='.$v.'&';
  }
  $postData = rtrim($postData, '&');
  print_r($postData);
  $theaders[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
  $theaders[] = "Content-Type: application/x-www-form-urlencoded";
  $theaders[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
  $theaders[] = "Accept-Encoding: gzip,deflate,sdch";
  $theaders[] = "Accept-Language: en-US,en;q=0.8";
  $theaders[] = "Cache-Control: max-age=0";
  //$theaders[] = "Connection: keep-alive";
  //$theaders[] = "Content-Length: 119";
  curl_setopt($ch, CURLOPT_URL,$form_action_url);
  //curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);//follow redirection
  curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt'); // set cookie file to given file
  //curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt'); // set same file as cookie jar
  //curl_setopt($ch, CURLOPT_COOKIE, 'cookies.txt');
  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36');
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);   
  curl_setopt($ch, CURLOPT_HTTPHEADER, $theaders);


  curl_setopt($ch, CURLOPT_HEADER, false);
  curl_setopt($ch, CURLOPT_COOKIESESSION, true);
  //curl_setopt($ch, CURLOPT_NOBODY, false);
  //curl_setopt($ch, CURLOPT_REFERER, "http://www.ripoffreport.com/go/login");      
  //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

  echo $content = curl_exec($ch);
  $headers = curl_getinfo($ch); 
  $errors  = curl_error($ch);

  echo "<pre>";
  print_r($headers);
  curl_close ($ch);

Note: I have googled a lot since 10 days, but nothing seems to work for me.

1

There are 1 best solutions below

6
hindmost On

You have to set both CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE options to the same absolute path value ('cookies.txt' is a relative path). This is necessary in order to enable cookies auto-handling (and therefore, session maintaining) within redirects series which the script will have.

Also you shouldn't set CURLOPT_CUSTOMREQUEST and CURLOPT_POST options together, only one of them (CURLOPT_POST in your case).

So the script should have the following lines:

curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__).'/cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).'/cookies.txt');

BTW: session_write_close() doesn't affect to CURL requests.