cURL Cookie Problem

Discussion in 'PHP' started by reptile1903, Feb 14, 2009.

  1. #1
    Hello,

    I try to post with curl to SMF. Everything is ok, but when i try to send pm i get "Please Login". I guess its cookie problem.

    login.php

    
    <?php
    
    // login.php
    
    $url = "http://www.xxx.com/forum/login2/";
    $postfields = "user=xxx&passwrd=xxx&cookieneverexp=on";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
    curl_setopt($ch, CURLOPT_COOKIEJAR, "C:\wamp\www\cookie.txt");
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);  
    ?> 
    
    PHP:
    pm.php

    
    <?php
    
    // pm.php
    
    $url = "http://www.xxx.com/forum/pm/?sa=send2";
    $post = "to=reptile&subject=Test&message=test&Submit=Send";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_COOKIEFILE, "C:\wamp\www\cookie.txt");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $out = curl_exec($ch);
    curl_close($ch);  
    ?>
    
    PHP:
    It is working, logining , going to pm page, writing message to blanks, click to send, then "Please Login".

    Thanks.
     
    reptile1903, Feb 14, 2009 IP
  2. Shoro

    Shoro Peon

    Messages:
    143
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Check the cookie.txt file you're writing to. Are the cookies being written to it? Try including both the cookiejar and cookiefile options in both scripts.
     
    Shoro, Feb 14, 2009 IP
  3. reptile1903

    reptile1903 Member

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    Yes cookies are writing but it isnt still working. I dont know what can i do. I have written some forums but i cant get any reply.
     
    reptile1903, Feb 15, 2009 IP
  4. e_hippie

    e_hippie Guest

    Messages:
    32
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Should CURLOPT_POST be true or 1?

    I'd also add a few things to your curl options:
    
    // Add a nice common useragent
    $curl_useragent = 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11';
    $curl_httpheader = array( 'Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
    'Accept-Language: en-us,en;q=0.5',
    'Accept-Encoding: gzip,deflate',
    'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7' );
    $curl_referer = 'http://www.xxx.com/';
    
    
    curl_setopt( $ch, CURLOPT_FAILONERROR, 0 );
    curl_setopt( $ch, CURLOPT_TIMEOUT, 0 );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt( $ch, CURLOPT_USERAGENT, $curl_useragent );
    curl_setopt( $ch, HTTPHEADER, $curl_httpheader );
    curl_setopt( $ch, CURLOPT_REFERER, $curl_referer );
    curl_setopt( $ch, CURLOPT_VERBOSE, false );
    curl_setopt( $ch, CURLOPT_COOKIESESSION, true );
    
    Dos that make a difference?
    PHP:
     
    e_hippie, Feb 21, 2009 IP
  5. harrisunderwork

    harrisunderwork Well-Known Member

    Messages:
    1,005
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    135
    #5
    You dont need to close the curl session in pm page, just go in one shot that will solve your problem.
     
    harrisunderwork, Feb 22, 2009 IP