Hello,
What I am trying to do:
[*]Read "userz.txt" for username:password combination
[*]Login to a site (by submitting a form) using my CURL class
[*]Stay login with current user and submit another form
[*]Login using the second username:password combination
[*]Repeat the steps.
Here is my code:
<?php
class Curl {
public $cookieJar = "";
// Make sure the cookies.txt file is read/write permissions
public function __construct($cookieJarFile = '/home/public_html/example/temp/cookie.txt') {
$this->cookieJar = $cookieJarFile;
}
function setup() {
$header = array();
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Pragma: "; // browsers keep this blank.
curl_setopt($this->curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7');
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($this->curl, CURLOPT_COOKIEJAR, $this->cookieJar);
curl_setopt($this->curl, CURLOPT_COOKIEFILE, $this->cookieJar);
curl_setopt($this->curl, CURLOPT_AUTOREFERER, true);
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
}
function get($url) {
$this->curl = curl_init($url);
$this->setup();
return $this->request();
}
function getAll($reg, $str) {
preg_match_all($reg, $str, $matches);
return $matches[1];
}
function postForm($url, $fields, $referer = '') {
$this->curl = curl_init($url);
$this->setup();
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_POST, 1);
curl_setopt($this->curl, CURLOPT_REFERER, $referer);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $fields);
return $this->request();
}
function getInfo($info) {
$info = ($info == 'lasturl') ? curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL) : curl_getinfo($this->curl, $info);
return $info;
}
function request() {
return curl_exec($this->curl);
}
}
?>
<?php
$referer = "http://www.google.com.com";
$file = fopen("userz.txt", "r") or exit("Unable to open file!");
//while loop begins
while(!feof($file)) {
$curl = new Curl();
$data = fgets($file);
$info = explode(":",$data);
//new values for user
$newdesc = "I'm new here.";
$newurl = "http://mysite.com";
//for login session
$url = "http://www.example.com/login.php";
$fields = "UN=$info[0]&PW=$info[1]";
//for profile page
$profileurl = "http://www.example.com/profile.php";
$profilefields = "update_description=$newdesc&update_url=$newurl&submit=Submit";
//unleash the hell
$html = $curl->postForm($url, $fields, $referer);
$html2 = $curl->postForm($profileurl, $profilefields, $referer);
}
echo $html2;
?>
What problem I am facing:
It reads the file correctly. But the second form is submitted for only the LAST username:password combination.
For example, if userz.txt file contains..
user1:pass1
user2:pass2
user3:pass3
Then the profile of only user3 will be updated.
I am unable to figure out the bug in the code. I shall be thankful if anybody could identify the problem and possibly provide the solution.
Thank you.