I'm having some trouble figuring out a way to establish a login session that is persistent enough to let me post. I figured i would try using PHP's cURL functions to log in, and then post the form to create a post.
First I created a new Geeklog user account called "App Helper".
Next, I want to first emulate the Geeklog login form:
Text Formatted Code
<form action="http://mysite.com/blog/users.php" method="post" style="margin-top:5px;margin-bottom:5px;">
<input name="loginname" value="App Helper">
<input name="passwd"><br /><input type="submit" value="Login">
So I use cURL to submit the login form:
Text Formatted Code
// create form data
$post_data = array ();
$post_data['loginname'] = 'App Helper';
$post_data['passwd'] = 'password';
$post_data['submit'] = 'Login';
foreach ($post_data as $key => $value)
{
$post_items[] = $key . '=' . $value;
}
$post_string = implode ('&', $post_items);
log_event ("post_string: $post_string");
// create a cURL connection
$curl_connection = curl_init ();
// set cURL options
curl_setopt ($curl_connection, CURLOPT_URL, 'http://mysite.com/blog/users.php');
curl_setopt ($curl_connection, CURLOPT_POSTFIELDS, $post_string);
curl_setopt ($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt ($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt ($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($curl_connection, CURLOPT_POST, true);
// execute the post
$result = curl_exec ($curl_connection);
At this point, $result contains:
Text Formatted Code
<html><head><meta http-equiv="refresh" content="0; URL=http://mysite.com/blog/index.php"></head></html>
So it seems I logged in successfully - I hope...
Next, I want to emulate the Geeklog create topic form:
Text Formatted Code
<form action="http://mysite.com/blog/forum/createtopic.php" method="POST" name="forumpost">
<input type="hidden" name="forum" value="7">
<input type="hidden" name="id" value="0">
<input type="hidden" name="modedit" value="0">
<input type="hidden" name="method" value="newtopic">
<input type="hidden" name="editpost" value="">
<input type="hidden" name="editpid" value="">
<input type="hidden" name="editid" value="">
<input type="hidden" name="page" value="0">
<input type="hidden" name="name" value="App Helper">
<input type="text" name="subject" value="subject of post">
<textarea name="comment">content of post</textarea>
<input type="checkbox" name="notify">
<input type="hidden" name="postmode" value="text">
<input type="submit" name="submit" value="Submit">
So, without closing the cURL connection, I make another post:
Text Formatted Code
// create form data
$post_data = array ();
$post_items = array ();
$post_data['forum'] = '7';
$post_data['id'] = '0';
$post_data['modedit'] = '0';
$post_data['method'] = 'newtopic';
$post_data['editpost'] = '';
$post_data['editpid'] = '';
$post_data['editid'] = '';
$post_data['page'] = '0';
$post_data['name'] = 'App Helper';
$post_data['subject'] = 'subject of post';
$post_data['comment'] = 'content of post';
$post_data['notify'] = '';
$post_data['postmode'] = 'text';
$post_data['submit'] = 'Submit';
foreach ($post_data as $key => $value)
{
$post_items[] = $key . '=' . $value;
}
$post_string = implode ('&', $post_items);
log_event ("post_string: $post_string");
// create a cURL connection
$curl_connection = curl_init ();
// set cURL options
curl_setopt ($curl_connection, CURLOPT_URL, 'http://mysite.com/blog/forum/createtopic.php');
curl_setopt ($curl_connection, CURLOPT_POSTFIELDS, $post_string);
curl_setopt ($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt ($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt ($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($curl_connection, CURLOPT_POST, true);
// execute the post
$result = curl_exec ($curl_connection);
Unfortunately, at this point, $result contains a full Geeklog web page with the standard access denied message:
Text Formatted Code
<h2>ACCESS ERROR</h2><br><p style="padding-left:10px;">Access to this forum is restricted.<br>It may be that you are not a member of this web site, and are attempting to access a members-only forum. You must register an account to access content on this web site.<br>If you feel this is incorrect, contact the site administrator.<p />
Any ideas on how I might be able to make this work the way I want? Or am I barking up the wrong tree?
-mono