469,336 Members | 5,240 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

Re-using POST data

25
Hi. I'm new to PHP but I'm making progress.

However, I'd like to know if I can use all the data in the $_POST array and send to the next page.
Essentially, I perform a check of the content (which can vary), before sending it on to another page which will handle the real processing of the required information.

Can this be done easily, or do I have to extract all the individdual paramteres and re-process them somehow to pass on to the next page.

My Post data contains nested arrays.

What are your recomendations?

thanks
Adrian
Aug 29 '06 #1
17 5842
vssp
268 100+
I f u want to get the variable multiple page using SESSION to get the value all the pages
Aug 29 '06 #2
ronverdonk
4,258 Expert 4TB
Actually, there are 3 ways to accomplish this:
  • Store the variables in the $_SESSION array. That way the called script can retrieve them also via the $_SESSION array. To use this, you must have called session_start() in order to be able to use the array.
  • Use the $_GET array. In this case you construct a URL of the script to be called with the parameters concatenated, e.g. "form.php?v1=var1&v2=var2 etc". The form.php script can now retrieve the values via the $_GET array.
  • Use the $_POST array. To use this you also construct the URL as in the previous, but now you pass it to CURL with the POST option. The receiving form.php can now access the values via the $_POST array.

Take your pick!

Ronald :cool:
Aug 29 '06 #3
Banfa
9,064 Expert Mod 8TB
  • Use the $_POST array. To use this you also construct the URL as in the previous, but now you pass it to CURL with the POST option. The receiving form.php can now access the values via the $_POST array.
Ronald, could you go into a little more detail about this method please? :-)
Aug 29 '06 #4
ronverdonk
4,258 Expert 4TB
Glad to, Banfa. I'll just start with the definition given by Zend:
cURL and libcurl are libaries that allow a webserver to transfer files with a remote computer using a variety of Internet protocols. The libaries are highly configurable, allowing practically any type of client-server request to be peformed. By using these tools, a webserver can act as a client, creating and responding to requests using any technology built on HTTP, like XML-RPC, SOAP, or WebDAV.
And one by Wikipedia:
cURL is a command line tool for transferring files with URL syntax, supporting FTP, FTPS, HTTP, HTTPS, TFTP, Telnet, DICT, FILE and LDAP. cURL supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading, Kerberos, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM and Negotiate for HTTP and kerberos4 for FTP), file transfer resume, http proxy tunneling and many other features. cURL is open source/free software distributed under MIT License.

The main purpose and use for curl the command line tool is to automate unattended file transfers or sequences of operations. It is for example a good tool for simulating a user's actions at a web browser.

Libcurl is the corresponding library/API that users may incorporate into their programs; cURL acts as a stand-alone wrapper to the libcurl library. libcurl is being used to provide URL transfer capabilities to numerous applications, Open Source as well as many commercial ones.

More than 30 different language bindings are available for libcurl.
Curl is free and open software that compiles under a wide variety of operating systems. Currently there are approx. 35 language bindings available.

Following addresses the reply I gave in this forum about POSTing via CURL. Just 2 samples (sending and receiving) that I got from the Zend site and use myself (adapted to my site):
[PHP]-------------- POST variables via curl ----------------------------------
<?php
//
// A very simple PHP example that sends a HTTP POST to a remote site
//

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"http://www.mysite.com/tester.phtml");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"postvar1=value1&postvar2=value2&postvar3=value3") ;
curl_exec ($ch);
curl_close ($ch);
?>
------------------ and this is to test the $_POST array ---------------
<?php

echo '<h3>Form variables I received: </h3>';

echo '<pre>';
print_r ($_POST);
echo '</pre>';

?>[/PHP]
There is quite some information about cURL, see:

