Jump to content

website design and communication between PHP and C++


geantbrun

Recommended Posts

Hi guys,

I'm relatively new to the domain of web programming and I need your opinion about the design of my website (server platform is Linux). The target audience is financial institutions, not too much traffic on the site but each client needs from time to time heavy calculations. Typically, the institution enters some parameters and the site would return results later (graphs, pdf file).

 

1 - I'm considering Joomla for the website design with some forms component to get user parameters filled in mysql tables.

2 - in some ways, I have to launch a program in the background (don't know how) that will do the number crunching (see below).

3 - the routine is started (but not finished) and the control returns to the the website. A message like this shows: "Thank you, you will receive an email when results are ready".

 

The step 2 could be done by a C++ program (actually, I already coded the heavy loops in this language) but I was thinking of PHP as a wrapper of the main routine in C. In PHP, it's easy to read parameters from mysql tables, create graphs (open flash charts?) and send email confirmation at the end.

 

My questions are:

- is this design good? Am I missing something?

- is it a good idea to wrap the C routine with PHP? how to launch the PHP program in the background?

- how to pass parameters from PHP to C++? Instead of having a neat solution to pass parameters, would it be easier to dump data in text files for the communication between PHP and C++? Data is not big here (and it's equally true for the input and the output) and time to read and write files is not important compared to the time for calculations itself.

 

Any hint would be greatly appreciated,

Patrick

Link to comment
Share on other sites

I've never used C++ and PHP together, so these suggestions are a bit of a shot in the dark.

 

First, I'd try to keep the web component (the Joomla site) as separate from the form processing (the C++) as possible.  Other than some simple communication ("here's the user-submitted info", "thanks, the process has started"), they shouldn't interact with one another.

 

Somehow, the C++ code needs to know when the form submission happened.  Can C++ handle/read HTTP requests?  If so, you could forgo the database all together as those values will be sent via POST.  You could intercept them right at form submission instead of storing them (unless, of course, you need to take into consideration any run-time collisions).

 

Once the process begins, the C++ code will need to send word to the website.  The easiest way would be to send another HTTP request - either through POST or GET - with a boolean value to represent the success/fail of the process initializing.

 

On the PHP side, you just need to make sure the form points to the C++ function, and that you have another bit of code than can handle the incoming success/fail value.

 

Once again, this is a bit of a shot in the dark, so take these suggestions with a pitcher of salt.

Link to comment
Share on other sites

  • 9 months later...

I know I am a bit late to this topic but here is my take on this. First of all if you have a whole lot of data to pull from the database then it would be better to have C++ grab it from the database and do all the processing from a compiled application. I have never interfaced with a database from C++, but I know for a fact that it can be done.

 

Next question up is executing the script. This is very simple with the php exec function (http://php.net/manual/en/function.exec.php). You can use this to execute code as if you were sitting at a command line. I am currently using it on several sites to simulate multi-threading in PHP to speed up the user experience at processing complex form submissions. The method I employ just causes the server to open another PHP instance from an invisible command prompt and execute more PHP code. When configured correctly you can just fire the code and let it run in the background.

 

This is actually something that Facebook does with their systems. Their engineers have written blog postings about how they converted a lot of PHP code into C++ and stored procedures so that they could run faster compiled code and then just use PHP as the front-end interface that requests information from the compiled code. (http://www.facebook.com/notes.php?id=9445547199). There are more articles than that, I actually first read about this more than 6 months ago in a completely different article.

 

Anyway, the benefits of a system like this are definitely in the performance metrics. You can harness the multi-threading capabilities of a compiled language and improve the user experience drastically. Now I am not sure how you get the data back to the user if you do the fire and run method that I use on my sites (probably an AJAX setup that has a timer checking the DB for changes every so often). But if the back-end code is fast enough you can just tell PHP to wait for the response.

 

One last note, in executing the command line program you can either pass all the needed variables via the command line or save all the variables to the database with PHP and then just pass a DB row for the back-end program to gather the data from. This is certainly a cleaner approach.

 

Anywho, just my two cents.

 

-Patrick

Link to comment
Share on other sites

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.