W login.php mam taki kod:
$cookieFile = $functions->random(10, 1); //funkcja do generowania losowej nazwy ciasteczka $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "http://strona.com/login.php"); curl_setopt($curl, CURLOPT_COOKIESESSION, true); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false ); curl_setopt($curl, CURLOPT_POSTFIELDS, "login=user&password=password"); curl_setopt($curl, CURLOPT_ENCODING, ""); curl_setopt($curl, CURLOPT_HEADER, 1); curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt($curl, CURLOPT_COOKIEJAR, $_SERVER["DOCUMENT_ROOT"].'/web/cookies/'.$cookieFile.'.txt'); curl_setopt($curl, CURLOPT_COOKIEFILE, $_SERVER["DOCUMENT_ROOT"].'/web/cookies/'.$cookieFile.'.txt'); $getCookie = curl_exec($curl); curl_close($curl);
Wszystko jest fajnie, plik z ciasteczkiem się tworzy:
# Netscape HTTP Cookie File # This file was generated by libcurl! Edit at your own risk. strona.com FALSE / FALSE 0 JSESSIONID 0F6BCB6D89F2DCD2B3B77AAA33AEA69A
I wszystko byłoby okej, gdyby w account.php, przy kodzie:
$cookie = $functions->get_cookie(); //pobiera nazwe pliku ciasteczka i ciasteczko z bazy danych (w login.php zapisane) $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "http://strona.com/account.php"); curl_setopt($curl, CURLOPT_COOKIE, $cookie["cookie"]); curl_setopt($curl, CURLOPT_COOKIESESSION, true); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false ); curl_setopt($curl, CURLOPT_ENCODING, ""); curl_setopt($curl, CURLOPT_COOKIEJAR, $_SERVER["DOCUMENT_ROOT"].'/web/cookies/'.$cookie["cookiefile"].'.txt'); curl_setopt($curl, CURLOPT_COOKIEFILE, $_SERVER["DOCUMENT_ROOT"].'/web/cookies/'.$cookie["cookiefile"].'.txt'); curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); $curlData = curl_exec ( $curl );
można było się zalogować. Ale nie można. Nie loguje się, a ciasteczko zmienia się na:
# Netscape HTTP Cookie File # This file was generated by libcurl! Edit at your own risk.
To są nagłówki, zwracane przy logowaniu w login.php (już z innej sesji ciasteczek). Przy account.php zwracany jest już tylko md5.
HTTP/1.1 302 Found Date: Wed, 15 Jan 2014 14:37:09 GMT Server: Apache/2.2.14 (Ubuntu) Set-Cookie: JSESSIONID=BDC17E49CAE16F876D22BE97C155E573; Path=/; HttpOnly Location: [url="http://strona.com/login.php;jsessionid=BDC17E49CAE16F876D22BE97C155E573"]http://strona.com/login.php;jsessionid=BDC...D22BE97C155E573[/url] Content-Language: en-US Vary: Accept-Encoding Content-Encoding: gzip Content-Length: 20 Connection: close Content-Type: text/html HTTP/1.1 200 OK Date: Wed, 15 Jan 2014 14:37:09 GMT Server: Apache/2.2.14 (Ubuntu) Content-Language: en-US Vary: Accept-Encoding Content-Encoding: gzip Content-Length: 21445 Connection: close Content-Type: text/html;charset=UTF-8
Tym bardziej to dziwne, przecież wszystko jest okej. Plik z ciasteczkiem jest odczytywany, ma dobry chmod - jest okej. W bazie wszystko dobrze zapisywane i odczytywane - debugowałem. Tylko czemu, u licha, nie chce korzystać już z tego ciasteczka w account.php?
Będę wdzięczny za każą podpowiedź, bo nie mam już sił. Nawet curl_error($curl); nic nie pokazuje...
Przy okazji, ten kod LOGUJE bez problemu:
$curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "http://strona.com/login.php"); curl_setopt($curl, CURLOPT_COOKIESESSION, true); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false ); curl_setopt($curl, CURLOPT_POSTFIELDS, "login=user&password=password"); curl_setopt($curl, CURLOPT_ENCODING, ""); curl_setopt($curl, CURLOPT_HEADER, 1); curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt($curl, CURLOPT_COOKIEJAR, $_SERVER["DOCUMENT_ROOT"].'/panel/cookies/cookie.txt'); curl_setopt($curl, CURLOPT_COOKIEFILE, $_SERVER["DOCUMENT_ROOT"].'/panel/cookies/cookie.txt'); $getCookie = curl_exec($curl);
ten kod NIE LOGUJE:
$cookieFile = $this->random(10, 1); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "http://strona.com/login.php"); curl_setopt($curl, CURLOPT_COOKIESESSION, true); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false ); curl_setopt($curl, CURLOPT_POSTFIELDS, "login=user&password=password"); curl_setopt($curl, CURLOPT_ENCODING, ""); curl_setopt($curl, CURLOPT_HEADER, 1); curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt($curl, CURLOPT_COOKIEJAR, $_SERVER["DOCUMENT_ROOT"].'/web/cookies/'.$cookieFile.'.txt'); curl_setopt($curl, CURLOPT_COOKIEFILE, $_SERVER["DOCUMENT_ROOT"].'/web/cookies/'.$cookieFile.'.txt'); $getCookie = curl_exec($curl);
Chociaż w drugim przypadku plik jest tworzony, ciasteczko jest zapisywane i odczytywane. Ale nie loguje.
WTF?!