The developers of Curl (Daniel Stenberg and 500 others): http://curl.haxx.se/libcurl/php/
PHP site: http://nl3.php.net/manual/en/ref.curl.php
Zend site: http://www.zend.com/pecl/tutorials/curl.php
PHP-It site: http ://www.phpit.net/article/using-curl-php/
PHP Freaks site: http://www.phpfreaks.com/tutorials/49/1.php
Higherpass site: http://www.higherpass.com/php/tutori...ote-Servers/1/ and http://www.higherpass.com/php/Tutori...emote-Servers/
Yahoo site: http://developer.yahoo.com/php/howto-reqRestPhp.html
bobcares.com: http://bobcares.com/article47.html

But, when you look around (e.g. Google, Yahoo), you'll find many more stuff on the net.

When you want, I can send you a Word document "CURL: Client URL Library" by Mr. MA Razzaque Rupom.

Ronald :cool:
Aug 29 '06 #5
iam_clint
1,208 Expert 1GB
you don't have to go as far as that just to retrieve these values my friends...


use hidden text fields in a form if your using a form.

if your not using a form make the links call the form to submit.
Aug 29 '06 #6
ronverdonk
4,258 Expert 4TB
That was not the point. Question was just how to pass data from one page to another. Making an extra form requires the user to click an extra link, and that was not the point here.
Passing it via the URL exposes your parameter data to an outside onlooker.

Posting it without using a form is the issue here, hence CURL.

Ronald :cool:
Aug 29 '06 #7
adriann
25
thanks all for your detailed replies.
I'll have to do some research on these approaches due to inexperience, but thanks for guiding me in the right direction.
Aug 29 '06 #8
RonS
4
Wow great thread. I actually have a similar problem.

While running a php script on user-supplied info from a form, I need to send some of that data to a php script running on another server (which does a database lookup), and get back a response.

This is the way that I've thought of (and successfully tried) doing it

[php]<?php
$foo = urlencode($foodata);
$bar = urlencode($bardata);

$response = file_get_contents("http://example.com?foo=$foo&bar=$bar");
if ($response == $checkVal)
.
.
.
?>[/php]And this works very nicely, thank you! However, I am concerned about the contents of the variables, they are originally part of user input, so I put them through urlencode() first, which I'd rather not do. Is there any way I can put these variables into a POST action and get back the results with code that is as simple (or as close to as simple) as the above snippet???

Thanks so much for your help.
Sep 13 '06 #9
iam_clint
1,208 Expert 1GB
your mistaking my post ronverdonk you can have javascripts submit the form using POST method which would in turn not put it in the address thanks have a good day.
Sep 13 '06 #10
ronverdonk
4,258 Expert 4TB
Okay iam_clint, you are right. It is just that I don't really like JavaScript or any other client-side routines.

Ronald :cool:
Sep 13 '06 #11
iam_clint
1,208 Expert 1GB
:) i am a javascript whore..
Sep 14 '06 #12
RonS
4
Is there any way I can put these variables into a POST action and get back the results with code that is as simple (or as close to as simple) as the above snippet???
Anybody have a clue on how to help me accomplish my goal?

I need to get back a response from the second server. File_get_contents() handles that quite nicely for me, and of course allows me to pass the variables in a GET action.

Any code or functions that someone can steer me towards that would be almost as elegant as file_get_contents, allow me to read the page returned, yet allow me to use the POST method?

Thanks again in advance.
Sep 15 '06 #13
ronverdonk
4,258 Expert 4TB
Ron S: I'll have to route you to a link to a 5-page tutorial that explains using CURL to query remote servers http://www.higherpass.com/php/tutori...ote-Servers/1/

Ronald :cool:
Sep 15 '06 #14
ronverdonk
4,258 Expert 4TB
:) i am a javascript whore..
Iam_clint: now you've made me curious. Could you show me a link that explains using JS to POST data? Thanks.

Ronald :cool:
Sep 15 '06 #15
RonS
4
Uhhh Thanks Ronald,

But sending me to a 5 page, poorly written "tutorial" on curl that doesn't answer the question that I asked and showed an example using urlencode which I specifically said I didn't want to do doesn't quite satisfy my needs! lol :D

