Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Monday, August 09, 2010

Calling the php interpreter within web script

Recently, part of a development required me to execute shell script as part of a web request. The design was to kick start a long running php process (via shell script), which would run beyond the length of a standard web request. To do this, I used forking shell script and popen... (if using shared hosting, shell execution methods are generally disabled - check php.ini's disable_functions config value to verify).

Within my dev environment (Windows) I was able to execute the following with no issues:

popen("start /b php my_script.php my_args", "w");
Note: 'start /b' forks a windows process - making it run in the background



When executing its equivalent in a Linux environment
popen("php my_script.php my_args &", "w");

In Linux, the server was thrown into a state of confusion, perpetually executing, then aborting the requested shell script command (infinitely starting and exiting the requested php script in the popen command). Obviously explicitly calling the php interpreter from within an executing php process is a particularly nasty thing to do.

I'm assuming this sort of thing doesn't happen in windows as shell script executes in its own command window (which when using proc_open with get_proc_status makes getting a correct PID value problematic). Anyone with futher insight - feel free to leave comments.

To get around this issue, I ensured that in the linux environment, the shebang path to the interpreter appeared on the first line of the executing script, and it was chmod'ed to allow direct cli execution... ie

my_script.php:
#!/usr/local/bin/php

//
// my code
//
?>

CHMOD command used:
chmod 755 my_script.php

Then finally the linux popen command:
popen("./my_script.php my_args &", "w");

The default working directory of popen, and all other shell script execution methods is always web root (so my_script.php sat in the web root dir).

The same issue occurs with all other shell script execution methods:
system, exec, proc_open, passthru, back ticks (`) etc.

Saturday, June 05, 2010

PHP, CodeIgniter, Doctrine and ExtJS


So, its been ages since I have posted anything on this particular blog, so time for an update.
-->


Although I still dabble with MS technologies, I find the bulk of my development has moved to MVC geared frameworks (ideally rails based) - which (I find) works best within the contexts of dynamic languages..... so I have been using platforms like Ruby, JRuby, PHP, even ColdFusion for the last few projects. My tech stack of choice is:

ExtJS : client side JS library
JRuby : presentation tier
Java + Hibernate : domain + data layer

In the latest project however I have been developing a survey site in PHP. I thought that to save anyone else time, I have created a 'project starter' development stub which incorporates:
  • ExtJS 3.2.0: client side JS library,
  • CodeIgniter 1.7.2: presentation + domain tier,
  • Doctrine 1.2: ORM data layer framework

The idea being that you can simply extract and develop, without wasting time integrating all the techologies so that they work together....

Some features of this project starter are:
  • CodeIgniter Models have been replaced with Doctrine Records
  • Doctrine is loaded into CI as a plugin
  • RoR type before and after filters....
  • Doctrine transactions automatically wrapped around every action at execution time (ATOMIC db updates)
  • Basic Role based security (I think Redux may be in there as well?)
Simply extract, hook up the database.php config file and viola.... You can start coding your layouts, views and models.

Licensing: refer to all integrated frameworks licensing agreements for details (i.e. Doctrine, ExtJS, CodeIgniter). Original licensing agreements are bundled in the Project starter for your reference. The Project starter is a freely distributed, open source integration of technologies. Just make sure that you are using each framework in adherence to their respective licensing terms. As far as I am aware all technologies can be freely used under GPL....