Author Topic: Want to call php as a module in my home-grown server  (Read 23575 times)

0 Members and 1 Guest are viewing this topic.

Offline tekaweniTopic starter

  • Irregular
  • Posts: 4
    • View Profile
Want to call php as a module in my home-grown server
« on: January 26, 2008, 10:27:39 PM »
Hi all, theres a lot of info about how to configure php as a module, but my scenario is one where I have to implement php from scratch into a web server.

Can anyone point me in the right direction?

Thanks for all input!

Offline tekaweniTopic starter

  • Irregular
  • Posts: 4
    • View Profile
Re: Want to call php as a module in my home-grown server
« Reply #1 on: January 27, 2008, 05:28:27 PM »

Sorry, badly phrased.... I meant I need to actually code the hooks (c/c++) into the web server, and need to know how variables like superglobals are passed to php in both cgi and module modes. Stdin/stdout? Environment variables? Has any of you done similar?

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,255
    • View Profile
Re: Want to call php as a module in my home-grown server
« Reply #2 on: January 28, 2008, 09:43:48 AM »
Quote
Has any of you done similar?

Not me personally but the php devs have. I would suggest downloading php's source and having a look at that.

Offline laffin

  • Devotee
  • Posts: 1,196
    • View Profile
Re: Want to call php as a module in my home-grown server
« Reply #3 on: January 28, 2008, 09:56:22 AM »
there are no php hooks, as u say.
php is an executable.
u can make a module to export environment variables for php to access.
which u can get from <?php phpinfo();?>

now fastcgi with php is something altogether different, as it keeps a copy of php-cgi in memory. i dunno how the interface works, but u wud do good looking at this module from apache.

Offline tekaweniTopic starter

  • Irregular
  • Posts: 4
    • View Profile
Re: Want to call php as a module in my home-grown server
« Reply #4 on: January 28, 2008, 05:25:53 PM »
Thanks Thorpe and Laffin, thats a big help. So PHP source is publicly available - and Apache too I see, for calling semantics - I hadn't realised that either would be.

Ultimately I'll be writing to load php as a module (dll) rather than cgi as its quicker and cleaner. I'll be happy to share my experiences (and code) with anyone who may be interested.

Cheers
Kim

Offline laffin

  • Devotee
  • Posts: 1,196
    • View Profile
Re: Want to call php as a module in my home-grown server
« Reply #5 on: January 28, 2008, 05:49:16 PM »
Actually i was looking for a way to do this as well, but from what i have researched, a few others have tried and there was no successful end result. It wud be great to have php as an embedded language like lua.
but ya may want to look at lua as an alternative if you will be embedding a scripting language into yer application.
anoter project u may look at is RoadSend PHP compiler although not complete it looks promising

Offline tekaweniTopic starter

  • Irregular
  • Posts: 4
    • View Profile
Re: Want to call php as a module in my home-grown server
« Reply #6 on: February 07, 2008, 09:17:24 PM »
Report back: Ok I have php working as cgi from my server (written in c). I could find so little documentation on this that my solution is inevitably a hack. Here's how my server handles php requests, using test.php?argone=11111&argtwo=22222 as an example -

Set an environment variable "phpvars" with the name of the script you want to call, plus the arguments passed from the browser. For example the example string would create phpvars="php=test.php&argone=11111&argtwo=22222"

Create two pipes, hooking your server's stdout to the stdin end of the php binary you're going to spawn and your stdin to the stdout of the other's stdin. (see http://msdn2.microsoft.com/en-us/library/ms682499(VS.85).aspx for a surprisingly decent howto)

Call the php binary (CreateProcess) giving it the name of a preprocessor script (see below). This script creates superglobals from "phpvars" and then runs the script (test.php) you ultimately wanted to run.

All echos are fed back to the server's stdin, etc etc.

Like I said, a hack. I haven't bothered to set $_GET's from the environment var and I dont pass any $_POST's through stdin because right now I dont need em, but that would be trivial to add.

Here's a basic preprocessor script with no error checking....


<?php
// Set up the superglobals
$fileToLoad = '';
$envs = getenv('phpvars');
$env = explode("&", $envs);
for ($c = 0; $c < sizeof($env); $c++)
{
   $envVar = explode("=", $env[$c]);
   $_REQUEST[$envVar[0]] = $envVar[1];
   if ($envVar[0] == 'php')
      $fileToLoad = $envVar[1];
}
if ($fileToLoad != '')
   include($fileToLoad);
?>

Offline ekinabby

  • Irregular
  • Posts: 1
    • View Profile
Re: Want to call php as a module in my home-grown server
« Reply #7 on: February 08, 2008, 06:49:20 AM »
err..

anything missing there?
or should i modified somthing?

coz i use that script but unsuccessful...

donno how to edit that   :-[

Offline deadimp

  • Enthusiast
  • Posts: 185
    • View Profile
Re: Want to call php as a module in my home-grown server
« Reply #8 on: February 25, 2008, 02:42:52 PM »
Are you making sure that PHP is running as a CGI module?
I think if you do that then PHP will automatically parse the query string and delegate it to all of the superglobals.
However, I haven't dealt much with manually setting up my own server, and I'm not that familiar with the down 'n dirities of PHP.
deadimp.org
> Thacmus - Open source CMS, PHP 5, MySQL

Beginner Tips:
. The more effort you show, the more effort others are likely to put into helping you.
. Think your problems through. Use a little logic and in almost every case you will be able to find what's going on. . References abound. Google's your friend, and manuals aren't hard to find.