Pregunta

i am creating a bot in PHP that can log in into a site and stay logged. But it's not working. i am using this code. but it's not working.

<?php  
$curl = curl_init("http://www.site.com/login.php");
curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_POSTFIELDS, "usr=username&pswd=password");
curl_exec($curl);
echo curl_error($curl);
curl_close($curl);
?>

i have also tried this but it's also not working

<?php
$fields = array(
    "login"=>"username",
    "pass"=>"passowrd"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.site.com/login.php");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
?>
¿Fue útil?

Solución

You'll need to use these to save their cookie:

CURLOPT_COOKIEFILE      => 'data/cookies/x.tmp',
CURLOPT_COOKIEJAR       => 'data/cookies/x.tmp',

Also, some times require a cookie to be set before the login is sent. This is often an issue. Often also worth setting:

CURLOPT_SSL_VERIFYHOST  => 0,
CURLOPT_SSL_VERIFYPEER  => 0,
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top