开发者

How to login to a website using curl and PHP?

开发者 https://www.devze.com 2023-04-02 02:27 出处:网络
I was trying to login to a site remotely. I have successfully done that and the landing page opens properly and I can extract all info from it. The problem is, when I click any link, the website logs

I was trying to login to a site remotely. I have successfully done that and the landing page opens properly and I can extract all info from it. The problem is, when I click any link, the website logs me out! Earlier the link was relative to my domain, now I've changed it to the website domain but still the problem persists. I'm attaching the code for you to see how I've sent cookies.

I have executed another curl_exec to another URL within the site, but the website logs me off!

<html> 
<head> 
<base href="http://example.com/" /> 
<body> 
<?php 
$username = $_POST["uname"]; 
$password = $_POST["pass"]; 
$fields = "username=" . $username . "&password=" . $password . "&Login.x=61&am开发者_如何转开发p;Login.y=15"; 
$ch=curl_init(); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 

curl_setopt($ch, CURLOPT_URL, 'http://www.SOMESITE.net/amizone/default.aspx'); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_COOKIEJAR, "ASPSESSIONIDSQADDRBQ=OCALKIGABGLPCJLKMGIKJLLF"); //I got this exact cookiename from LiveHTTPheaders (Firefox extension)
curl_setopt($ch, CURLOPT_COOKIEFILE, "ASPSESSIONIDSQADDRBQ=OCALKIGABGLPCJLKMGIKJLLF"); 
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1000 ); 
curl_setopt($ch, CURLOPT_TIMEOUT, 10000 ); 
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.8) Gecko/20061025 Firefox/1.5.0.8' ); 
curl_setopt($ch, CURLOPT_AUTOREFERER, 1 ); 
curl_setopt ($ch, CURLOPT_REFERER, "http://www.example.com/"); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0 ); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0 ); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); 
ob_start(); //the issue with the script remains with/without ob_start/ob_end_clean
$raw_data=curl_exec($ch); 
curl_setopt($ch, CURLOPT_POST, 0); //at this point im trying to get another page using curl. since i will not be using post i have set it to 0.
curl_setopt($ch, CURLOPT_URL, 'http:/site.net/amizone/WebForms/TimeTable/StudentTimeTableForWeek.aspx'); 
$ttable = curl_exec($ch); 
ob_end_clean(); 
echo $ttable; 
//curl_close($ch); 
//$raw_data = preg_replace('/\s(1,)/',' ',$raw_data); //clean it up 
//echo $raw_data; 
//$raw_data = stripslashes($raw_data); 
//$raw_data = explode("<strong>",$raw_data); 
//2 3 and 4 
//echo htmlentities($raw_data[4]); 
?> 
</body> 
</html> 

Note: I have taken the cookie name from 'LiveHttpHeaders', an extension for Firefox. What am I missing?


What are you doing with the cookiejar and cookiefile? Those are not for setting cookies, and a session variable shouldn't be hardcoded, but be obtained from the first request to the ASP web server.

You want to set the cookiejar and cookiefile to a value as per the documentation:

CURLOPT_COOKIEFILE The name of the file containing the cookie data. The cookie file can be in Netscape format, or just plain HTTP-style headers dumped into a file.
CURLOPT_COOKIEJAR The name of a file to save all internal cookies to when the handle is closed, e.g. after a call to curl_close.

So, no cookie data, but a path to a file. This way the session cookie (and other cookies) will be stored and used in successive requests.

0

精彩评论

暂无评论...
验证码 换一张
取 消