Archive

Posts Tagged ‘PHP’

Mimic a Login with CURL

June 11th, 2009 Matt J. Wilson No comments

Maybe this concept is much easier for others, but I had a hard time figuring out how to properly use CURL and PHP to mimic a login.  I’ll go ahead and post the code and then describe how you can figure out exactly what you should be doing to mimic a website’s login process:

<?
$ch = curl_init();

// Let's set the URL of where we want the form to POST to
curl_setopt($ch, CURLOPT_URL, 'http://www.somewebsite.com/login');

// Set the referring page
curl_setopt ($ch, CURLOPT_REFERER, "http://www.somewebsite.com");

// Make sure we enable the POST
curl_setopt ($ch, CURLOPT_POST, 1);

// Set the parameters for the POST fields
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'action=login&user=Someuser&password=Somepassword&submit=Login');

// This is the key line here.  This will mimic a cookie on our machine, but instead
// will save it to the local directory of this script in a cookie.txt file.
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

// We don't want it to print out the results for this, so set
// this option to 1
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

// Execute our login request
curl_exec ($ch);

// Now we should be able to go wherever we want since we've mimiced a login
// by using curl and a 'cookie.txt' cookie jar file
$target_url = "http://www.website.com/the_page_we_want_to_visit_after_login.php";
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_exec($ch);
?>

To figure out what pages to visit and exactly what items were being posted to the above page, I used a nice plugin for Mozilla called LiveHTTPHeaders.  The easiest way to detect all of the URL’s, POST’ed variables, and any other checks that a login process does is to use this add-on to capture all of that information and then mimic it with the CURL functions above.  You should be able to login and then roam around freely with your usual credentials, thanks to our cookie.txt file, which will look something like:

# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.

.somewebsite.com	TRUE	/	FALSE	0	PHPSESSID	9c78c16ef63ad3cb2cd9f1d00466c319
.somewebsite.com	TRUE	/	FALSE	1276117626	isAuthorized	Y

I hope someone else finds this useful!

Categories: Programming Tags: ,

Reading a Google Docs spreadsheet using PHP

June 2nd, 2009 Matt J. Wilson No comments

So I recently came across the need to read in a Google Docs spreadhseet via PHP, which is actually incredibly easy to do.  I opted to use CSV output and had Google Docs give me a link to use.  It’s as simple as clicking “Share”, “Start Publishing”, then changing the format from “Web-Page” to “CSV (comma-separated values)”.  The link for this example is:

http://spreadsheets.google.com/pub?key=rwY-bmP3cyAzaS5xzxv3XFg&output=csv

Now for the code; my example reads in the file and inserts into my custom ‘markers’ table, but you should be able to get the picture from this:

<?
require("includes/db_conn.php");

// Opens a connection to a MySQL server
$connection = mysql_connect("localhost", $username, $password);
if (!$connection) {
die("Not connected : " . mysql_error());
}

// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die("Can\'t use db : " . mysql_error());
}

$file = fopen("http://spreadsheets.google.com/pub?key=rwY-bmP3cyAzaS5xzxv3XFg&amp;output=csv","r");

//we don't want to get the first line
$firstLine = true;

$addresses = array();
$i = 0;

while (($arr = fgetcsv($file, 1000, ",")) !== FALSE)
{
if ($firstLine)
{
$firstLine = false;
}
else
{
$arr = str_replace("'", "\'", $arr);
$addresses[$i++] = "'" . $arr[1] . "'";

$sql = "select * from markers where address ='" . $arr[1] . "';";
$result = mysql_query($sql);
if(mysql_num_rows($result) == 0)
{
$sql = "insert into markers (name, address, description, type) values (".
"'" . $arr[0] . "', '" . $arr[1] . "', '" . $arr[2] . "', '" . $arr[3] . "');";
mysql_query($sql);
}
else
{
$sql = "UPDATE markers SET name='" . $arr[0] . "', description = '" . $arr[2] . "', type = '" . $arr[3] . "' WHERE address='" . $arr[1] . "';";
mysql_query($sql);
}
if(mysql_error())
{
echo mysql_error() ."<br>\n";
}
}
}

// delete any addresses that existed previously, but aren't in the file now
$sql = "DELETE FROM markers WHERE address NOT IN(" . implode(",", $addresses) . ");";
mysql_query($sql);

fclose($file)
?>
Categories: Programming Tags: ,