Jump to content

curl login


ycc
 Share

Recommended Posts

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

 

Link to comment
Share on other sites

I apologize for unclear information. The simplest thing to detect a correct login is to visit (curl) this page.

http://www.zgpzw.com/bbs/pz/showpic.asp?number=4852&code=fb67aa6082363a6d

If you are not logged in it shows a log-in form. If you are logged in it shows pictures of Chinese ration tickets.

 

I am focusing on making the second code snippet above work. From what I can understand, the login cookie is correctly written to the server file cookie.txt:

 

# Netscape HTTP Cookie File# http://www.netscape.com/newsref/std/cookie_spec.html# This file was generated by libcurl! Edit at your own risk.www.zgpzw.com	FALSE	/bbs/	FALSE	1284803334	DvForum+8%2E3%5Fwww%2Ezgpzw%2Ecom	StatUserID=68184307618www.zgpzw.com	FALSE	/	FALSE	0	ASPSESSIONIDQQQCQADS	BKHLPBDCOLNHCMDMGGHAMLJO

 

Link to comment
Share on other sites

The program works now. Sorry for basic mistake causing this thread. I had the wrong page as "action-page" for the form. This works:

 

function curl_login($page, $options, $cookie_file, &$ch) {
// $page= 'http://www.zgpzw.com/bbs/login.asp?action=chk';
// $options= 'username=tsttst&password=tsttstpwd&CookieDate=2&userhidden=2';
// $cookie_file= 'cookie.txt';

// http://www.knowledgesutra.com/forums/topic/38162-automatic-login-using-curl/

// SET URL FOR THE POST FORM LOGIN
curl_setopt($ch, CURLOPT_URL, $page);

// 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');
curl_setopt ($ch, CURLOPT_POSTFIELDS, $options);

// IMITATE CLASSIC BROWSER'S BEHAVIOUR : HANDLE COOKIES
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie_file);

# 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);

}

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.