SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy curl login failed cause cookie is not set...

    Dear all,

    I'm trying to login to a forum using curl method.

    $loginURL="http://test.com/login.php?action=login";

    $loginFields="formhash=5abfae36&cookietime=2592000&loginfield=username&username=test&password=test&loginsubmit=true";

    $cookiefile = "/temp/cookie.txt";
    $agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);

    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $loginFields);
    curl_setopt($ch, CURLOPT_URL, $loginURL);


    $ret = curl_exec($ch);
    curl_close($ch);
    //return $ret;

    echo "Results: <br>".$ret;
    print_r($_POST);
    When i test the login link with this curl script, it will show login successful. But when it redirects the user to the other page, it shows user in guest status.

    I've check the cookie directory and found that the cookie is not set.

    I'm very confuse and here are my few questions regarding to this matter..

    1. When user login as usual(not through curl), the cookie will be created automatically, how come when goes to curl, the cookie is missing?

    2. What is the correct way to create the cookie when user login using curl method? Is my setting correct in the curl script above?

    Hope to hear from your advice soon... Thank you!
    Last edited by cathie; Sep 14, 2008 at 06:52.

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I make script with curl login and this work. See examples php.net/manual/ru/function.curl-setopt.php

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by witigo View Post
    I make script with curl login and this work. See examples php.net/manual/ru/function.curl-setopt.php
    Dear witigo,

    Thanks for your reply..

    I've gone through the page you shared here..

    I guess the problem is that I need to define an absolute path for CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE. However, where suppose this path point to? the tmp file in my server? (sorry, I have no experience and idea in this)

    Do you see any more mistake in my script?

    Thank you!

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Absolute path examples:
    widows: example: d:/temp/cookie
    unix:/.... blabla.........script_folder......./cookie

    File cookie must be available at record.

    Do you see any more mistake in my script?
    Your use 'curl_setopt($ch, CURLOPT_POST,.....' twice, this not good.

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by witigo View Post
    Absolute path examples:
    widows: example: d:/temp/cookie
    unix:/.... blabla.........script_folder......./cookie

    File cookie must be available at record.
    Hmm.. I'm not familiar with the structure in server but i know I'm using unix. So can I specify any folder as the absolute path?

    Quote Originally Posted by witigo View Post
    Your use 'curl_setopt($ch, CURLOPT_POST,.....' twice, this not good.
    yaya, I just notice that(for the double post command).. I've removed it.. Thanks for debugging...


    Thanks witigo...

  6. #6
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know where's the path to temporary folder now..
    so eveyone will use the same cookie?

    I tried execute the curl script, the value in cookie has changed, however, when i try to view the cookie from my pc, the cookie is not created...

    I'm refering to this tutorial...

    PHP Code:
    <?php
    /*
    This script is an example of using curl in php to log into on one page and 
    then get another page passing all cookies from the first page along with you.
    If this script was a bit more advanced it might trick the server into 
    thinking its netscape and even pass a fake referer, yo look like it surfed 
    from a local page.
    */

    $ch curl_init();
    curl_setopt($chCURLOPT_COOKIEJAR"/tmp/cookieFileName");
    curl_setopt($chCURLOPT_URL,"http://www.myterminal.com/checkpwd.asp");
    curl_setopt($chCURLOPT_POST1);
    curl_setopt($chCURLOPT_POSTFIELDS"UserID=username&password=passwd");

    ob_start();      // prevent any output
    curl_exec ($ch); // execute the curl command
    ob_end_clean();  // stop preventing output

    curl_close ($ch);
    unset(
    $ch);

    $ch curl_init();
    curl_setopt($chCURLOPT_RETURNTRANSFER,1);
    curl_setopt($chCURLOPT_COOKIEFILE"/tmp/cookieFileName");
    curl_setopt($chCURLOPT_URL,"http://www.myterminal.com/list.asp");

    $buf2 curl_exec ($ch);

    curl_close ($ch);

    echo 
    "<PRE>".htmlentities($buf2);
    ?>
    Any one knows why this happenned?

  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not familiar with the structure in server but i know I'm using unix. So can I specify any folder as the absolute path?
    try this:
    $cookiefile = dirname(__FILE__)."/cookie.txt";

  8. #8
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by witigo View Post
    try this:
    $cookiefile = dirname(__FILE__)."/cookie.txt";
    Thanks again witigo,

    I guess i left the final phase now where to pass the cookie value to the right link to make the login successful (I hope).. I'm now looking for the correct path to send the cookiefile value as I actually working on an opensource forum system, a bit complicated in the script...

    Thanks for being so helpful~
    I need help


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
  •