1

Alright, so I've looked at a ton of questions, but I only found 1 that resembled what I am trying to do. Here is the link to it: Passing POST data from one web page to another with PHP

I want to pass data from one PHP file(we'll call it editData.php) to another PHP file(we'll call it submitData.php). Neither file has any HTML elements (pure PHP code I mean). The first file(editData.php) receives $_POST data, edits it, and needs to send it to the second file. The second file(submitData.php) needs to be able to read in the data using $_POST. No sessions nor cookies can be used I'm afraid.

In the linked question above, the answer accepted was to create hidden fields inside a form and POST the data from there. This worked for the OP because he had user interaction on his "editData.php", so when the user wanted to go to "submitData.php", he would POST the data then.

I can't use this solution(at least, I don't think I can), because I am accessing (and sending $_POST data to) editData.php from a javascript AJAX call and there will be no user interaction on this page. I need the modified data to be POSTed by code, or some other way that does the transfer 'automatically'(or 'behinid-the-scenes' or whatever you want to call it). submitData.php will be called right after editData.php.

I don't know if I can rewrite submitData.php to accept GET data, so count that out as well (it's a matter of being able to access the file). I really don't want to echo stuff back to my original JavaScript function(and then AJAX again). I am encrypting info in editData.php, and (while it sounds silly to say it) I don't want to make it easy for someone to develop a cipher for my encryption. Returning values after being encrypted(viewable with Inspect Element) would make it too easy to decipher if you ask me.

I feel like this issue could come up a lot, so I'd expect that there is something obvious I'm missing. If so, please tell me.

tl;dr? How can I send data to a PHP file via the POST method while only using code in another PHP file?

Community
  • 1
  • 1
cNovak
  • 94
  • 1
  • 9
  • 1
    You could use [PHP curl](http://us2.php.net/manual/en/book.curl.php). – Waleed Khan Jul 18 '12 at 19:52
  • Do I need to install anything for that? Someone told me it was an external library. – cNovak Jul 18 '12 at 19:53
  • 1
    Do they have to be seperate PHP files? AFAIK you may end up running into AJAX problems as it will be requesting a specific PHP file; and the data is going to be returned from another. (_In other words, I'm not sure if XMLHttpRequest will work with one PHP file calling another which will actually return the data; it's not liking working with a browser that can be redirected_) – Fergus In London Jul 18 '12 at 19:53
  • Yes, they have to be separate files. I don't need to return anything from the files, so don't worry about AJAX getting incorrect responses. – cNovak Jul 18 '12 at 19:55
  • @user1454408 There's a good chance that your server already has curl installed. Try a `curl_init()` and see if your server outputs an error or not. – Waleed Khan Jul 18 '12 at 19:55
  • Can't you just have editData.php POST the data directly to submitData.php, e.g. http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/ – ernie Jul 18 '12 at 19:56
  • Can't you just include `submitData.php` at the end of `editData.php`? – jeroen Jul 18 '12 at 19:56
  • 1
    @jeroen Using "include(submitData.php);" doesn't send POST data. – cNovak Jul 18 '12 at 20:01
  • @ernie Thanks for the article, I haven't seen this method before. It looks promising. – cNovak Jul 18 '12 at 20:03
  • @user1454408 No, it does not *send* anything but the whole `$_POST` array and all other global variables will be available just like in the file that includes it... – jeroen Jul 18 '12 at 20:03
  • If you want to use a library for url you could alos use Zend or Guzzle HttpClient to post FORM data to an url. Here are both versions I used to simulate a multipart form POST: https://gist.github.com/webdevilopers/c9e1b1f53a573cb0cfdd – webDEVILopers Aug 11 '14 at 14:03

1 Answers1

4

Well you might consider just streamlining your approach and including the submitData logic at the end of the editData file. But assuming that this is not possible for some reason (files live on different systems, or whatver), your best bet might be to use cURL functionality to post the data to the second script.

If the files are on the same server though I would highly recommend not posting the data to the second script as this will basically just double the amount of requests your web server needs to handle related to this script.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103
  • +1 Your code should be modular so that you can include the logic from submitData.php easily on other pages like editData.php. – Xeoncross Jul 18 '12 at 19:57
  • @Xeoncross My submitData.php is usually called via JS AJAX, but in this instance I need it accessible from a PHP file. – cNovak Jul 18 '12 at 20:01
  • @user1454408, and that is my point. Your designed the system without proper separation of components which now places you in this tough situation where you can't easily use logic from one place in another place. – Xeoncross Jul 18 '12 at 20:04
  • Yes. But with good design, you could have all your logic for submitting data in its own include file which could simply be included at the end of editData.php or submitData.php to due the submitting. In other words if AJAX call to editData.php occurs, it reads the posted data, does whatever it needs to do, and then executes the submit include file. If the AJAX call goes directly to submitData.php, then that files reads the posted data and does whatever it needs to do before also include the submit include file. Same functionality used from two different places. – Mike Brant Jul 18 '12 at 20:05
  • I actually inherited this system from some guy who quit. Trust me, I'd redesign it if I could. I would like to do what you are describing Mike, just having some trouble getting it to do that. Say that I did include a submitting file at the end of "editData.php", how would you suggest I pass my data to it? – cNovak Jul 18 '12 at 20:10
  • I'm probably going to use the cURL functions. Turns out my server has them already, so thanks for the suggestion. – cNovak Jul 18 '12 at 20:12
  • You would not need to pass data to it at all. Whatever values are available in the global scope would be available to the included file. This would include the initial $_POST superglobal assuming you have not unset it at some intermediate point. – Mike Brant Jul 18 '12 at 20:12
  • Yes, but I am not merely transferring $_POST values. I am editing the $_POST values and then I need to submit them. I store them in separate variables for the editing part. Can I put them back into $_POST somehow(like "$_POST['abc'] = $editedVar" or it's equivalent)? – cNovak Jul 18 '12 at 20:22
  • if yo are editing the $_POST value in place (i.e not reading the post data into a different variable, then they will reflect the changed values in the including script. If you are using separate variable (which is usually better practice), then you can simply overwrite $_POST with the modified variable values. – Mike Brant Jul 18 '12 at 20:25
  • Alright, thanks! I didn't realize you could do "$_POST['whatever'] = $var". That's actually all I wanted. – cNovak Jul 18 '12 at 20:36
  • No problem. Yes superglobals can be treated just like any other variable with regard to setting them and unsetting them. It is generally just bad practice, IMO, to modify those (with exception of $_SESSION which is really expected to be modified). Generally I get request superglobals into other variables and sanitize them right at the point where they are first needed in the app, and then never use them again, instead working with the sanitized data. – Mike Brant Jul 18 '12 at 20:58