0

I have a few php pages that might want to access the database and insert data into it. Now, I have a PHP script (save_data.php) that does the insert. It gets the input via POST then insert the data into database.

I want the other pages to be able to call/hit the save_data.php and send parameters via POST so that those pages dont have to directly access the database. After hitting the save_data.php, the other pages, must resume loading their own HTML codes.

I've read about cURL in PHP and using WebServices but Im not sure which one is appropriate or is there any other way? Thanks.

John Ng
  • 869
  • 3
  • 15
  • 28
  • curl example: http://stackoverflow.com/questions/2440252/php-curl-i-need-a-simple-post-request-and-retrival-of-page-example this is the quick way to handle this. Web services may be more appropriate if other developers at other companies need to use this – codercake Feb 15 '12 at 23:54

2 Answers2

1

Why don't you just include the script and call a function in it?

UPDATE: based on your comments below, I'd recommend some sort of web service and then the "pages" that need access to the save can use cURL.

Gregg
  • 34,973
  • 19
  • 109
  • 214
  • the other pages might be in different servers.. and the other pages could be developed by other people uploaded to the site, and we just want to provide access to the database through a link – John Ng Feb 15 '12 at 23:51
  • You need to be clearer in your question then. You didn't mention any of this. – Gregg Feb 15 '12 at 23:54
  • sorry about that.. by using webservice, does it mean I need to use SOAP? – John Ng Feb 15 '12 at 23:56
  • That is entirely up to you. I'd recommend something RESTful, but that is just my opinion. – Gregg Feb 16 '12 at 14:03
1

Based on your comments on other answers, it sounds like you want to provide access to the your save scripts to other servers and developers. The standard best practice way to do this is to offer a Web Service. Either an API that can be accessed via cURL, REST, or maybe even expose a SOAP server.

There's an old school trick to do some of these things, though, if you're looking for a quick and easy way to expose this script. You can use image embedding in HTML, kind of like how old school site counters work.

The page includes an image:

<img src="http://yoursite.com/your_script.php" />

And your_script.php does your database write logic, and responds with the appropriate MIME type, plus the data of a blank GIF.

This is used more often to share cookies across multiple domains and services using multiple types of technologies, but could work in your case.

I'm not advising its the BEST way to go. But it could work for you.

Edit

Another reason I outlined this solution is because it does not require PHP on the initial page. When your question stated that the pages may be uploaded by users, I got heart palpitations thinking about user uploaded PHP including your PHP. Please don't let users upload and execute PHP code. Please oh please!

Casey Kinsey
  • 1,451
  • 9
  • 16
  • i think web service is the way to go. What do you mean by not letting them execute php code? – John Ng Feb 16 '12 at 00:15
  • When you said, "the other pages could be developed by other people uploaded to the site," I got (perhaps mistakingly) the impression that you were going to allow untrusted users to upload their own PHP pages on your server, and that is a gigantic security vulnerability. – Casey Kinsey Feb 16 '12 at 00:23