Hi,

I am working with normal curl login function but it doesn't work for this particular site, please see this login page and suggest any solution for me.

http://nbk2.autodata.no/default.aspx?11=4&10=0006117171746465004012&14=12&12=1003

For login on this site, you at begin must parse __VIEWSTATE and __PREVIOUSPAGE variables a then send post request by curl like this:

__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwUJOTEyNTM2NDY2ZGReJw3PG62XkgM7gYqdrdP%2B0zHxDQ%3D%3D&username=test&password=test&login=Send&__PREVIOUSPAGE=oL0kSGhtXbLqdHQg0uFgqg2

And don't forget about cookies!

Hi ,

I am attaching my code along with this, I also placed the values in the code with respect to your suggestion in the previous post.
The issue is we can login to many other sites with this login curl function. But it doesn't work for this particular site.
I just placed a dummy text in the place of username and password here.
here is the php code:

    $baseurl = "http://www.norskbildelkatalog.no";
    $loginurl = "http://www.norskbildelkatalog.no";
    $userName = "test";             //value of username
    $userPwd = "test";                              //value of password field       
    $loginButton = "";                                      //value of login button
    $userfieldname = "ctl00$tbUserName";                                //name of the field username
    $passfieldname = "ctl00$tbPassword";                                //name of the field password
    $buttonName = "ctl00$btnLogin";                                         //name of the submit button
    //$spiderId = $LoginDetails['idSpiders'];                                           //end values
    $link = "VW";                                                           
    $cookie_file_path = "cookies.txt"; 
    $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7";
    $reffer = "http://www.norskbildelkatalog.no";


$result_final = curlLogin($loginurl,$userName,$userPwd,$loginButton,$userfieldname,$passfieldname,$buttonName,$cookie_file_path,$agent);
            print_r($result_final);



function curlLogin($loginurl,$userName="",$userPwd="",$loginButton="",$userfieldname="",$passfieldname="",$buttonName="",$cookie_file_path="",$agent=""){
                if($passfieldname!="") {                                 //for sites that have a password field
                        $password = '&' .$passfieldname.'=';
                    $POSTFIELDS = $userfieldname.'='. $userName . $password. $userPwd . '&'.$buttonName.'='. $loginButton.'__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=wEPDwULLTE2OTYxMTQyOTJkZKnDWEWkr2o2KzEZL/UtwkVYytbh&__EVENTVALIDATION='."wEWBAKCiZ7KBAL06sf/AQK/0oqcCQLnrI+ZD94ytl4m3yVK2AjSmUM/lJpk24m3".'&__PREVIOUSPAGE=oL0kSGhtXbLqdHQg0uFgqg2';
                }
                //$POSTFIELDS = '__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwUJOTEyNTM2NDY2ZGReJw3PG62XkgM7gYqdrdP%2B0zHxDQ%3D%3D&username=vwx7a&password=smwhe&login=Send&__PREVIOUSPAGE=oL0kSGhtXbLqdHQg0uFgqg2'; 
                //$ch = curl_init(); 
                $ch = $this->ch;
                curl_setopt($ch, CURLOPT_URL,$loginurl);
                curl_setopt($ch, CURLOPT_USERAGENT, $agent);
                curl_setopt($ch, CURLOPT_POST, 1); 
                curl_setopt($ch, CURLOPT_POSTFIELDS,$POSTFIELDS); 
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                curl_setopt($ch, CURLOPT_REFERER, $reffer);
                curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
                curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
                $result = curl_exec ($ch);
                return $result;
                curl_close ($ch); 

    }

No!
You must parse __VIEWSTATE and __PREVIOUSPAGE variables using regular expressions because they will change in next time.

I checked the __VIEWSTATE hidden value on different times but seems same on different submission.I think, some times the issue may due to js function while submitting.

Okay. But you must save cookies from this page before sending post query.

Hi ,

You can also see my test page and its output in the given url please see this too
And I also placed the cookie variable in code please see. and let me know if any change is needed in the code with sample code

