Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Posting Data from a php form to two servers

Status
Not open for further replies.

tewari68

Programmer
Jan 25, 2005
87
US
Hi,
How can I post a form data on two different servers at the same time. The two servers are in HA cluster and the user connect to anyone of the servers, I want to submit the formdata of the user to both the servers simultaneously.
The servers can locally see each other with an internal ipaddress (10.x.xx.xxx).

Appreciate any help in this regards.

Thanks,
tewari
 
you cannot, with html alone, submit a form to two urls.

however, on the server side, you can use php to do anyone of the following:

1. receive form data and send it to another page after processing it
2. receive form data and write it to two databases sequentially
3. many other things.

in HA setups that are database driven, typically the databases are replicated and synchronised, so a write to one database will automatically be carried over to all others within the cluster. this is often done with all writes going to a single master server and reads being proxied to a number of different slaves. the efficiency of this tends to be good for those applications that are primarily read based. where there is a higher proportion of writes, there are other potential solutions.

so assuming you are after a simple variety of item 1 and your original form uses the post method, this is how you could process

Code:
<?
if (isset($_POST['submit'])){	//or some other variable to test submission
	
	if (redirectInput()) {
		echo "redirect worked ok";
	} else {
		echo "redirect failed";
	}
	processFormLocally();
}

function processFormLocally(){
	//do some processing
}

function redirectInput(){
	$remoteURL = "[URL unfurl="true"]http://10.x.y.z/somepage.php";[/URL]
	return http_post_fields($remoteURL, $_POST);
}
?>

You need the pecl extension pecl_http available to do it this easily. otherwise you could use something like this:

Code:
function redirectTheLongWay(){
	
	$host = "10.x.y.z";
	$url = "[URL unfurl="true"]http://$host/somepage.php";[/URL]
	//need to reprocess the POST superglobal
	$data = "";
	
	foreach ($_POST as $key=>$var){
		$data .= urlencode($key)."="urlencode(trim($var)) . "&";
	}
	
	$data = trim($data, "&"); //get rid of the trailing &
	$dataLength = strlen($data); //get length of data
	
	$headers = <<<EOF
POST $url  HTTP/1.1
Host: $host
User-Agent: PHP Script;
Content-Type: application/x-[URL unfurl="true"]www-form-urlencoded[/URL]
Content-Length: $dataLength;
Connection: close

$data

EOF;
	//open a socket to the remote server
	$sk = fsockopen($host, 80, $errNo, $errStr);
	//test for a good open 
	if (!$sk) {
		die ("Problem opening socket <br/>$errStr ($errNo)");
	}
	//now write the data to the open socket
	fwrite($sk, $headers, strlen($headers));
	fclose ($sk);	
}

ideally you should grab the response headers and check for errors from the last function

to write the data to two different database servers is the easiest solution

Code:
function posttoMultiDB(){
	$dbs = array(
				array(	"host"=>"10.x.y.z",
						"username"=>"username",
						"password"=>"password",
						"database"=>"database"),
				array(	"host"=>"10.x.y.z",
						"username"=>"username",
						"password"=>"password",
						"database"=>"database")
			);

	$query = "	Insert 
				into $table
				set 
				fieldname = '".mysql_escape_string(trim($_POST['fieldname']))."'";

	foreach ($dbs as $c=>$connData){
		$conn{$c} = mysql_connect($connData['host'], $connData['username'], $connData['password'])
					or die(mysql_error());
		mysql_select_db($connData['database'], $conn{$c}) 
					or die(mysql_error());
		$result{$c} = mysql_query($query, $conn{$c});
	}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top