Jump to content

Recommended Posts

Hey,

 

page.php contains some $_POST array data that I want automatically passed on to page1.php, without the use of a form. So..........

 

On page.php I formed a post request in a variable like so:

        $host = "localhost";

        $uri = "page1.php";

        $data = $_POST['data1'];

$contentlength = strlen($data);

        $reqheader =  "POST $uri HTTP/1.1\r\n".

    "Host: $host\n". "User-Agent: PostIt\r\n".

            "Content-Type: application/x-www-form-urlencoded\r\n".

            "Content-Length: $contentlength\r\n\r\n".

            "$data\r\n";

 

Now what do I do with this? I tried opening page1.php and writing the post request to it:

$handle = fopen($url, "r");

fwrite($handle, $reqheader);

 

But now what? At this point I want to go to page1.php and see the posted data there, just as I would with a form with an action of "page1.php". Any help is much appreciated!

 

Thanks.

Hey chronister,

 

I dont want to use sessions as it will complicate things for me, considering my script, plus then I become dependent on cookies, which I dont want to be. I dont want to pass it in the URL as a GET because I dont want this data to be seen by users.

 

Ive got the solution though, to use cURL, which is luckily available on my hosts server, but I think its available on most.

 

<?
$myURL="http://www.remoteSite.com/login.php";
$chttp = curl_init();
curl_setopt($chttp, CURLOPT_URL,"$myURL");  //URL to post the data to
curl_setopt($chttp, CURLOPT_POST, 1);
curl_setopt($chttp, CURLOPT_POSTFIELDS, "key1=value1&key2=value2");  //POST names (key1, key2) and their respective values
curl_exec ($chttp);
curl_close ($chttp);
?> 

 

Ive also found a way to do this without cURL, as seen in this link:

http://www.zend.com/zend/spotlight/mimocsumissions.php

 

Although I cant seem to get this one working..

 

mkosmosports

 

 

just one question would this be causing problems?

$host = "localhost";

        $uri = "page1.php";

        $data = $_POST['data1'];

  $contentlength = strlen($data);

        $reqheader =  "POST $uri HTTP/1.1\r\n".

            "Host: $host\n". "User-Agent: PostIt\r\n".

                  "Content-Type: application/x-www-form-urlencoded\r\n".

                  "Content-Length: $contentlength\r\n\r\n".

                  "$data\r\n";

 

Now what do I do with this? I tried opening page1.php and writing the post request to it:

  $handle = fopen($url, "r");

  fwrite($handle, $reqheader);

 

just a thought

Yes that would certainly be causing problems blueman. Thanks. Ive got some more changes coming, and I believe I can now successfully send the post request (Thanks to that Zend article!), but Im still failing to redirect to that page at the same time just as a form post script would. (I know how to do it with cURL but I want to know and understand this way as well) So, this is test.php...

 

//Sending the deal URL through a POST to test1.php
function post_it($datastream, $url)
{
        //Replace 'http://' with the empty string
//Get the Host address substring
//Get the URI of the desired Resource
$_url = preg_replace("@^http://@i", "", $url);
$host = substr($_url, 0, strpos($_url, "/"));
$uri = strstr($_url, "/");

        //Initialize the $reqbody variable
        //Traverse the $DataStream array
        //If $reqbody is not empty, append a ampersand sign '&'
        //Append the variable/value pair to the end of $reqbody
        $reqbody = "";
        foreach($datastream as $key=>$val)
        {
            if (!empty($reqbody)) $reqbody.= "&";
    		$reqbody.= $key."=".urlencode($val);
        }

//Construct the POST request to send to the desired server.
$contentlength = strlen($reqbody);
	 $reqheader =  "POST $uri HTTP/1.0\r\n".
				   "Host: $host\n". "User-Agent: PostIt\r\n".
	 "Content-Type: application/x-www-form-urlencoded\r\n".
	 "Content-Length: $contentlength\r\n\r\n".
	 "$reqbody\r\n";

//Connect to the server using fsockopen()
//If there was a connect failure, return in error
//Send the POST request to the server
$socket = fsockopen($host, 80, $errno, $errstr);

if (!$socket)
{
   $result["errno"] = $errno;
   $result["errstr"] = $errstr;
   return $result;
}

fputs($socket, $reqheader);
fclose($socket);
return $result;
}
//Now to send the below array, using the post_it function above
$_data["d1"] = "I want to send this data over";
$result = post_it($_data, "http://localhost/test1.php");

 

This seems to workNow how do I get it to redirect to "http://localhost/test1.php" like a normal form post request would do??

 

Thanks for any suggestions!

sorry if u have already fixed it but i figure if u havnt ill point it out u have the same thing here:

//Sending the deal URL through a POST to test1.php

function post_it($datastream, $url)

{

        //Replace 'http://' with the empty string

//Get the Host address substring

//Get the URI of the desired Resource

$_url = preg_replace("@^http://@i", "", $url);

$host = substr($_url, 0, strpos($_url, "/"));

$uri = strstr($_url, "/");

 

        //Initialize the $reqbody variable

        //Traverse the $DataStream array

        //If $reqbody is not empty, append a ampersand sign '&'

        //Append the variable/value pair to the end of $reqbody

        $reqbody = "";

        foreach($datastream as $key=>$val)

        {

            if (!empty($reqbody)) $reqbody.= "&";

    $reqbody.= $key."=".urlencode($val);

        }

 

//Construct the POST request to send to the desired server.

$contentlength = strlen($reqbody);

$reqheader =  "POST $uri HTTP/1.0\r\n".

  "Host: $host\n". "User-Agent: PostIt\r\n".

"Content-Type: application/x-www-form-urlencoded\r\n".

"Content-Length: $contentlength\r\n\r\n".

"$reqbody\r\n";

 

//Connect to the server using fsockopen()

//If there was a connect failure, return in error

//Send the POST request to the server

$socket = fsockopen($host, 80, $errno, $errstr);

 

if (!$socket)

{

  $result["errno"] = $errno;

  $result["errstr"] = $errstr;

  return $result;

}

 

fputs($socket, $reqheader);

fclose($socket);

return $result;

}

//Now to send the below array, using the post_it function above

$_data["d1"] = "I want to send this data over";

$result = post_it($_data, "http://localhost/test1.php");

 

or is it intentional?

if so please tell me why

Hey blueman,

 

In this case it is intentional yes. The uri variable holds the part of the url after the domain, so the path/directory to the page name (including that page name as well). This is actually pretty confusing on my part as the uri IS the full url, but in this case the above is what this variable does.

 

mkosmosports

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.