I am attaching my code along with this, I also placed the values in the code with respect to your suggestion in the previous post.
The issue is we can login to many other sites with this login curl function. But it doesn't work for this particular site.

I think the issue is due to the site create an encrepted url for each login, and it is not available on the curl response .


http://autobutler.no/test123.php

here is the php code:

<?php

$value = date("dmhis");
setcookie("Cookie", $value);

$baseurl = "http://www.norskbildelkatalog.no";
$loginurl = "http://www.norskbildelkatalog.no";
$userName = "vwx7a"; //value of username
$userPwd = "smwhe"; //value of password field
$loginButton = ""; //value of login button
$userfieldname = "ctl00$tbUserName"; //name of the field username
$passfieldname = "ctl00$tbPassword"; //name of the field password
$buttonName = "ctl00$btnLogin"; //name of the submit button
//$spiderId = $LoginDetails; //end values
$link = "VW";
$cookie_file_path = "cookies.txt";
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7";
$reffer = "http://www.norskbildelkatalog.no";


$result_final = curlLogin($loginurl,$userName,$userPwd,$loginButton,$userfieldname,$passfieldname,$buttonName,$cookie_file_path,$agent);
print_r($result_final);

function curlLogin($loginurl,$userName="",$userPwd="",$loginButton="",$userfieldname="",$passfieldname="",$buttonName="",$cookie_file_path="",$agent=""){
if($passfieldname!="") { //for sites that have a password field
$password = '&' .$passfieldname.'=';
$POSTFIELDS = $userfieldname.'='. $userName . $password. $userPwd . '&'.$buttonName.'='. $loginButton.'__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=wEPDwULLTE2OTYxMTQyOTJkZKnDWEWkr2o2KzEZL/UtwkVYytbh&__EVENTVALIDATION='."wEWBAKCiZ7KBAL06sf/AQK/0oqcCQLnrI+ZD94ytl4m3yVK2AjSmUM/lJpk24m3".'&__PREVIOUSPAGE=oL0kSGhtXbLqdHQg0uFgqg2';
}
//$POSTFIELDS = '__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwUJOTEyNTM2NDY2ZGReJw3PG62XkgM7gYqdrdP%2B0zHxDQ%3D%3D&username=vwx7a&password=smwhe&login=Send&__PREVIOUSPAGE=oL0kSGhtXbLqdHQg0uFgqg2';
//$ch = curl_init();
$ch = $this->ch;
curl_setopt($ch, CURLOPT_URL,$loginurl);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$POSTFIELDS);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_REFERER, $reffer);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
$result = curl_exec ($ch);
return $result;
curl_close ($ch);

}
?>

