0

I'm trying to make it use the cookie, which is saved, instead of logging in each time the site is visited.

<?php
$uid = $_GET['uid'];
$ch = curl_init();
$cookie_file = dirname(__FILE__) . '/cookie.txt';
curl_setopt($ch, CURLOPT_URL, "http://www.forum.net/member.php?action=do_login");
curl_setopt($ch, CURLOPT_REFERER, "http://www.forum.net/member.php?action=login");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5");
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookie.txt');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, '/cookie.txt');
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=USERNAME&password=PASSWORD");
$exec = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, "http://www.forum.net/reputation.php?uid=" . $uid . "&page=1");
$exec = curl_exec($ch);
echo $exec;
?>

It won't let me reuse the cookie, for some reason, but instead it just logs me in. The cookie.txt file contains the cookie stuff.

MortenMoulder
  • 6,138
  • 11
  • 60
  • 116
  • 1
    You are setting both `CURLOPT_COOKIEJAR` and `CURLOPT_COOKIEFILE` twice, and the final result will be different values. `/cookie.txt` will store the file in the system root, and `dirname(__FILE__) . '/cookie.txt'` will store the file in the same directory as the currently executing script. Ensure you are only setting each option once, with the same value (probably the latter). – DaveRandom Jul 08 '13 at 17:12
  • Okay I set the CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE to: dirname(__FILE__) . '/cookie.txt' - however, it won't work. Updated OP with new code. – MortenMoulder Jul 08 '13 at 17:24
  • Possible duplicate http://stackoverflow.com/questions/12885538/php-curl-and-cookies/ – ChrisF Feb 21 '14 at 19:39

2 Answers2

1

You are setting both CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE twice, and the final result will be different values. '/cookie.txt' will store the file in the system root, and dirname(__FILE__) . '/cookie.txt' will store the file in the same directory as the currently executing script. Ensure you are only setting each option once, with the same value (probably the latter).

You are also making two requests with the same handle - this is not the recommended approach, and is likely semantically incorrect, because you will end up POSTing to both pages, when the second should probably be a GET.

I recommend storing the cookie jar path in a variable at the top of the block, and passing this in. This makes it easier to see what is going on.

I suspect you want something more like this:

<?php

    // Username and password for login
    $username = 'USERNAME';
    $password = 'PASSWORD';

    // Create the target URLs
    // Don't forget to escape your user input!
    $loginUrl = "http://www.forum.net/member.php?action=do_login";
    $reputationUrl = "http://www.forum.net/reputation.php?uid=" . urlencode($_GET['uid']) . "&page=1";

    // Referer value for login request
    $loginReferer = "http://www.forum.net/member.php?action=login";

    // Note that __DIR__ can also be used in PHP 5.3+
    $cookieJar = dirname(__FILE__) . '/cookie.txt';

    // The User-Agent string to send
    $userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5";


    // Create the cURL handle for login
    $ch = curl_init($loginUrl);

    // Set connection meta
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    // Set header-related options
    curl_setopt($ch, CURLOPT_REFERER, $loginReferer);
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieJar);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieJar);

    // Set request body
    $bodyFields = array(
        'username' => $username,
        'password' => $password
    );
    $body = http_build_query($bodyFields);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

    // Execute the request
    if (!curl_exec($ch)) {
        exit("ERROR: Login request failed: " . curl_error($ch));
    }
    curl_close($ch);


    // Create the cURL handle for reputation scrape
    $ch = curl_init($reputationUrl);

    // Set connection meta
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    // Set header-related options
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieJar);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieJar);

    // Execute the request
    if (!$result = curl_exec($ch)) {
        exit("ERROR: Reputation scrape request failed: " . curl_error($ch));
    }

    // Output the result
    echo $result;
DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • Thanks a lot for organising my code. That really looks good! However... That code gives me this error: Parse error: syntax error, unexpected '"ERROR: Login request failed: ' (T_CONSTANT_ENCAPSED_STRING) in C:\myfile.php on line 48 – MortenMoulder Jul 08 '13 at 18:02
  • @Snorlax fixed. I always forget that `exit` requires braces, unlike `echo` and `print` – DaveRandom Jul 08 '13 at 18:09
  • Okay it works now, but it still doesn't use the cookie file. It logs in every time I visit the file. And yes, the cookie file is made, and is being made. – MortenMoulder Jul 09 '13 at 16:06
0

You have:

curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookie.txt');
...[snip]...    
curl_setopt($ch, CURLOPT_COOKIEFILE, '/cookie.txt');

You are resetting the location of the cookiefile to the root of your filesystem, where the webserver process almost certainly does not have permission to write a file.

As well, make sure that the webserver process CAN write files wherever dirname(__FILE__) points to.

Marc B
  • 356,200
  • 43
  • 426
  • 500