Click here to Skip to main content
Click here to Skip to main content

PHP Executed From ActiveX

, 4 May 2005
Rate this:
Please Sign up or sign in to vote.
Execute PHP without having to install PHP.

Introduction

Usually, running a PHP script in .NET involves shelling to the OS and piping the response back. With Serge Perevoznyk's PHP4Delphi, Delphi developers were able to utilize the Zend (PHP) engine without having to resort to the shell while accruing some superb features, like User-Defined Functions, Variables, Constants, and Error events. PHPX is an ActiveX control to wrap this functionality. I needed it for a project and I thought I'd share it with all of you.

Requires

  • PHPXControl.ocx - an ActiveX wrapper to the PHP4Delphi library. This file must be registered in your OS using regsvr32 (at the command line, type "regsvr32 phpxcontrol.ocx").
  • php4Delphi.dll - Serge Perevoznyk's library to interpret the getting and setting of information with the Zend engine (included in ZIP).
  • php4ts.dll or php5ts.dll - The PHP Zend engine, not included in this ZIP. This file and the entire PHP engine package is freely available from the PHP Site, but all you need is php*ts.dll.

The ZIP file contains another JavaScript and a C# example.

JavaScript Example

(Of course, the ActiveX control can be used in C++, C#, and VB/VB.NET also.)

// Create the activex control

var php = new ActiveXObject("PHPXControl.PHPX");


// set the directory that php5ts.dll is located in
php.DLLFolder = "c:\\phpx\\";

// run the PHP code
var result = php.RunCode('print "Hello World!";');

// show the result
window.alert(result);

// clean up the object
php.FinalizeControl();

Using The Control

After instantiating the control, set the DLLFolder property. If this is not set, you'll receive an error that, the script engine could not start. As a general rule, I suggest you have the components listed above all in the same directory for ease of use. Creating the control more than once will expose a bug in the underlying Delphi code, so I suggest against it.

Running a script is done using a RunCode or Execute method. The difference is that, RunCode wants your code as a string where Execute wants a filename.

The coolest feature (for me) is the ability to add functions, variables, and constants to PHP. You read it right-- add functions to PHP. You can register your code function using the AddFunction method. You receive an OnFunctionExecute event when your method is fired from within PHP, and you can return information to PHP using the ReturnValue reference variable.

At the end of your application or script, you also need to call the FinalizeControl method to free the memory in the underlying code.

Responsible Usage

(Read as "using this for good and not evil")

Use at your own risk.

This is very useful and at the same time, potentially harmful functionality.

Some points about browsers and this control:

  1. The ActiveX control only works in browsers that support them, namely the Internet Explorer.
  2. Even in IE, you must specifically allow ActiveX controls.
  3. I implore you to never freely enable running ActiveX controls. Instead, I suggest allowing them only on a trusted site, and then only at a prompt setting.

Conclusion

I hope you enjoy the code, and please pardon some of the messy extra properties in the control. I admit, I was a little lazy here. Delphi requires that you select a visual control in order to quickly make an ActiveX control, so you get some of the baggage along with it. If I have a few requests, I'll re-create the project without the extras. I will maintain a link for the latest version at angorasoftware.com

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Daaron
Web Developer
United States United States
Raised by wolves in the mean streets of Macintosh C/C++ for IBM and Motorola, moved on to Delphi and now C#. Ah, the mother's milk of Microsoft...

Comments and Discussions

 
General.NET C# wrapper Pinmembermokaddim17-Jun-07 5:45 
GeneralPhp enguine is not active Pinmemberporfirio_ribeiro13-Nov-06 5:55 
I always get that message
And i have it all right, i puted the dll's and ocx in php directory and there registed the activex but it dont work...
QuestionRe: Php enguine is not active Pinmemberbugmenotftw26-May-07 10:24 
AnswerRe: Php enguine is not active Pinmemberbugmenotftw26-May-07 11:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 4 May 2005
Article Copyright 2005 by Daaron
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid