Bác thử cái này nhé, Em viết thử script để login vào mấy forum vbb :-p
Đây là file để login, lưu cookie lại. Cookie sẽ được lưu ở file cookies.txt
PHP Code:
<?php
$username = 'user';
$password = 'pass';
$refererUrl = 'http://ddth.com/';
$loginUrl = 'http://ddth.com/login.php?do=login';
$postFields = array(
"vb_login_username={$username}",
"vb_login_password={$password}",
"securitytoken=guest",
"do=login",
"cookieuser=1"
);
$cookieFile = dirname(__FILE__).DIRECTORY_SEPARATOR.'cookies.txt';
// Initialize CURL
$curl = curl_init($loginUrl);
// Set CURL cookie option
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookieFile);
// Custom HTTP referer for CURL Request
curl_setopt($curl, CURLOPT_REFERER, $refererUrl);
// Get the response as a string from curl_exec()
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Don't use a cached version of the url
curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
// Custom HTTP User agent
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER***91;'HTTP_USER_AGENT'***93;);
// Change request method to POST
curl_setopt($curl, CURLOPT_POST, true);
// Send post data
curl_setopt($curl, CURLOPT_POSTFIELDS, implode('&', $postFields));
// Return headers
curl_setopt($curl, CURLOPT_HEADER, 1);
// Send request to login
curl_exec($curl);
?>
Còn đây là file để đọc nội dung trang sau khi đã login
PHP Code:
<?php
$forumUrl = 'http://ddth.com/';
$cookieFile = dirname(__FILE__).DIRECTORY_SEPARATOR.'cookies.txt';
$curl = curl_init();
// Set CURL cookie option
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookieFile);
curl_setopt($curl, CURLOPT_URL, $forumUrl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
echo curl_exec($curl);
?>
Bookmarks