Anomalous
04-25-2007, 01:36 PM
Hello All, I've been racking my brain for over an hour trying to figure out what is wrong with this script. I'm hoping it's something really simple I'm just noobing it on. Basically the script takes some basic data in a form and then posts it to mf.php, mf.php takes the data and sends out an email, but then I want the same data to be posted to a remote site. I wrote a small script to test whether the post was working, but everything shows up blank in the database.
$host = 'localhost';
$method = 'POST';
$path = '/mftest.php';
$postdata = "FirstName=Bob&City=Bobville&State=Bobonia";
function sendToHost($host,$method,$path,$postdata,$useragent=0)
{
// Supply a default method of GET if the one passed was empty
if (empty($method)) {
$method = 'GET';
}
$method = strtoupper($method);
$fp = fsockopen($host, 80);
if ($method == 'GET') {
$path .= '?' . $postdata;
}
fputs($fp, "$method $path HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp,"Content-type: application/x-www-form- urlencoded\r\n");
fputs($fp, "Content-length: " . strlen($postdata) . "\r\n");
if ($useragent) {
fputs($fp, "User-Agent: MSIE\r\n");
}
fputs($fp, "Connection: close\r\n\r\n");
if ($method == 'POST') {
fputs($fp, $postdata);
}
while (!feof($fp)) {
$buf .= fgets($fp,128);
}
fclose($fp);
}
sendToHost($host,$method,$path,$postdata,$useragent=0);
I've echo'd $postdata inside the function and the results were what I expected. I'm also sure it's reaching mftest.php because it's posting blank data to the database whenever I test the script. Here is mftest.php:
<?php
$dbusername = 'root';
$dbpass = '';
$dbname = 'database';
$dbhost = 'localhost';
mysql_connect("$dbhost", "$dbusername", "$dbpass") or die('Unable to connect to database');
mysql_select_db("$dbname") or die('Unable to select database');
$firstname = $_POST['FirstName'];
$city = $_POST['City'];
$state = $_POST['State'];
$query = "INSERT INTO omgtest(id, field1, field2, field3) VALUES('', '$firstname', '$city', '$state')";
$insert = mysql_query($query) or die(mysql_error());
?>
I've tried to echo the $_POST['FirstName'] and it's blank along with the others. I've POST'd right from the form page and everything worked as expected. Somewhere in between the sendToHost() function and mftest.php those POST variables are getting lost. I got the sendToHost function from a site pretty high up on google list search for "PHP HTTP POST" so I'm assuming it works. Am I formatting my POST string wrong or something?
I edited the code posted for relevance and changed some variable names and stuff to make it easier to understand. Hopefully I didn't leave in any mistakes that will be confused for my problem. =P
Thanks
$host = 'localhost';
$method = 'POST';
$path = '/mftest.php';
$postdata = "FirstName=Bob&City=Bobville&State=Bobonia";
function sendToHost($host,$method,$path,$postdata,$useragent=0)
{
// Supply a default method of GET if the one passed was empty
if (empty($method)) {
$method = 'GET';
}
$method = strtoupper($method);
$fp = fsockopen($host, 80);
if ($method == 'GET') {
$path .= '?' . $postdata;
}
fputs($fp, "$method $path HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp,"Content-type: application/x-www-form- urlencoded\r\n");
fputs($fp, "Content-length: " . strlen($postdata) . "\r\n");
if ($useragent) {
fputs($fp, "User-Agent: MSIE\r\n");
}
fputs($fp, "Connection: close\r\n\r\n");
if ($method == 'POST') {
fputs($fp, $postdata);
}
while (!feof($fp)) {
$buf .= fgets($fp,128);
}
fclose($fp);
}
sendToHost($host,$method,$path,$postdata,$useragent=0);
I've echo'd $postdata inside the function and the results were what I expected. I'm also sure it's reaching mftest.php because it's posting blank data to the database whenever I test the script. Here is mftest.php:
<?php
$dbusername = 'root';
$dbpass = '';
$dbname = 'database';
$dbhost = 'localhost';
mysql_connect("$dbhost", "$dbusername", "$dbpass") or die('Unable to connect to database');
mysql_select_db("$dbname") or die('Unable to select database');
$firstname = $_POST['FirstName'];
$city = $_POST['City'];
$state = $_POST['State'];
$query = "INSERT INTO omgtest(id, field1, field2, field3) VALUES('', '$firstname', '$city', '$state')";
$insert = mysql_query($query) or die(mysql_error());
?>
I've tried to echo the $_POST['FirstName'] and it's blank along with the others. I've POST'd right from the form page and everything worked as expected. Somewhere in between the sendToHost() function and mftest.php those POST variables are getting lost. I got the sendToHost function from a site pretty high up on google list search for "PHP HTTP POST" so I'm assuming it works. Am I formatting my POST string wrong or something?
I edited the code posted for relevance and changed some variable names and stuff to make it easier to understand. Hopefully I didn't leave in any mistakes that will be confused for my problem. =P
Thanks