Forum Moderators: coopster

Message Too Old, No Replies

POST Method Without Forms

         

Komodo_Tale

11:48 pm on Jan 16, 2007 (gmt 0)

10+ Year Member



I am trying to pass variable content via POST Method, visit the target document, then retrieve the variable content without using a HTML Form.

I found the script below which makes me think I am on the right track, except that this script sends the info to the target using fsockopen instead of delivering the browser to the target document.

Can anyone explain how to do this? Thanks.

==================

/* sendToHost
* ~~~~~~~~~~
* Params:
* $host - Just the hostname. No http:// or
/path/to/file.html portions
* $method - get or post, case-insensitive
* $path - The /path/to/file.html part
* $data - The query string, without initial question mark
* $useragent - If true, 'MSIE' will be sent as
the User-Agent (optional)
*
* Examples:
* sendToHost('www.google.com','get','/search','q=php_imlib');
* sendToHost('www.example.com','post','/some_script.cgi',
* 'param=First+Param&second=Second+param');
*/

function sendToHost($host,$method,$path,$data,$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 .= '?' . $data;
}
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($data) . "\r\n");
if ($useragent) {
fputs($fp, "User-Agent: MSIE\r\n");
}
fputs($fp, "Connection: close\r\n\r\n");
if ($method == 'POST') {
fputs($fp, $data);
}

while (!feof($fp)) {
$buf .= fgets($fp,128);
}
fclose($fp);
return $buf;
}

mcavic

5:16 am on Jan 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure exactly what you're trying to accomplish, but without an HTML form, the only way to send POST data is via fsockopen or something similar like curl. If you can post more detail, I'm sure there's a solution. Remember that a form can be nothing but some hidden fields and a submit button, and the button can be a graphic. It doesn't actually have to look like a form.

mcibor

9:59 am on Jan 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or if it's the same server, then you can even use $_SESSION to store that data - nice, clean method.

The function you provided sends data directly to another server, eg. you have external db which you can access only internally, then you can use this method.

For forwarding data on one server I recommend using other methods.

Michal

PS. But avoid using direct command: $_SESSION = $_POST, as it may be exploitable

jatar_k

11:31 am on Jan 17, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



maybe this thread from the Library might help
Single form posted to multiple locations [webmasterworld.com]

henry0

12:28 pm on Jan 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You will need to check first if your host allows it
quite a few won't (at least by default)