Help for php cURL Login

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
iserge
Forum Newbie
Posts: 2
Joined: Wed Dec 19, 2012 4:45 pm

Help for php cURL Login

Post by iserge »

Now I am trying to login to my account using PHP and CURL, but still I am not sure why is not working.

The page I'm trying to login to is: http://lardi-trans.com/index.jsp After the execution of the script, I get header of page.
Help to find error.
Thanks in advance!

Code: Select all

<?php
$url = "http://lardi-trans.com/log/index.jsp";
$referer = "http://lardi-trans.com/index.jsp";
$user_agent = "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:17.0) Gecko/20100101 Firefox/17.0";
$post = "action=/log/login.jsp&log=MYLOGIN&passwd=MYPASSWORD&enter=enter&onlog=%D0%92%D0%BE%D0%B9%D1%82%D0%B8";
$header [] = "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$header [] = "Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3";
$header [] = "Accept-Encoding:gzip,deflate";
$header [] = "Accept-Language:en-US,en;q=0.5";
$user_cookie = "cookies.txt";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url ); 
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_ENCODING, "identity");
curl_setopt($ch, CURLOPT_COOKIEFILE, $user_cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $user_cookie);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$page = curl_exec($ch);
curl_close($ch);

echo $page;
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Help for php cURL Login

Post by Christopher »

Did you try:

Code: Select all

curl_setopt($ch, CURLOPT_HEADER, 0);
(#10850)
iserge
Forum Newbie
Posts: 2
Joined: Wed Dec 19, 2012 4:45 pm

Re: Help for php cURL Login

Post by iserge »

Yes. And i see blank page)
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Help for php cURL Login

Post by Eric! »

Your post string shouldn't contain the url query, just the fields. Are you sure you have all the fields including any hidden ones in the form? You could try using firefox and an extension like live http headers to view what is submitted during login.

Also, use this trick for debugging curl by putting the debug info stream into a file:

Code: Select all

$file = "debug.txt";
$fp=fopen($file,"w+");
curl_setopt($handle, CURLOPT_VERBOSE, true);
curl_setopt($handle, CURLOPT_STDERR,$fp);
That should give you a start.

Edit: Also you might need to set the referring url to the url that submits the form

Code: Select all

curl_setopt($handle, CURLOPT_REFERER, 'http://www.example.com/1');
Post Reply