Reputation: 5409
I'm trying to post to a phpBB2 forum running on localhost with PHP and cURL. I've handled the logging in alright, it's just the posting that I can't get my head around.
Here's my code:
<?php
$cookieFile = 'C:\xampp\htdocs\cookies\\' . uniqid(true) . '.txt';
// Login
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://localhost/phpbb2/login.php');
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_POST, true);
$postVars = array('username' => 'admin', 'password' => 'password', 'autologin' => 'on', 'login' => 'Log in');
curl_setopt($curl, CURLOPT_POSTFIELDS, $postVars);
$resp = curl_exec($curl);
curl_close($curl);
// Parse sid from cookie file
preg_match('/phpbb2mysql_sid\t(.*)/', file_get_contents($cookieFile), $match);
$sId = $match[1];
// Post
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://localhost/phpbb2/posting.php?mode=newtopic&f=1');
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookieFile);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_POST, true);
$postVars = array('subject' => 'Test post',
'message' => 'Test post, please ignore.',
'sid' => $sId,
'f' => 1,
'post' => 'Submit');
curl_setopt($curl, CURLOPT_POSTFIELDS, $postVars);
$resp = curl_exec($curl);
curl_close($curl);
echo $resp;
The cURL sets the cookie fine, and I know the sid
parameter I'm sending with my POST request is correct because it's the same one that's in the database. However, when I run this code, phpBB spits out this error: Invalid Session. Please resubmit the form.
.
I don't get it. I'm grabbing the cookie after I login, sending it with the POST request to create a new topic, yet it says invalid session.
What could be going wrong here?
Upvotes: 0
Views: 1345
Reputation: 25
I'd hazard a guess it is because you execute
curl_close($curl);
$curl = curl_init();
after logging in, before then posting. You want to delete those two lines and continue using the same curl handle.
BTW: Your phpbb login code worked nicely for me... ;)
Upvotes: 1