tags:

views:

61

answers:

3

I am writing a small web server, nothing fancy, I basically just want to be able to show some files. I would like to use PHP though, and im wondering if just putting the php code inside of the html will be fine, or if I need to actually use some type of PHP library?

http://www.adp-gmbh.ch/win/misc/webserver.html

I just downloaded that and I am going to use that to work off of. Basically I am writing a serverside game plugin that will allow game server owners to access a web control panel for their server. Some features would be possible with PHP so this is my goal. Any help would be appreciated, thanks!

+2  A: 

The PHP won't serve itself. What happens in a web server like Apache is before the PHP is served to the user it is passed through a PHP parser. That PHP parser reads, understands and executes anything between (or even ) tags depending on configuration. The resultant output, usually still HTML, is served by the web server.

There are a number of ways to achieve this. Modules to process PHP have been written by Apache but you do not have to use these. PHP.exe on windows, installed from windows.php.net, will do this for you. Given a PHP file as an argument it will parse the PHP and spit the result back out on the standard output.

So, one option for you is to start PHP.exe from within your web server with a re-directed standard output to your program, and serve the result.

How to create a child process with re-directed IO: http://msdn.microsoft.com/en-us/library/ms682499%28VS.85%29.aspx however, you won't be writing the child process, that'll be PHP.exe

Caveat: I am not sure from a security / in production use perspective if this is the most secure approach, but it would work.

Ninefingers
Thank you for the great response!
Brett Powell
No problem, good luck and remember you can implement Python and other languages in this way too.
Ninefingers
Also take a look at FastCGI libraries, these may help you http://www.fastcgi.com/drupal/ should you want to make a production server.
Ninefingers
+1  A: 

PHP needs to be processed by the PHP runtime. I'm assuming the case you're talking about is that you have a C++ server answering HTTP queries, and you want to write PHP code out with the HTML when you respond to clients.

I'm not aware of any general-purpose PHP library. The most straightforward solution is probably to use PHP as a CGI program.

Here's a link that might be useful for that: http://osdir.com/ml/php-general/2009-06/msg00473.html

This method is nice because you don't need to write the HTML+PHP out to a file first; you can stream it to PHP.

DK
Yes you are correct, My c++ server is answering queries, and I want to display php with the html. I have never heard of using PHP as a CGI program, I will take a look at that link you provided, thank you!
Brett Powell
+1  A: 

You need execute the PHP page to serve the page it generates.

The easiest thing for you to do would be to add CGI support to your webserver in some basic form. This is non-trivial, but not too difficult. Basically you need to pass PHP an environment and input, and retrieve the output.

Once you have CGI support you can just use any executable, including PHP, to generate webpages.

McPherrinM