PHP cURL session issues with login

youtalk

Regular Member
Joined
Jul 5, 2012
Messages
353
Reaction score
6
Just to start this off, I am not a developer.
But I'm trying to help my developers with issues we're facing.

We are having issues with sessions on logging into sites with cURL and PHP.
What is the best way to code this to help with session issues?
 
What kind of problem do you have?

Also show me your curl code, maybe I can see something what's wrong.
 
Let me get one of the developers to post that for you.
 
The issue were having is maintaining sessions. It keeps booting us out before the data is collected.
 
Here is the issue: it is not allowing curl sessions and data scrapping through curl sessions from password protected websites.
 
Well, PHP sessions are server based.. so yea. With PHP it's little bit difficult to do it, but I really can't suggest anything if I haven't seen your CURL code.
 
Code:
<?php

function login($purl, $pData = "") {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $purl); //login URL
    curl_setopt($ch, CURLOPT_POST, 1);
    $postData = 'login=login&login%3Ausername=XXXXXXXX&login%3Apassword=XXXX&login%3Aloginimg.x=53&login%3Aloginimg.y=9&javax.faces.ViewState=j_id1';
    /*$postData='
      login:username=brad
      &login:password=xxxxx
      &txthdbtn=Login
      &imageField.x=27
      &imageField.y=8';
     */
    curl_setopt($ch, CURLOPT_POSTFIELDS, $pData);
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $store = curl_exec($ch);
    if(!$store) {
        echo 'error occurred';
    }
    echo "_______________<br />";
    print_r($store);
    echo "_______________<br />";
    return $ch;
}


function downloadUrl($Url, $ch) {
    curl_setopt($ch, CURLOPT_URL, $Url);
    curl_setopt($ch, CURLOPT_POST, 0);
    curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com/"');
    curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $output = curl_exec($ch);
    return $output;
}
$uuu = "https://www.beconsumables.com/cm/UserShunt2.cfm?action=logon";
$login_url = "https://www.aviall.com/aviall/login-iframe.seam";
$ref = "http://www.beconsumables.com/cm/support/support.cfm";
$postv = "POSTDATA=username=XXXXX&upasswd=XXXXXX&Submit+Request=%3C%3C+Log+In+%3E%3E";
$zerosch = "http://www.zerosch.com/";




//$session = login($uuu, $postv);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $zerosch); //login URL
    //curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $store = curl_exec($ch);
    if(!$store) {
        echo 'error occurred';
    }


$first_url = "https://www.aviall.com/aviall/searchPart.ftl?searchTerm=HL20PB5-3&cid=99777011756";
//$ret = downloadUrl($first_url, $session);
echo "XXXXXXXXXXX<br />";
//print_r($ret);
echo "XXXXXXXXXXX<br />";


?>
 
One thing to check is that cookie.txt is writable and that there is data after login
 
Why don't you use cookiejar for DownloadUrl function?
 
Please note that most sites check a referer in login function. You set google.com as referer and it's not normal.
Usually user opens website and then fills and submit the form. So referer should be from the same domain.
 
Hi, I faced a little problem doing that when I started using curl for this kind of purposes.

Don't forget to curl_close() right after the exec if you need the cookie in the next call.

The cookie will be only written physically when the script dies or when the curl session closes.

Hope it helps

Regards
 
Back
Top