• 0

[PHP] Pass POST data from one page to another


Question

Hi all,

I want to PASS post data from a form to a php page and then to another php page.

At the moment it does this.

1. POST to External PHP page.

2. External PHP page displays SUCCESS or ERROR.

I don't want the user to see this.

Is there a way to implement it like so:

1. POST to local PHP page

2. POST from local PHP to external PHP.

3. Load PHP response into string.

4. If string contains word ERROR then display a message on local php page.

5. Otherwise Display "submission successful"

7 answers to this question

Recommended Posts

  • 0

Hi Axel, it sounds to me like you want an Ajax call; have you considered this? It would at least cover the requirement of letting you post data to the external PHP page and retrieving back a result without moving your user off the page.

  • 0

Yo again!

I did this by doing the following. Please note I don't know how security-risky is this. But here it goes.


<?php
session_start();

$_SESSION['post_data'] = $_POST;

?>
[/CODE]

Now, on the other page

[CODE]
<?php
session_start();
$_SESSION['post_data']/// Use this as the POST variable.
?>
[/CODE]

If you want to be sure that there data is cleared then do it this way:

[CODE]
<?php
session_start();
$_SESSION['post_data']/// Use this as the POST variable.

//After finishing the code:
unset($_SESSION['post_data']);
?>
[/CODE]

Hope this helps :)

Anybody who thinks this presents a security risk please let me know, because I'm using this technique right now. Although I'm not using a sensitive information.

  • 0

I'll clarify a little bit. I'm sending the form data to https://www.formstac...forms/index.php

I don't think the Formstack API supports JSON.

I can quite easily get a webpage into a variable using php:

  $url = "https://www.formstack.com/forms/index.php"
  function get_data($url) {
  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}

Now is it possible to somehow forward the $_POST data in the curl request so that I get the relevant data?

I hope that makes sense.

Edit: Hi Jose_49! I don't thing that technique will work on the basis that I don't have any access to the code on the https://www.formstack.com/forms/index.php page so unfortunately I can't alter it. What do you make of my suggestion above?

  • 0

This is interesting:

http://superuser.com/questions/149329/what-is-the-curl-command-line-syntax-to-do-a-post-request

curl --data "param1=value1&amp;param2=value2" http://example.com/resource.cgi

No idea how to use this though.

  • 0
  On 02/02/2013 at 17:42, Jose_49 said:

I did this by doing the following.

<snip>

You've misunderstood, one of the two pages the OP wants to send the data to is on another web service!

  On 02/02/2013 at 17:43, Axel said:

I'll clarify a little bit. I'm sending the form data to https://www.formstac...forms/index.php

I don't think the Formstack API supports JSON.

One way you could perhaps find out: grab the POSTman addon for Google Chrome and use it to send a request. Set the method to POST, place your json encoded string in the body (RAW mode), and set the 'Content-Type' header to 'application/json', then see what you get back.

  On 02/02/2013 at 17:43, Axel said:

I can quite easily get a webpage into a variable using php:

  $url = "https://www.formstack.com/forms/index.php"
  function get_data($url) {
  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}

Now is it possible to somehow forward the $_POST data in the curl request so that I get the relevant data?

This is the method I would suggest you use to do it, submitting to your own PHP script, then with that submitting to the other web service.

Assuming the web service accepts JSON encoded data, you could use the following code:

$data = json_encode($_POST);

$ch = curl_init('https://www.formstack.com/forms/index.php');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	'Content-Type: application/json'
);

$result = curl_exec($ch);

If the web service does not accept JSON:

The default encoding for form data in a POST request is application/x-www-form-urlencoded. I just made a little test script, and it does not seem that PHP keeps a copy of it in this format in a server variable. The application/x-www-form-urlencoded format is described here: http://www.w3.org/TR...tml#h-17.13.4.1. It's essentially the same as query string format. e.g. forename=foo&surname=bar, and certain characters being encoded.

Thankfully though, there is no need to translate the $_POST array into an application/x-www-form-urlencoded string, you can simply give CURLOPT_POSTFIELDS an array, and it'll sort it out for you!

$ch = curl_init('https://www.formstack.com/forms/index.php');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);

  On 02/02/2013 at 18:20, Axel said:

This is interesting:

http://superuser.com...-a-post-request

curl --data "param1=value1&amp;param2=value2" http://example.com/resource.cgi

No idea how to use this though.

Obviously this is command line usage (I'm sure you knew that, just making sure). Note that the data is in application/x-www-form-urlencoded form, as mentioned above! You could probably pass this as a string to php's exec() function, but that could very likely be extremely insecure, so I would strongly discourage it!

  • Like 2
  • 0
  On 02/02/2013 at 19:23, theblazingangel said:

Thankfully though, there is no need to translate the $_POST array into an application/x-www-form-urlencoded string, you can simply give CURLOPT_POSTFIELDS an array, and it'll sort it out for you!

$ch = curl_init('https://www.formstack.com/forms/index.php');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);

