1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP cURL session issues with login

Discussion in 'PHP & Perl' started by youtalk, Dec 29, 2013.

  1. youtalk

    youtalk Regular Member

    Joined:
    Jul 5, 2012
    Messages:
    337
    Likes Received:
    6
    Occupation:
    Owner
    Location:
    I don't even know anymore
    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?
     
  2. Diplomat

    Diplomat Jr. VIP Jr. VIP Premium Member

    Joined:
    Oct 25, 2011
    Messages:
    872
    Likes Received:
    410
    Occupation:
    CEO
    What kind of problem do you have?

    Also show me your curl code, maybe I can see something what's wrong.
     
  3. youtalk

    youtalk Regular Member

    Joined:
    Jul 5, 2012
    Messages:
    337
    Likes Received:
    6
    Occupation:
    Owner
    Location:
    I don't even know anymore
    Let me get one of the developers to post that for you.
     
  4. youtalk

    youtalk Regular Member

    Joined:
    Jul 5, 2012
    Messages:
    337
    Likes Received:
    6
    Occupation:
    Owner
    Location:
    I don't even know anymore
    The issue were having is maintaining sessions. It keeps booting us out before the data is collected.
     
  5. youtalk

    youtalk Regular Member

    Joined:
    Jul 5, 2012
    Messages:
    337
    Likes Received:
    6
    Occupation:
    Owner
    Location:
    I don't even know anymore
    Here is the issue: it is not allowing curl sessions and data scrapping through curl sessions from password protected websites.
     
  6. Diplomat

    Diplomat Jr. VIP Jr. VIP Premium Member

    Joined:
    Oct 25, 2011
    Messages:
    872
    Likes Received:
    410
    Occupation:
    CEO
    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.
     
  7. youtalk

    youtalk Regular Member

    Joined:
    Jul 5, 2012
    Messages:
    337
    Likes Received:
    6
    Occupation:
    Owner
    Location:
    I don't even know anymore
    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 />";
    
    
    ?>
     
  8. laowai

    laowai Power Member

    Joined:
    Feb 27, 2011
    Messages:
    522
    Likes Received:
    184
    One thing to check is that cookie.txt is writable and that there is data after login
     
  9. qadir87

    qadir87 Newbie

    Joined:
    Dec 29, 2013
    Messages:
    1
    Likes Received:
    0
    There is definitely data after login and cookie.txt is writable :)
     
  10. Diplomat

    Diplomat Jr. VIP Jr. VIP Premium Member

    Joined:
    Oct 25, 2011
    Messages:
    872
    Likes Received:
    410
    Occupation:
    CEO
    Why don't you use cookiejar for DownloadUrl function?
     
  11. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,155
  12. barigain

    barigain Junior Member

    Joined:
    Aug 23, 2012
    Messages:
    100
    Likes Received:
    12
    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.
     
  13. sephoral

    sephoral Newbie

    Joined:
    Jan 18, 2014
    Messages:
    14
    Likes Received:
    0
    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