Reputation: 6648
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.
Upvotes: 0
Views: 3984
Reputation: 88647
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;
Upvotes: 1
Reputation: 360662
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.
Upvotes: 0