Hi,
I am very excited, making my first post in this very qualified, highly active forum.
I try to log in using curl. I do not know so much about the problem, but the login-cookie should be written to the server disk somehow, I think.
I have tried the following two snippets. (The original source of the snippets and the login credentials for the site I am trying to log in to are indicated in the quoted code.)
<?php
// http://www.zgpzw.com/bbs/pz/liangpic.asp
// http://www.zgpzw.com/bbs/pz/showpic.asp?number=4852&code=fb67aa6082363a6d
// tsttst, tsttstpwd
/*
cookiejar.php
http://curl.haxx.se/libcurl/php/examples/cookiejar.html
This script is an example of using curl in php to log into on one page and
then get another page passing all cookies from the first page along with you.
If this script was a bit more advanced it might trick the server into
thinking its netscape and even pass a fake referer, yo look like it surfed
from a local page.
*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookieFileName");
curl_setopt($ch, CURLOPT_URL,"http://www.zgpzw.com/bbs/pz/liangpic.asp");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'username=tsttst&password=tsttstpwd&CookieDate=2&userhidden=2');
ob_start(); // prevent any output
curl_exec ($ch); // execute the curl command
ob_end_clean(); // stop preventing output
curl_close ($ch);
unset($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookieFileName");
curl_setopt($ch, CURLOPT_URL,"http://www.zgpzw.com/bbs/pz/showpic.asp?number=4852&code=fb67aa6082363a6d");
$buf2 = curl_exec ($ch);
curl_close ($ch);
// echo "<PRE>".htmlentities($buf2);
echo $buf2;
?>
<?php
// http://www.knowledgesutra.com/forums/topic/38162-automatic-login-using-curl/
// INIT CURL
$ch = curl_init();
// SET URL FOR THE POST FORM LOGIN
curl_setopt($ch, CURLOPT_URL, 'http://www.zgpzw.com/bbs/pz/liangpic.asp');
// ENABLE HTTP POST
curl_setopt ($ch, CURLOPT_POST, 1);
// SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'username=tsttst&password=tsttstpwd&CookieDate=2&userhidden=2');
// IMITATE CLASSIC BROWSER'S BEHAVIOUR : HANDLE COOKIES
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
# Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
# not to print out the results of its query.
# Instead, it will return the results as a string return value
# from curl_exec() instead of the usual true/false.
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
// EXECUTE 1st REQUEST (FORM LOGIN)
$store = curl_exec ($ch);
// SET FILE TO DOWNLOAD
curl_setopt($ch, CURLOPT_URL, 'http://www.zgpzw.com/bbs/pz/showpic.asp?number=4852&code=fb67aa6082363a6d');
// EXECUTE 2nd REQUEST (FILE DOWNLOAD)
$content = curl_exec ($ch);
// CLOSE CURL
curl_close ($ch);
echo $content;
?>
I am trying to help a friend who is a collector of Chinese ration tickets.
The login page I am trying to log in to is this:
http://www.zgpzw.com/bbs/pz/liangpic.asp
The log in form looks like this
<form method="post" action="../login.asp?action=chk" onSubmit="return checkLogin(this);">
<tr>
<td width="60" height="27">???:</td>
<td><input type="text" name="username" id="username" tabindex="1" style="width:120px;height:22px;BORDER-RIGHT: #666666 1px solid; BORDER-TOP: #666666 1px solid; FONT-SIZE: 9pt; BORDER-LEFT: #666666 1px solid; BORDER-BOTTOM: #666666 1px solid"/></td>
</tr>
<tr>
<td width="60" height="27">??:</td>
<td><input type="password" name="password" id="pwd1" tabindex="2" style="width:120px;height:22px;BORDER-RIGHT: #666666 1px solid; BORDER-TOP: #666666 1px solid; FONT-SIZE: 9pt; BORDER-LEFT: #666666 1px solid; BORDER-BOTTOM: #666666 1px solid"/></td>
</tr>
<tr>
<td> </td>
<td height="30"><input type="hidden" name="comeurl" value="/bbs/pz/showpic.asp?number=4852&code=fb67aa6082363a6d"/>
<input name="CookieDate" type="hidden" value="2">
<input name="userhidden" type="hidden" value="2">
<input type=image alt=??
src="image/login.gif" name=image3 width="60" height="19"></td>
</tr>
</form>
To check if my login is successful I try to curl down the following page:
http://www.zgpzw.com/bbs/pz/showpic.asp?number=44213&code=f278c2ae7f53ba02
The proof that one would be correctly logged in is that the page would show pictures of Chinese ration tickets. (Not just text)
If I go to the login page and provide the login data manually (putting logincookie not on server, but on my workstation) works, of course.
http://www.zgpzw.com/bbs/pz/liangpic.asp
The site is in Chinese, but I do not think it makes so much difference. If my question is unclear, please ask for clarifications.
The two login scripts I am trying are not successful. (Or rather - I have failed in applying them.) What should I do to log in to this site using php-curl?
Thanks.
ycc