SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast ZefQ's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    curl and cookies

    So i'm trying to login to my university website so i can check my mail and so on from my start page without having to go to the site and login and so on.

    first of i have a php script that goes to the loginsite to collect a cookie that i need to be able to login. and get the value of a hidden element in the login form that has the same value as the cookie. (it changes everytime you reload the page)

    PHP Code:
    $ch curl_init(); 
    curl_setopt($chCURLOPT_URL,$login_url); 
    curl_setopt($chCURLOPT_USERAGENT$agent);
    curl_setopt($chCURLOPT_RETURNTRANSFER1);  
    curl_setopt($chCURLOPT_FOLLOWLOCATION1);
    curl_setopt($chCURLOPT_COOKIEFILE$cookiefile);
    curl_setopt($chCURLOPT_COOKIEJAR$cookiefile);
     
    $data curl_exec ($ch);
    curl_close ($ch);

    //parse the html into a DOMDocument
    $dom = new DOMDocument();
    @
    $dom->loadHTML($data);
    // grab all the on the page

    $xpath = new DOMXPath($dom);
    $inputs $xpath->evaluate("//input"); 
    //search the page for every input element
        
    for ($i 0$i $inputs->length$i++) {
            
    //checks the name attribute and saves the right value
            
    $input $inputs->item($i);
            if(
    $input->getAttribute('name') == ctid) {
                
    $ctid $input->getAttribute('value');
            }
        } 
    now the cookie file have the content "JSESSIONID=same as ctid was when the page was visited"
    Then i send this forum element values to the site that handels the login

    PHP Code:
    $postfields = array(
    'ctid' => $ctid,
    'fuser'    => $username,
    'fpassword' => $password,
    );

    $ch curl_init(); 
    curl_setopt($chCURLOPT_URL,$login_post_url);
    curl_setopt($chCURLOPT_USERAGENT$agent);
    curl_setopt($chCURLOPT_POST1); 
    curl_setopt($chCURLOPT_POSTFIELDS,
    http_build_query($postfields)); 
    curl_setopt($chCURLOPT_RETURNTRANSFER1); 
    curl_setopt($chCURLOPT_FOLLOWLOCATION1);
    curl_setopt($chCURLOPT_REFERER$login_url);

     
    curl_setopt($chCURLOPT_COOKIEFILE$cookiefile);
    curl_setopt($chCURLOPT_COOKIEJAR$cookiefile);
    /* using the cookie files from before

     
    $data = curl_exec ($ch);
     
    curl_close ($ch); 
    now the cookie file have the content "JSESSIONID=same as ctid was when the page was visited", "PortalPrint=the user name that was used to login"

    this works if i do echo $data the page prints and you are loged in.
    but i cant click on the links eg email link. this ones opens in a new window.

    in the new window it is a text saying you have been loged out. this most be cuz the cookies dont exist in the browser but in a file.

    i have tried setting my own cookie
    PHP Code:
    setCookie("JSESSIONID"$ctid);
    setCookie("PortalPrint"$username); 
    but i still get the same message. if i open a new window login to the site the right way the link on my own site works

    anyone got an ide of what i have to do? or is this lost cause =(
    //Kempe

  2. #2
    SitePoint Guru
    Join Date
    Feb 2008
    Posts
    655
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't set your own cookie because it won't be for the correct domain.

    Could you not use cURL to open up the links, read the contents of the page and print it back to you?

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Why not just use a program like Windows Live Mail to look at your email?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    SitePoint Enthusiast ZefQ's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by corbyboy View Post
    You can't set your own cookie because it won't be for the correct domain.

    Could you not use cURL to open up the links, read the contents of the page and print it back to you?
    Yeah sure i could but i do not just want it to be able to see if i got new mails, i want to beable to read them and replay and other things. thats why it would be best to be able to fake the cookie so the mail,schedule and what not popup
    could open and function as it would if i had open it from there webpage.

    but then again maybe i could write a script that makes all the the links on the page go to /browser.php?page=blabla then i use curl to get them and then show them same for forms and what not..

    but i would really like something simpler..


    Quote Originally Posted by arkinstall View Post
    Why not just use a program like Windows Live Mail to look at your email?
    cuz it is more then mail i like to be able to reach like schedule, room-booking and so on
    //Kempe

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you use setcookie(), your browser knows which domain the cookie came from. Im guessing this script runs from localhost for you. So your browser isn't going to send the cookie to some other other domain(your uni's site), because the browser rightfully thinks the cookie is meant for localhost only.

    You're opening the new window with your web browser. So of course the uni site is going to say you're not logged in because the browser isn't going to send the proper cookies.

    What you need to do is have curl fetch the url which corresponds to this new window. curl has the proper cookies since it's the browser that logged in. So it can send those cookies when it asks for that url, and it should work.

    Basically have curl log in, and view the page which lists the links to the emails in your inbox. Have it generate a bunch of links for you to click. If this is the url to a specific email


    Code:
    http://zefquniversity.com/email/view.php?messageid=12345678
    Then you could produce a link like
    PHP Code:
    <a href="http://localhost/proxy.php?url=<?php echo urlencode('http://zefquniversity.com/email/view.php?messageid=12345678'); ?>">homework assign #4</a>

    Then have proxy.php fetch the url using curl.

    You probably have a lot of work to do if you want to read and reply using a proxy script.

  6. #6
    SitePoint Enthusiast ZefQ's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    You probably have a lot of work to do if you want to read and reply using a proxy script.
    Yeah i'm saying..

    so that leeds me two 3 questions.

    1. is there no way to fake a cookie so it thinks it does come from the university-page

    2. is there another php function that can do what i want..

    3. could it be done with javascript. open the page in an iframe or something wait for it to load, set the username and password variables then a click command to submit the form?
    Last edited by ZefQ; Feb 20, 2009 at 11:34.
    //Kempe


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •