PHP cURL can't login with cURL PHP cURL can't login with cURL curl curl

PHP cURL can't login with cURL


Try this simple function to do login.

send parameters as an array to CURLOPT_POSTFIELDS

/*** If login is required use this function* to have session/cookie.*/function logIn($loginActionUrl,$parameters){        curl_setopt ($this->curl, CURLOPT_URL,$loginActionUrl);         curl_setopt ($this->curl, CURLOPT_POST, 1);         curl_setopt ($this->curl, CURLOPT_POSTFIELDS, $parameters);         curl_setopt ($this->curl, CURLOPT_COOKIEJAR, realpath('cookie.txt')); // cookie.txt should be in same directoy, where calling script is         curl_setopt ($this->curl, CURLOPT_COOKIEFILE, realpath('cookie.txt'));        curl_setopt ($this->curl, CURLOPT_FOLLOWLOCATION, 1);        curl_setopt ($this->curl, CURLOPT_RETURNTRANSFER, 1);        curl_setopt ($this->curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i586; de; rv:5.0) Gecko/20100101 Firefox/5.0');                    curl_setopt ($this->curl, CURLOPT_REFERER, 'http://www.google.com');    // set referer        curl_setopt ($this->curl, CURLOPT_SSL_VERIFYPEER, FALSE);// ssl certificate        curl_setopt ($this->curl, CURLOPT_SSL_VERIFYHOST, 2);        $result['EXE'] = curl_exec($this->curl);        $result['INF'] = curl_getinfo($this->curl);        $result['ERR'] = curl_error($this->curl);        return $result;                 }


On ImpressPages I've done it this way:

//initial request with login data$ch = curl_init();curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/login.php');curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36');curl_setopt($ch, CURLOPT_POST, true);curl_setopt($ch, CURLOPT_POSTFIELDS, "username=XXXXX&password=XXXXX");curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_COOKIESESSION, true);curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie-name');  //could be empty, but cause problems on some hostscurl_setopt($ch, CURLOPT_COOKIEFILE, '/var/www/ip4.x/file/tmp');  //could be empty, but cause problems on some hosts$answer = curl_exec($ch);if (curl_error($ch)) {    echo curl_error($ch);}//another request preserving the sessioncurl_setopt($ch, CURLOPT_URL, 'http://www.example.com/profile');curl_setopt($ch, CURLOPT_POST, false);curl_setopt($ch, CURLOPT_POSTFIELDS, "");$answer = curl_exec($ch);if (curl_error($ch)) {    echo curl_error($ch);}