Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

PHP Executed From ActiveX

0.00/5 (No votes)
4 May 2005 1  
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