Pomoże ktoś z uzyskaniem ciasteczka po podaniu logina i hasła? Login i hasło podane tutaj jest publicznie dostępne na stronie gościa który sprowadza samochody ze stanów.
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ą.


  1. <?php
  2.  
  3. /*********************************
  4. **Set up your variables**
  5. **********************************/
  6. $cookiefile = tempnam("/tmp", "cookies");
  7. /* Create a temporary file to store cookies.
  8.   This should work on most systems and is more
  9.   flexible than specifying path explicitly */
  10.  
  11.  
  12.  
  13.  
  14. $login_url='http://web.copart.com/cgi-bin/counter/login.dtw/disp';
  15. /* The page that displays the login form. */
  16.  
  17. $login_post_url='http://web.copart.com/cgi-bin/counter/login.dtw/disp2';
  18. /* The "action" value of the login form. This is not always
  19.   equal to $login_url. */
  20.  
  21.  
  22. $loginString = 'userid=95042&password=ducati';
  23.  
  24.  
  25.  
  26. $userid = "95042";
  27. $password = "ducati";
  28.  
  29. $agent="Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
  30.  
  31. /*********************************
  32. **Load the "login" page and get some cookies**
  33. **********************************/
  34.  
  35. $ch = curl_init();
  36. curl_setopt($ch, CURLOPT_URL,$login_url);
  37. /* The URL of the page to retrieve */
  38.  
  39. curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  40.  
  41. $referer = 'http://www.google.pl/search?q=counter+man';
  42.  
  43. curl_setopt($ch, CURLOPT_REFERER, $referer );
  44. /* Disguise self as a browser app. Some servers
  45. might need a different value here. Some servers
  46. might try to check if the page is visited by a
  47. real human being using this value. */
  48.  
  49. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  50. /* Don't output the results -
  51.   return them as a string instead */
  52.  
  53. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  54. /* Follow redirects.
  55. This isn't actually necessary here :P */
  56.  
  57. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
  58. /* Read cookies from this file */
  59.  
  60. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
  61. /* Save cookies to the same file too */
  62.  
  63. $result = curl_exec ($ch);
  64. /* Perform the query, retrieve the page. */
  65.  
  66. echo $result;
  67.  
  68. curl_close ($ch);
  69.  
  70.  
  71.  
  72. /* The fields of the login form. These will probably be
  73.   different for every particular page. */
  74. $postfields = array(
  75. 'userid' => $userid,
  76. 'password' => $password
  77. );
  78.  
  79. $referer = 'http://web.copart.com/cgi-bin/counter/login.dtw/disp';
  80. /* If the server checks the referer we need to spoof it */
  81.  
  82. $ch = curl_init();
  83.  
  84.  
  85. curl_setopt($ch, CURLOPT_URL, $login_post_url);
  86. curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  87. curl_setopt($ch, CURLOPT_POST, 1);
  88.  
  89. //curl_setopt ( $ch, CURLOPT_POSTFIELDS, $loginString );
  90.  
  91. curl_setopt($ch, CURLOPT_POSTFIELDS,
  92. http_build_query($postfields));
  93.  
  94. /* http_build_query() will properly escape the fields and
  95.   build a query string. */
  96.  
  97. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  98. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  99. curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
  100. /* Follow redirects. This is probably necessary here. */
  101. curl_setopt($ch, CURLOPT_REFERER, $referer);
  102. /* spoof the HTTP referer */
  103.  
  104. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
  105. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
  106. /* Note that this is the same file as before */
  107.  
  108. $info = curl_getinfo($ch);
  109. print_r( $info );
  110.  
  111. $result = curl_exec ($ch);
  112. /* Now we've got the contents of the page you see after
  113.   logging in saved in $result */
  114.  
  115. curl_close ($ch);
  116.  
  117. echo $result;
  118.  
  119.  
  120. /******************************************
  121. **All done. Kill the cookie file once it's not needed anymore**
  122. *******************************************/
  123. //unlink($cookiefile);
  124.  
  125. ?>



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