Click here to Skip to main content
15,884,388 members
Articles / Programming Languages / PHP
Article

PHP Executed From ActiveX

Rate me:
Please Sign up or sign in to vote.
3.91/5 (4 votes)
4 May 20053 min read 63.6K   891   14   4
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.)

JavaScript
// 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


Written By
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 Pin
mokaddim17-Jun-07 5:45
mokaddim17-Jun-07 5:45 
Hi,
I know that php5ts.dll is the main php core Engine.
I want to marshal this with C# and create a C# wrapper.
There are some methods as far I know, are responsible for executing php.
One of these is 'php_execute_script' or something similar like this.
But I dont know the paramters of these method. do you have any Idea about this.

Everything is possible in programming and technology

GeneralPhp enguine is not active Pin
porfirio_ribeiro13-Nov-06 5:55
porfirio_ribeiro13-Nov-06 5:55 
QuestionRe: Php enguine is not active Pin
ng0026-May-07 10:24
ng0026-May-07 10:24 
AnswerRe: Php enguine is not active Pin
ng0026-May-07 11:06
ng0026-May-07 11:06 

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

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