Hi.
Glad you found a solution.
Let me suggest another one tho.
You can have cUrl save all cookies by setting the CURLOPT_COOKIEFILE and CURLOPT_COOKIEJAR options. So if the site uses Sessions to log you in, you can simply pass the POST data required, and then save the Session ID cookie to maintain the session.
Say the server has a script like this:
-
<?php
-
session_start();
-
-
if(isset($_SESSION['UserName'])) {
-
echo "You are logged in as ". $_SESSION['UserName'];
-
}
-
else if(isset($_POST['UserName'])) {
-
$_SESSION['UserName'] = $_POST['UserName'];
-
echo "You have been logged in as ". $_POST['UserName'];
-
}
-
else {
-
echo "No username available in session or post.";
-
}
-
?>
-
You can log in by doing something like this:
-
<?php
-
// Create cookie file if it doesn't exists
-
$cookieFile = "myCookiefile.txt";
-
if(!file_exists($cookieFile)) {
-
$fh = fopen($cookieFile, "w");
-
fwrite($fh, "");
-
fclose($fh);
-
}
-
-
// Initialize the cUrl connection
-
$ch = curl_init();
-
curl_setopt($ch, CURLOPT_URL, 'http://localhost/login.php');
-
-
// Set the POST data
-
$data = array('UserName' => 'Foo');
-
curl_setopt($ch, CURLOPT_POST, 1);
-
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
-
-
// Set the COOKIE files
-
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
-
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
-
-
// Send request and print results
-
curl_exec($ch);
-
-
// Close the connection
-
curl_close($ch);
-
?>
-
It will log you in on the first execution but print the "You are logged in" message after that.