I worked with HttpFox and have compared the values (url and post data) with the ones my code has generated. They are completely the same, but I always see the message 'Login failed' on the webpage. I have no idea what could be the problem.
Code:
require 'domparser_1_5/simple_html_dom.php';
$username = "username";
$password = "password";
function login($url,$data){
$fp = fopen("cookie.txt", "w");
fclose($fp);
$login = curl_init();
curl_setopt($login, CURLOPT_URL, $url);
curl_setopt($login, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($login, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($login, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($login, CURLOPT_TIMEOUT, 40000);
curl_setopt($login, CURLOPT_RETURNTRANSFER, true);
curl_setopt($login, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($login, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($login, CURLOPT_POST, true);
curl_setopt($login, CURLOPT_POSTFIELDS, $data);
ob_start();
$result = curl_exec($login);
ob_end_clean();
curl_close($login);
unset($login);
return $result;
}
function generate_pass($user, $password, $token) {
if ($password) {
$enc_pass = md5($password);
$pass = $user.':'.$enc_pass.':'.$token;
return md5($pass);
}
}
function get_token() {
$html = file_get_html('https://www.foo.com/');
$token = "";
foreach($html->find('input') as $element) {
if($element->name == "token") {
$token = $element->value;
}
}
if (!$token) {
die('No token found');
}
return $token;
}
$token = get_token();
echo login("https://www.foo.com/login/start.html", "user=".$username."&password=".$password."&submit=Anmelden&logintype=login&pid=4%2C93%2C1828&pass=".generate_pass($username, $password, $token)."&redirect_url=login%2Fstart.html&tx_felogin_pi1%5Bnoredirect%5D=0&token=".$token."");
Authentication fails due to the mismatch between token
and cookie(s) passed.
When you load the page to retrieve the token with get_token()
the server is sending you a cookie that you are not saving.
Later when you try to login the server expects to receive same cookie it sent you when you got the token
. But you're not sending it.
I suggest you to the rewrite get_token()
using curl and storing cookies in cookie.txt
. This will let you pass them later when you'll call login()
Like this:
function get_token() {
$url = 'https://www.foo.com/';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($curl, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($curl, CURLOPT_TIMEOUT, 40000);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($curl);
curl_close($curl);
unset($curl);
$html = str_get_html( $result );
$token = "";
foreach($html->find('input') as $element) {
if($element->name == "token") {
$token = $element->value;
}
}
if (!$token) {
die('No token found');
}
return $token;
}
Important:
Remove
$fp = fopen("cookie.txt", "w");
fclose($fp);
From login()
as that truncates to zero bytes the cookie.txt
file, and you don't want to delete the cookie just retrieved.
Note that curl_exec()
creates the speciefied cookie file if it doesn't exist.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With