问题
I have logged in to a website using url,username and password using curl.The main question is that i want to get the response back and store it in php script that the site has been logged in or not ?
Storing response i.e. logged in or login failed in the php script is the main question ? I have given the script below can u tell me what to add ?
Thanks in advance
<?php
$cookiefile = tempnam("/tmp", "cookies");
$login_url='**********';
$login_post_url='*********';
$username = "*****";
$password = "*****";
$agent="Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2 ";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$login_url);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec ($ch);
curl_close ($ch);
$postfields = array(
'j_username' => $username,
'j_password' => $password,
);
$reffer = $login_url;
$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,
http_build_query($postfields));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_REFERER, $reffer);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec ($ch);
echo $result;
curl_close ($ch);
unlink($cookiefile);
?>
回答1:
just check with strpos(...) or preg_match(...) if a certain pattern / string is in the response (saved in curl_exec($ch)).
after that, just save the name of the cookie you're using and you won't need to login afterwards again, when you're running this script again.
Let's say the text (part of the response) is Login successful, then you can tell:
$login_successful = strpos($response, "Login successful") !== FALSE;
or if you don't care about case sensitivity
$login_successful = strpos(strtolower($response), "login successful") !== FALSE;
hope that helps!
回答2:
Storing response i.e. logged in or login failed in the php script is the main question ?
This should be stored in a Boolean value on success, along with the accompanying HTTP code.
EDIT:
You can use this in the second cURL invocation when logging in.
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$loginSuccess = $httpCode >= 200 && $httpCode < 400;
This will accept redirections as well after login.
回答3:
If you refer to the HTTP/401 authorization and login via curl (instead of POST to a form), you will either get HTTP/401 again if failed, or HTTP/200 if you success.
If the "curl" is driven by a PHP script, I'll suggest you to initiate the HTTP request directly inside the PHP using, say, HTTP_Request from pear.
来源:https://stackoverflow.com/questions/9564857/storing-the-response-of-login-using-curl