title: Trouble getting .aspx page using php curl
body: <p>I'm trying to login to a secure aspx site using curl, and retrieve some of the account's data.<br>
  The page uses the aspx __VIEWSTATE to keep track of the browser's state.  From checking the request headers here is the sequence:</p>
  
  <p>user GETS from Login.aspx (including __VIEWSTATE)<br>
  user POSTS __VIEWSTATE, loginName and loginPassword to login.aspx -> server responds with 302
  user GETS Submissions.aspx</p>
  
  <p>submissions.aspx is a table of different clients referred to by __EVENTTARGET=dgrdSubmissions$ctl0x$ctl00 where the first $ctl0x represents that client's row.</p>
  
  <p>user POSTS _<em>VIEWSTATE,</em>_EVENTTARGET and an AdvisorView param to submissions.aspx -> server responds with 302
  user GETS Policy.aspx</p>
  
  <p>This works fine in the browser (Chrome - The site suspiciously breaks in Firefox with Message: Exception of type 'System.Web.HttpUnhandledException' was thrown) but in my php script the GET Policy.aspx responds with the login page and not the expected client info.</p>
  
  <p>Here is my code (minus error-checking and page displaying):<br>
  Helper Functions:</p>
  
  <pre><code>function curl_page($url){
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  $data=curl_exec($ch);
  curl_close($ch);
  return $data;
  }
  
  function curl_ssl_page($url="",$postdata=""){
  $ch = curl_init();
  $cookie = 'cookie.txt';
  curl_setopt ($ch, CURLOPT_URL, $url); 
  curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
  curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
  curl_setopt ($ch, CURLOPT_TIMEOUT, 60); 
  curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); 
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
  curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); 
  curl_setopt ($ch, CURLOPT_REFERER, $url); 
  curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); 
  curl_setopt ($ch, CURLOPT_POST, 1); 
  $result = curl_exec ($ch);
  
  return $result;
  }
  
  function curl_get_page($url=""){
  
  $ch = curl_init();
  $cookie = 'cookie.txt';
  curl_setopt ($ch, CURLOPT_URL, $url); 
  curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
  curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
  curl_setopt ($ch, CURLOPT_TIMEOUT, 60); 
  curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); 
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
  curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie); 
  curl_setopt ($ch, CURLOPT_REFERER, $url); 
  
  $result = curl_exec ($ch);
  return $result;
  }
  </code></pre>
  
  <p>Pages</p>
  
  <pre><code>Pages - Login:
  
  if(isset($_POST['user-name'])) {
      //GET login page
      $url = "http://www.gryphinonline.ca/Login.aspx";
      $login_page = $this-&gt;curl_page($url);
  
      // get viewstate
      $regexViewstate = '/__VIEWSTATE\" value=\"(.*)\"/i';
      $regexEventVal  = '/__EVENTVALIDATION\" value=\"(.*)\"/i';
  
  $viewstate = $this-&gt;regexExtract($login_page,$regexViewstate,1);
  $eventval = $this-&gt;regexExtract($login_page, $regexEventVal,1);
  
  //Post to login page
  $postdata = '__VIEWSTATE='.rawurlencode($viewstate)
       .'&amp;txtLoginName='.$_POST['user-name']
       .'&amp;txtPassword='.$_POST['password']
       .'&amp;Start=Login+%2F+Ouverture+de+session';
  
  $this-&gt;curl_ssl_page($url,$postdata);
  header("Location:http://url-edited/submissions");
  }
  
  Pages - Submissions:
      $url = "http://www.gryphinonline.ca/Submissions.aspx";
  $submissions = $this-&gt;curl_get_page($url);
  
  $dom = new DOMDocument();
  @$dom-&gt;loadHTML($submissions);
  
  // scrape for data including viewstate
  $view = $dom-&gt;getElementById('dgrdSubmissions'); 
  if(!$view) header("Location://url-edited/login");
  
      $h_data = $dom-&gt;getElementsByTagName('div');
  $h_data = $h_data-&gt;item(0);
  
  if(isset($_POST['__EVENTTARGET'])){
      $postdata=array();
      foreach ($_POST as $key =&gt; $value) {
          $postdata[]=$key.'='.$value;
      }
      $postdata = implode('&amp;', $postdata);
  
      $this-&gt;curl_ssl_page($url,$postdata);
      header("Location:http://url-edited/policy");        
  }
  
  Pages - Policy:
      $url = "http://www.gryphinonline.ca/Policy.aspx";
  $policy = $this-&gt;curl_get_page($url);
  </code></pre>
  
  <p>All the HTTP requests and cookies are identical as far as I can tell.  Anyone have any idea what is going on here?  Is this possibly related to the site's problems with Firefox or am I misunderstanding something basic?</p>
  
  <p>I've been at this for a few days and any help would be appreciated.</p>