I can RTFM too, I was hoping someone with experience could make my life a little easier today (or 2 days ago).

Next time, please just say "I don't know" or nothing instead of sending me on a wild goose chase. I already have wild turkeys riunning around in my backyard, that's fowl enough for me. ;)

Thanks.
Ron.
Sep 15 '06 #16
ronverdonk
4,258 Expert 4TB
Obviously, you have no intention of being shown the way to get to the solution of your problem. You also do not want to invest any time in acquiring new knowledge and learning anything new, such as CURL (which would actually help you achieve your goal).

Nosir, not for you: you want the answer, you want it in code, and you want it now! And if you don't get exactly that, you blame others for your failure to comprehend.

Ronald :cool:
Sep 16 '06 #17
RonS
4
Hey dude,

In my last job I was head of 3 departments, before that various shades of programming manager for years, a development dude for many years before that. I've probably hired, trained, managed and groomed more professional coders than you've known in your life, even though you claim 37 years of coding.

I know how to read the manual, I even know the URL for php.net. Further, I went as far as to publish my own devised method for accomplishing the task and was clearly asking if there was a better way, not just a different and completely less elegant way. I stopped in, registered and asked for help because I was googling this problem and found this thread with a very similar situation. I was hoping the person giving advice was actually qualified.

You sent me to a completely bull.... site with bad examples, no useful commentary, specifically didn't answer my question and did exactly what I stated I didn't want to do. Tell me, how were you helpful? ..and now you're offended because I called you on it?

I can read the manuals, and I can assemble a series of php functions that will get the job done, but I'd to see some examples from others in how they would accomplish such a task, because over the years I've actually learned that I'm not always right.

Not insignificantly, curl will almost certainly not be installed, compiled into php, or available to be used on all of my client's machines.

Finally, If you just want to be an index for wikipedia, how about posting a link to something of quality like this?

[php]//Here's a slightly modified version of sendtohost():

/* 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;
}[/php]
Source: http://www.faqts.com/knowledge_base/...id/12039/fid/5

Or perhaps something like this:
[php]
<?php
function post_it($datastream, $url) {
$url = preg_replace("@^http://@i", "", $url);
$host = substr($url, 0, strpos($url, "/"));
$uri = strstr($url, "/");
$reqbody = "";
foreach($datastream as $key=>$val) {
if (!is_empty($reqbody)) $reqbody.= "&";
$reqbody.= $key."=".urlencode($val);
}
$contentlength = strlen($reqbody);
$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".
"$reqbody\r\n";
$socket = fsockopen($host, 80, $errno, $errstr);

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

fputs($socket, $reqheader);

while (!feof($socket)) {
$result[] = fgets($socket, 4096);
}

fclose($socket);

return $result;

}

?> [/php]USING THE FUNCTION:[php]<?php

$data["foo"] = "some";
$data["bar"] = "data";

$result = post_it($data, "http://www.zend.com/test/test.php3");

if (isset($result["errno"])) {
$errno = $result["errno"];
$errstr = $result["errstr"];
echo "<B>Error $errno</B> $errstr";
exit;
} else {

for($i=0;$i< count($result); $i++) echo $result[$i];

}

?> [/php]
Source: http://www.zend.com/zend/spotlight/mimocsumissions.php

But like I said, I was looking for an elegant solution.

So, do you understand my unhappiness at your response?

Good luck in your endeavors.
Ron.
Sep 16 '06 #18

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Nel | last post: by
4 posts views Thread by Craig Bailey | last post: by
11 posts views Thread by James | last post: by
4 posts views Thread by Alan Walkington | last post: by
1 post views Thread by John Ryan | last post: by
1 post views Thread by joost | last post: by
1 post views Thread by Clarice Almeida Hughes | last post: by
2 posts views Thread by JW | last post: by
2 posts views Thread by Frans Schmidt | last post: by
2 posts views Thread by sky2070 | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.