Próbuję przebrnąć przez logowanie ale nie daję rady. Używając tego kodu z kilkoma zmiennymi zamienionymi udało się zalogować na demotywatory.pl bez problemu.
Tutaj sprawa wygląda tak:
1) Najpierw udaję się do http://web.copart.com/cgi-bin/counter/login.dtw/disp, gdzie znajduje się formularz
2) Wysyłam POSTem do http://web.copart.com/cgi-bin/counter/login.dtw/disp2 login i hasło ( Listing 1 - pokazuje poprawny proces logowania tak jak to robię przez przeglądarkę )
3) Po zalogowaniu (jeśli ciasteczko jest ustawione - i właśnie tu jest problem) udaję się do: http://web.copart.com/cgi-bin/counter/index.dtw/disp
Niby banał, ale jednak te przekierowania trochę mieszają.
<?php /********************************* **Set up your variables** **********************************/ /* Create a temporary file to store cookies. This should work on most systems and is more flexible than specifying path explicitly */ $login_url='http://web.copart.com/cgi-bin/counter/login.dtw/disp'; /* The page that displays the login form. */ $login_post_url='http://web.copart.com/cgi-bin/counter/login.dtw/disp2'; /* The "action" value of the login form. This is not always equal to $login_url. */ $loginString = 'userid=95042&password=ducati'; $userid = "95042"; $password = "ducati"; $agent="Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"; /********************************* **Load the "login" page and get some cookies** **********************************/ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$login_url); /* The URL of the page to retrieve */ curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); $referer = 'http://www.google.pl/search?q=counter+man'; curl_setopt($ch, CURLOPT_REFERER, $referer ); /* Disguise self as a browser app. Some servers might need a different value here. Some servers might try to check if the page is visited by a real human being using this value. */ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); /* Don't output the results - return them as a string instead */ curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); /* Follow redirects. This isn't actually necessary here :P */ curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile); /* Read cookies from this file */ curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile); /* Save cookies to the same file too */ $result = curl_exec ($ch); /* Perform the query, retrieve the page. */ curl_close ($ch); /* The fields of the login form. These will probably be different for every particular page. */ 'userid' => $userid, 'password' => $password ); $referer = 'http://web.copart.com/cgi-bin/counter/login.dtw/disp'; /* If the server checks the referer we need to spoof it */ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $login_post_url); curl_setopt($ch, CURLOPT_USERAGENT, $agent); curl_setopt($ch, CURLOPT_POST, 1); //curl_setopt ( $ch, CURLOPT_POSTFIELDS, $loginString ); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields)); /* http_build_query() will properly escape the fields and build a query string. */ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_MAXREDIRS, 5); /* Follow redirects. This is probably necessary here. */ curl_setopt($ch, CURLOPT_REFERER, $referer); /* spoof the HTTP referer */ curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile); /* Note that this is the same file as before */ $info = curl_getinfo($ch); $result = curl_exec ($ch); /* Now we've got the contents of the page you see after logging in saved in $result */ curl_close ($ch); /****************************************** **All done. Kill the cookie file once it's not needed anymore** *******************************************/ //unlink($cookiefile); ?>
Listing 1
http://web.copart.com/cgi-bin/counter/login.dtw/disp2
POST /cgi-bin/counter/login.dtw/disp2 HTTP/1.1
Host: web.copart.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10 ( .NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: pl,en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-2,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://web.copart.com/cgi-bin/counter/login.dtw/disp
Cookie: __utma=221776199.87643467.1281882269.1286569745.1286733511.8; __utmz=221776199.1286537459.5.2.utmcsr=google|utmccn=(organic)|utmcmd=organic|ut
mctr=copart%20login; COUNTLOGIN=; COPARTURL=
Content-Type: application/x-www-form-urlencoded
Content-Length: 33
URL=&userid=95042&password=ducati
HTTP/1.1 200 OK
Date: Sun, 17 Oct 2010 21:07:52 GMT
Server: Apache/2.0.63 (iSeries)
Content-Length: 471
Keep-Alive: timeout=120, max=99
Connection: Keep-Alive
Content-Type: text/html; charset=ISO-8859-1
----------------------------------------------------------
http://web.copart.com/cgi-bin/counter/index.dtw/disp
GET /cgi-bin/counter/index.dtw/disp HTTP/1.1
Host: web.copart.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10 ( .NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: pl,en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-2,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://web.copart.com/cgi-bin/counter/login.dtw/disp2
Cookie: __utma=221776199.87643467.1281882269.1286569745.1286733511.8; __utmz=221776199.1286537459.5.2.utmcsr=google|utmccn=(organic)|utmcmd=organic|ut
mctr=copart%20login; COUNTLOGIN=95042:1568740835; COPARTURL=
HTTP/1.1 200 OK
Date: Sun, 17 Oct 2010 21:07:53 GMT
Server: Apache/2.0.63 (iSeries)
Content-Length: 4083
Keep-Alive: timeout=120, max=97
Connection: Keep-Alive
Content-Type: text/html; charset=ISO-8859-1
">
http://web.copart.com/cgi-bin/counter/login.dtw/disp2
POST /cgi-bin/counter/login.dtw/disp2 HTTP/1.1
Host: web.copart.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10 ( .NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: pl,en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-2,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://web.copart.com/cgi-bin/counter/login.dtw/disp
Cookie: __utma=221776199.87643467.1281882269.1286569745.1286733511.8; __utmz=221776199.1286537459.5.2.utmcsr=google|utmccn=(organic)|utmcmd=organic|ut
mctr=copart%20login; COUNTLOGIN=; COPARTURL=
Content-Type: application/x-www-form-urlencoded
Content-Length: 33
URL=&userid=95042&password=ducati
HTTP/1.1 200 OK
Date: Sun, 17 Oct 2010 21:07:52 GMT
Server: Apache/2.0.63 (iSeries)
Content-Length: 471
Keep-Alive: timeout=120, max=99
Connection: Keep-Alive
Content-Type: text/html; charset=ISO-8859-1
----------------------------------------------------------
http://web.copart.com/cgi-bin/counter/index.dtw/disp
GET /cgi-bin/counter/index.dtw/disp HTTP/1.1
Host: web.copart.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10 ( .NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: pl,en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-2,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://web.copart.com/cgi-bin/counter/login.dtw/disp2
Cookie: __utma=221776199.87643467.1281882269.1286569745.1286733511.8; __utmz=221776199.1286537459.5.2.utmcsr=google|utmccn=(organic)|utmcmd=organic|ut
mctr=copart%20login; COUNTLOGIN=95042:1568740835; COPARTURL=
HTTP/1.1 200 OK
Date: Sun, 17 Oct 2010 21:07:53 GMT
Server: Apache/2.0.63 (iSeries)
Content-Length: 4083
Keep-Alive: timeout=120, max=97
Connection: Keep-Alive
Content-Type: text/html; charset=ISO-8859-1