YOU SIR ARE AN ACTUAL GOD - THANK YOU SO MUCH!!!

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Yes, and the reason is the defaults is has. The masses have no interest to change settings etc. It feels cluttered by default. The default home/NTP feels cluttered with so much stuff from MSN. The sidebar has too many buttons with Microsoft services. The default search engine is Bing. Just compare Edge defaults with Chrome defaults. The masses open Edge or are "forced" to open it, they don't like what they see and close it and go back to Chrome.
    • PrivaZer 4.0.106 by Razvan Serea PrivaZer is a PC cleaner that helps you master your security and freedom at home and at work. PrivaZer permanently and irretrievably erases unwanted traces of your past activity on your computer and on your storage devices (USB keys, external drive, and so on) which prevents others from retrieving what you have done, watched, streamed, visited on internet, freeing up valuable hard disk space, and keeping your PC running secure. PrivaZer key features: Deep Cleaning: PrivaZer thoroughly cleans your PC by removing unnecessary files, traces of activity, and potential privacy risks. Advanced Scan Modes: With multiple scan modes, including Quick and Deep scans, PrivaZer ensures comprehensive cleaning tailored to your needs. Customizable Cleaning: PrivaZer allows you to customize cleaning settings, so you can choose exactly what to clean and what to keep. Privacy Protection: PrivaZer safeguards your privacy by securely erasing traces of your online and offline activities, including browsing history and temporary files. Secure File Deletion: PrivaZer securely deletes sensitive files beyond recovery, ensuring your confidential data remains private. Startup Manager: PrivaZer helps you control which programs launch at startup, improving boot times and overall system performance. Automatic Updates: PrivaZer regularly updates its cleaning algorithms to adapt to new threats and ensure effective protection. Scheduled Cleanups: PrivaZer offers the convenience of scheduling automated cleanups, so your PC stays optimized without manual intervention. Portable Version: PrivaZer offers a portable version, allowing you to carry it on a USB drive and clean any PC without installation. Detailed Reports: PrivaZer provides detailed reports after each cleanup, giving you insights into the space reclaimed and the areas cleaned. File Shredder: PrivaZer includes a file shredder feature to securely delete files, making data recovery impossible even with specialized tools. Context Menu Integration: PrivaZer integrates with the context menu, enabling quick and easy access to cleaning functions from any file or folder. Multi-Language Support: PrivaZer supports multiple languages, making it accessible to users worldwide. Automatic Traces Detection: PrivaZer automatically detects traces of activity on your PC, ensuring thorough cleaning without manual intervention. System Restore Point Creation: PrivaZer creates system restore points before cleaning, allowing you to revert changes if needed. Disk Health Analysis: PrivaZer analyzes disk health and alerts you to potential issues, helping you prevent data loss and maintain system stability. Browser Extensions Cleanup: PrivaZer cleans up browser extensions and add-ons, improving browser performance and security. File Association Management: PrivaZer helps you manage file associations, ensuring files open with the correct programs for optimal usability. Intuitive User Interface: PrivaZer features an intuitive user interface, making it easy for both novice and advanced users to optimize their PCs for better performance and privacy. PrivaZer 4.0.106 changelog: New cleanup : BAM (Background Activity Monitor) Improved cleanup : Clipboard Improved UI Download: PrivaZer 4.0.106 | Portable PrivaZer ~30.0 MB (Freeware, paid upgrade available) View: PrivaZer Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • This was cool back in the day when done properly - loved having icons of specific devices.
    • Microsoft quietly burying a massive Windows 7 hardware driver feature as Windows 11 kills it by Sayan Sen Last month Microsoft announced a big update for Windows hardware drivers. The company declared that it was killing Windows Device metadata and the Windows Metadata and Internet Services (WMIS). For those wondering what it is, device metadata, as the name suggests, is the collection of additional, user-facing information that an original equipment manufacturer (OEM) provides about a hardware device. The feature was introduced with Windows 7 and can include stuff like icons, logos, descriptive texts, among other things, that help the Windows UI display details about such devices in places like Task Manager or Device Manager. This was a huge deal back in the day when Windows 7 debuted. The company called the feature "Device Stage" and Microsoft described it as a "new visual interface" that essentially worked like a "multi-function version of Autoplay where it displays all the applications, services, and information related to your device." It is often considered synonymous with the Windows "Devices and Printers" Control Panel applet. Neowin did an in-depth overview of the feature when it first launched which you can find in its dedicated article here. The Windows OS was able to obtain the device experience metadata from the WMIS, but now that the feature is being deprecated, Microsoft has begun removing information about Device Stage from its official support documents. Neowin noticed while browsing that a support article regarding automatic Windows hardware drivers was updated for Windows 11 and 10 sometime last year after the release of Windows 11 24H2. Previously, this article was geared for Windows 7 and was much longer. It also contained information about Device Stage, which, as mentioned above, was a headlining feature on Windows 7. In the said article, the section "If Windows can't find information about your device in Device Stage" has been deleted. You can find the archived version of the support page here. Aside from shortening the amount of information on the page, Microsoft has also added some more details on it. The company has now tried to define what the Microsoft Basic Display Adapter is, how updating drivers through Device Manager works, as well as a thorough and detailed troubleshooting section for common hardware driver errors on Windows, including one for USB-C. You can find all the new details on the updated support page here on Microsoft's website.
  • Recent Achievements

    • Veteran
      Yonah went up a rank
      Veteran
    • First Post
      viraltui earned a badge
      First Post
    • Reacting Well
      viraltui earned a badge
      Reacting Well
    • Week One Done
      LunaFerret earned a badge
      Week One Done
    • Week One Done
      Ricky Chan earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      481
    2. 2
      +FloatingFatMan
      264
    3. 3
      snowy owl
      238
    4. 4
      ATLien_0
      232
    5. 5
      Edouard
      176
  • Tell a friend

    Love Neowin? Tell a friend!