phpzend-frameworkhttpcookieszend-http-client

Zend_Http_Client not storing cookies


I am using the following code to access a page protected by username/password login.

  //Fetch homepage
  $client = new Zend_Http_Client();
  $client->setCookieJar();
  $client->setUri('https://www.yourloungelearning.co.uk/crew_trainer/login.php');
  //$client->setParameterPost( 'username', $_SESSION['username'] );
  //$client->setParameterPost( 'password', $_SESSION['password'] );
  $client->setParameterPost( 'username', '#####' );
  $client->setParameterPost( 'password', '#####' );
  $response = $client->request('POST');

  // Now we're logged in, get private area! 
  $client->setUri('https://www.yourloungelearning.co.uk/crew_trainer/index.php');
  $response = $client->request('GET');

  echo $response->getBody();

The echo at the end always returns the login screen again (suggesting an unsuccessful login). This is copied almost exactly from the Zend docs. Can anyone see what I'm doing wrong?


Solution

  • A quick couple of tests on that page reveal that you do indeed need to include the submit button value (the actual value is irrelevant, it just has to be present) as part of the POST data for the login to be processed.

    $client->setParameterPost('submit', 'Login');
    

    You would have noticed this if you checked the response from the login attempt.