<?php
$url="http://nbk2.autodata.no/default.aspx?11=4&14=12&10=0091771271436042004012&12=1003";
 $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
        curl_setopt($ch, CURLOPT_FAILONERROR, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
        curl_setopt($ch, CURLOPT_TIMEOUT, 20); // times out after 4s
        curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8');
curl_setopt($ch, CURLOPT_COOKIEJAR, './cook.txt'); 
curl_setopt($ch, CURLOPT_COOKIEFILE, './cook.txt'); 
		$html = curl_exec($ch);
echo $html;

$ch = curl_init('http://nbk2.autodata.no/default.aspx?11=4&14=12&10=0091771271436042004012&12=1003&204=false');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, '__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwUJOTEyNTM2NDY2ZGReJw3PG62XkgM7gYqdrdP%2B0zHxDQ%3D%3D&username=admin&password=passw&login=Send&__PREVIOUSPAGE=oL0kSGhtXbLqdHQg0uFgqg2');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5');
	

    curl_setopt($ch, CURLOPT_COOKIEJAR, './cook.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, './cook.txt');
    $res = curl_exec($ch);
	echo $res;
?>

Did you check this code with the provided username and password.? it doesn't work here now.I checked it with this username and password. user: vwx7a
Pass : smwhe
I think the dynamically generated url makes the issue in login section...

With your log/pass it doesn't work in browser.

<?php
$url="http://www.norskbildelkatalog.no/default.aspx?cat=2&Id=18";
 $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
        curl_setopt($ch, CURLOPT_FAILONERROR, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
        curl_setopt($ch, CURLOPT_TIMEOUT, 20); // times out after 4s
        curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8');
curl_setopt($ch, CURLOPT_COOKIEJAR, './cook.txt'); 
curl_setopt($ch, CURLOPT_COOKIEFILE, './cook.txt'); 
		$html = curl_exec($ch);


$ch = curl_init('http://www.norskbildelkatalog.no/default.aspx?cat=2&Id=18');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, '__VIEWSTATE=%2FwEPDwULLTE2OTYxMTQyOTJkZKnDWEWkr2o2KzEZL%2FUtwkVYytbh&__EVENTVALIDATION=%2FwEWBAKCiZ7KBAL06sf%2FAQK%2F0oqcCQLnrI%2BZD94ytl4m3yVK2AjSmUM%2FlJpk24m3&ctl00%24tbUserName=vwx7a&ctl00%24tbPassword=smwhe&ctl00%24btnLogin=');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5');
	

    curl_setopt($ch, CURLOPT_COOKIEJAR, './cook.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, './cook.txt');
    $res = curl_exec($ch);
	echo $res;
?>

Thank you very much , my code works fine now.
:)

Hi,

i am facing an issue in the curl search method, i implemented the curl login with your help but in the case of search page
i tried to implement the same method but it does not return the search result page .Here I am placing the home url and the search
credentials but it doesn't work please help
this is my code:

ob_start();

    echo $searchRes =  curlLogin(); 

function curlLogin(){

$baseurl = "http://www.norskbildelkatalog.no/default.aspx?cat=2&Id=18";
        $loginurl = "http://www.norskbildelkatalog.no/default.aspx?cat=2&Id=18";
//      $userName = "vwx7a";                //value of username
//      $userPwd = "smwhe";                             //value of password field       
//      $loginButton = "";                                      //value of login button
//      $userfieldname = "ctl00$tbUserName";                                //name of the field username
//      $passfieldname = "ctl00$tbPassword";                                //name of the field password
//      $buttonName = "ctl00$btnLogin";                                         //name of the submit button
        //$spiderId = $LoginDetails['idSpiders'];                                           //end values
        $link = "VW";                                                           
        $cookie_file_path = "/cook.txt"; 
        $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7";
        $reffer = "http://www.norskbildelkatalog.no/default.aspx?cat=2&Id=18";
            //  if($passfieldname!="") {                                 //for sites that have a password field
                        $password = '&' .$passfieldname.'=';
                    $POSTFIELDS = 'tp_articlesearch_pnl_search_vehicle_criterion=1&tp_articlesearch%24input_search_single=AA52844&tp_articlesearch%24btn_search_single=tp_articlesearch_btn_search_single&__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwUJOTEyNTM2NDY2ZGReJw3PG62XkgM7gYqdrdP%2B0zHxDQ%3D%3D&username=vwx7a&password=smwhe&login=Send&__PREVIOUSPAGE=oL0kSGhtXbLqdHQg0uFgqg2';
                //}
                //$POSTFIELDS = '__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwUJOTEyNTM2NDY2ZGReJw3PG62XkgM7gYqdrdP%2B0zHxDQ%3D%3D&username=vwx7a&password=smwhe&login=Send&__PREVIOUSPAGE=oL0kSGhtXbLqdHQg0uFgqg2'; 
                    $ch = curl_init(); 
                    curl_setopt($ch, CURLOPT_URL,$loginurl);
                    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
                    curl_setopt($ch, CURLOPT_POST, 1); 
                    curl_setopt($ch, CURLOPT_POSTFIELDS,$POSTFIELDS); 
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
                    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                    curl_setopt($ch, CURLOPT_REFERER, $reffer);
                    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
                    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);

                    $result = curl_exec ($ch);

                    return $result;
                    curl_close ($ch); 

        }

Could you please help me, for the above mentioned problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.