A blend of programming and seo

5 cool things you can do with windows and php

Many PHP examples out there are designed for a linux/unix operating system. I am going to give some examples of some interesting functionality that only works with php running in a windows environment (IIS or apache).

1) Eject the CD-ROM

//create an instance of Windows Media Player
$mp = new COM("WMPlayer.OCX");
//ejects the first cd-rom on the drive list
$mp->cdromcollection->item(0)->eject();

2) Read and write from/to the registry

function registry_read($folder, $key)
{
    $WshShell = new COM("WScript.Shell");
   
    $registry =  "HKEY_LOCAL_MACHINE\SOFTWARE\\" . $folder . "\\"  . $key;
    $result = $WshShell->RegRead($registry);
   
    return($result);
}

$key = registry_read("RegisteredApplications","Firefox");

parameters:

  • Folder name – (key path past HKEY_LOCAL_MACHINE\SOFTWARE\\)
  • key – the key name to read from
function registry_write($folder, $key, $value,$type="REG_SZ")
{
    $WshShell = new COM("WScript.Shell");
   
    $registry =  "HKEY_LOCAL_MACHINE\SOFTWARE\\" . $folder . "\\"  . $key;
    $result = $WshShell->RegRead($registry);
    $result = $WshShell->RegWrite($registry,$value, $type);
     
    return($result);
}

parameters:

  • Folder name – (key path past HKEY_LOCAL_MACHINE\SOFTWARE\\)
  • key – the key name to write to
  • value – value that will be written to the key
  • type – key type (default: REG_SZ)

3) register and un-register phpscripts as a windows service

# registering a service

win32_create_service(array(
’service’ => ‘myservice’, # the name of your service
‘display’ => ’sample dummy PHP service’, # description
‘params’ => ‘c:\path\to\script.php run’, # path to the script and parameters
));

# un-registering a service

win32_delete_service(’myservice’);

# code run as a service

if ($argv[1] == 'run') {
  win32_start_service_ctrl_dispatcher('myservice');

  while (WIN32_SERVICE_CONTROL_STOP != win32_get_last_control_message()) {
    # write script here
    # as a general rule, keep it below 30 seconds through each loop iteration
  }
}

This uses the windows API Service DLL, which is not enabled by default. Here is how to install it:

  • Download the main library (it’s included in the main PECL extension download from php.net)
  • extract php_win32service.dll to your ext directory (where your php extension .dlls are located)
  • add the following line to your php.ini: extension=php_win32service.dll

4) print pages/data

#this is an example function that will format a host/printer name, for printing to shared printers over the network

function getPrinter($host,$SharedPrinterName) {
return “\\\\”.$host.”\\”.$SharedPrinterName;
}

#this opens the printer
$handle = printer_open(getPrinter(”my computer 2″,”my printer”));

An extensive list of functions for printing can be found here

#this is possible in *nix as well. Here is some example code

function lpr($string,$printer) {
$prn=(isset($printer) && strlen($printer))?”$printer”:C_DEFAULTPRN ;
$CMDLINE=”lpr -P $printer “;
$pipe=popen(”$CMDLINE” , ‘w’ );
if (!$pipe) {print “pipe failed.”; return “”; }
fputs($pipe,$string);
pclose($pipe);
}

This uses the windows API Service DLL, which is not enabled by default. Here is how to install it:

  • Download the main library (it’s included in the main PECL extension download from php.net)
  • extract php_printer.dll to your ext directory (where your php extension .dlls are located)
  • add the following line to your php.ini: extension=php_printer.dll

5) List the current system processes

# list all the current processes running on the system

print_r(win32_ps_list_procs());

other related commands:

# Retrieves statistics about the global memory utilization
print_r(win32_ps_stat_mem());

# Retrieves statistics about the process with the process id pid (if no process id is given, the current process will be used)
print_r(win32_ps_stat_proc(int processid));

This uses the windows API Service DLL, which is not enabled by default. Here is how to install it:

  • Download the main library (it’s included in the main PECL extension download from php.net)
  • extract php_win32ps.dll to your ext directory (where your php extension .dlls are located)
  • add the following line to your php.ini: extension=php_win32ps.dll
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • Reddit
  • Twitter
  • HackerNews
  • StumbleUpon
  • Technorati

14 comments

1 5 cool things you can do with windows and php | A blend of … | Adsense Syndicate { 03.31.09 at 8:59 am }

[...] More here:  5 cool things you can do with windows and php | A blend of … | [...]

2 Ryan { 03.31.09 at 9:28 am }

Hammer, see nail. Ever thought of using a scripting language with better security and management functions rather than calling out to COM or the shell to get things done? Python, Perl, PowerShell?

3 Zorro { 03.31.09 at 10:05 am }

I don’t thing that there are people out there that really want to do windows programming on php..

4 Tom { 03.31.09 at 2:04 pm }

Well, you’re wrong :o )

5 dzhariy { 03.31.09 at 6:42 pm }

Looks like it will be better to use Jscript for such things. It is already present in all operating systems higher than Win2k. Let’s use PHP to do some other stuff.

6 abcphp.com { 04.01.09 at 2:22 am }

5 cool things you can do with windows and php | A blend of programming and seo…

Many PHP examples out there are designed for a linux/unix operating system. I am going to give some examples of some interesting functionality that only works with php running in a windows environment (IIS or apache)….

7 Timothy { 04.01.09 at 8:57 am }

Ryan, Zoro, dzhairy — sorry, but I think you’re wrong. Sometimes you just can’t escape Windows. And if you have PHP installed (perhaps you’re deploying on a *nix box but developing locally on Windows– a common setup) then why not do quick and dirty tasks in a language you’re already familiar and comfortable with?

More and more functionality is expected out of today’s web-based applications. PHP isn’t just generating webpages, but complex business-critical applications. Depending on the environment and your needs, it might make perfect sense to store configuration information such as database credentials in the Windows registry, or print incoming invoices to a printer connected to the server in real-time.

I am curious though as to how much influence these articles in PHP|Architect played in this blog post:

An Introduction to PHP’s Printer Functions (September 2003)
Printing from PHP (June 2007)
Writing a Windows Service in PHP (September 2007)
Reusable Shell Scripting (July 2008)

8 Justin (rawseo) { 04.01.09 at 9:34 am }

@timothy,

I’ve never read those articles (thanks for the links). I am a professional php developer and I mostly work in a linux environment. I have recently started to venture into windows servers (specifically running PHP scripts on them) and I felt that there aren’t enough articles on this topic and I wanted to share my findings.

9 David Dashifen Kees { 04.01.09 at 10:25 am }

“I don’t thing that there are people out there that really want to do windows programming on php.”

Actually, we run PHP on Windows for our site. I’ve run it in both environments and I actually prefer it on Windows, myself. Regardless, this stuff is pretty neat. I’ve used the printing features myself and it can be very nice to send dynamically generated PDF files directly to a printer rather than to the screen or filesystem.

10 Justin’s Blog: 5 cool things you can do with windows and php : Dragonfly Networks { 04.01.09 at 11:52 pm }

[...] has posted a few cool things that you can do by combining PHP with someof the Windows functionality PHP has access to (either [...]

11 hype.yeebase.com { 04.03.09 at 12:01 pm }

5 cool things you can do with windows and php…

Herausgekommen ist eine Liste mit fünf coolen Dingen, die man unter Windows mit PHP erledigen kann….

12 5 coole Dinge die man mit PHP unter Windows tun kann | tagdocs.de { 04.06.09 at 12:06 am }

[...] Rawseo wurde ein Artikel mit 5 “coolen” Dingen veröffentlicht die man mit PHP unter Windows [...]

13 arsey { 04.21.09 at 6:07 am }

people, how to insert CD???it’s posible?

14 Twitter Trackbacks for 5 cool things you can do with windows and php | A blend of programming and seo [rawseo.com] on Topsy.com { 08.23.09 at 10:37 pm }

[...] 5 cool things you can do with windows and php | A blend of programming and seo http://www.rawseo.com/news/2009/03/31/5-cool-things-you-can-do-with-windows-and-php – view page – cached Many PHP examples out there are designed for a linux/unix operating system. I am going to give some examples of some interesting functionality that only works with php running in a windows environment (IIS or apache). — From the page [...]

Leave a Comment