Zend and Microsoft Collaboration

Recently it was announced that Zend and Mirosoft are collaborating to ensure PHP will run on Windows IIS servers with equal (or close enough) performance of Linux/Apache servers.

While all reports claim that “it’s giving users another choice not to choose linux”, I find myself asking “Why would I want to use PHP on IIS?”

Once upon a time I used to run PHP as a CGI on IIS when I was starting out, something I don’t reccomend to anyone. It was slow, hard to setup and didn’t help me learn about the real world servers using Apaches’ modules.

The biggest feature of the LAMP combination is the cheap price tag. Hosting providers that use Windows Server and IIS charge more to the customer.

However, these changes may convert some ASP developers to use PHP alongside their applications, and slowly raise more demand for PHP programmers.

See the articles here:

PHP Ajax File Browser 2.0 Beta

Update: Version 2.0.1 is available. Update post found here.

After the success of the PHP Ajax File Browser V1.1, I’ve re-invented the wheel with this new version. It’s not just a simple file browser anymore!Public Front endHere’s a list of features (some were already present in 1.1):

  • Easy to use interface
  • Allows public or restricted users to download anything you allow
  • Share directories do not have to be beneath the site root
  • Permissions: List, Download, Upload and Delete
  • Administration Area for easy maintenance
  • Simple self installation script
  • Display extra links
  • Logs everything to a Mysql database
  • Reports on downloads, clickthrus, everything.
  • Theme Support
  • Block certain filetypes, filenames and directories
  • Small page weight

Note: This is a beta. It will have bugs. Use at your own risk. I use it, so it can’t be that bad ;) But I do take no responsibility for loss of data etc etc. Also, I hope that you’re wise enough not to share the site directory of this application, or people could download your config files and get your passwords.

Administration AreaInstallation

Just unzip to a directory somewhere on your webserver, and point to index.php. Installation will ask you for details from there. The application does not need to be at the root of your site (anymore).

So, where do you get it?

Please report bugs and suggestions here. See below for a list of compatibilities.

Op System Webserver PHP Mysql Result
Windows Apache 2 5.1.4 5.0.22 OK
Linux Apache 2 4.4.3 4.1.21 OK
Op System Browser Result
Windows Firefox 1.5 OK
Windows Firefox 2 OK
Windows IE 6 OK
Windows IE 7 OK

If you have had success or failure with other combinations please let me know – particularly non Windows browsers.

Known issues

Contributor Problem
Rutger Files and directories with accented characters such as ‘é’ and other like ‘ø’ cause the application to hang when retrieving a directory listing. Resolved in 2.0.1

Technorati: ajax, php, javascript, web 2.0

Vista Aero Mouse Cursors

I don’t normally rave about little things like backgrounds, icons or mouse cursors… But my latest preview with Vista RC1 showed me that the mouse cursors look cool. So I wanted to get them, knowing that someone out there would have made them for other windows systems. And they have. Sweet.

Technorati: vista, aero, cursors

Free IP Network Browser 1.1

Free IP Network BrowserYears and years ago I used a powerful IP Network browser from Solarwinds (if the company name hasn’t changed). It was great until windows 2000 SP2 came out, then it didn’t work anymore. Sure Solarwinds released future versions, but then they started charging $$ for it. Being the avid problem solver I like to think I am, I wrote an IP Network browser of my own. It’s no where near as fancy as other non-free applications, and certainly not as fast. But it gets the job done. Recently I’ve had to dig up this code and tweak it for my needs. And because I’m such a nice guy, I’ll give you the application and code, for free!

So what does it do?

  • Scans an IP Subnet within the boundaries you set.
  • Can also attempt to get the Hostname for the IP’s returned.

Where do you get it?

Technorati: visual basic, network, free

How to dynamically return XML data using PHP

This is the second part of a tutorial covering the Front-end browser part of Ajax’ing and the Back-end part of sending data with PHP. The first part can be found here.

So we’ve covered how to request an interpret XML data from a webpage using Sarissa. Now we want to control what data is being returned, dynamically.

The basic principal behind setting up the script is to make the browser think it’s an XML document, not a PHP script. There are 2 ways you can do this:

  1. Add a content-type header into your script
  2. Add an apache handler to treat xml files as scripts (which still involves step 1 anyway)

So I’ll only cover the first option. On top of that, I’m going to throw you in the deep end here, and show you the entire script:

<?php
//simulate a remote connection
sleep(2);
 
//include any libraries you want to use here.
 
//get the $mode var
$mode = stripslashes($_GET['mode']);
//Set the content-type header to xml
header("Content-type: text/xml");
//echo the XML declaration
echo chr(60).chr(63).'xml version="1.0" encoding="utf-8" '.chr(63).chr(62);
?>
<xmlresponse>
  <?php
  //make a decision based on $mode
  switch ($mode) {
    case 'getitems':
      //set items in a test array
      $items_array[] = array(
        'id'=>15,
        'name'=>'Finger Bun',
        'price'=>'$0.80 ea'
      );
      $items_array[] = array(
        'id'=>16,
        'name'=>'Donuts',
        'price'=>'$0.50 ea'
      );
      $items_array[] = array(
        'id'=>17,
        'name'=>'Apple Pie',
        'price'=>'$1.20 slice'
      );
      $items_array[] = array(
        'id'=>18,
        'name'=>'Double Choc Chip Cup Cakes',
        'price'=>'$1.00 ea'
      );
 
      //echo a count of items
      echo '<item_count>'.count($items_array).'</item_count>';
      //echo the array in XML style
      for ($x=0;$x<count($items_array);$x++) {
        echo '<item>';
        echo '<id>'.htmlspecialchars($items_array[$x]['id']).'</id>';
        echo '<name>'.htmlspecialchars($items_array[$x]['name']).'</name>';
        echo '<price>'.htmlspecialchars($items_array[$x]['price']).'</price>';
        echo '</item>';
      }
 
      //set a No Error response - function defined below
      SetErrorNode();
      break;
 
    default:
      //inccorrect mode, let the hacker know!
      SetErrorNode(404,'Stop hacking. Get a real job.');
      break;
  } //end switch
  ?>
</xmlresponse><?php
 
//FUNCTIONS
//this sets the error code node - essential for every xml document to be returned.
function SetErrorNode($code=0,$text='') {
  echo "<error_code>" . $code . "</error_code>\\n<error>" . htmlspecialchars($text) . "</error>";
}
?>

Download this code: _ajax.php

Here’s an explanation. First we simulate an internet connection delay (for those using local development servers) incase you’ve got any features such as a waiting screen on the front-end that you want to admire for two seconds.

Next we get the $mode variable from the GET array (as used in part one of this tutorial) so we can decide what to do with it later. Then we send the content-type headers to the browser, so that it knows this is an XML document. Then we echo the XML declaration. You might be wondering why I’ve used chr everywhere there. This is so we don’t have to turn off php short tags just to make this thing work, a tip for all you out there.

Then we echo the root tag, xmlresponse. I’m not sure if changing this will affect Sarissa’s interpretations, but I don’t change it anyway.

The rest of the script is echo’ing data for our application to use. The only thing to mention is that any data you want to send within xml tags should be coded using htmlspecialchars. Even HTML code should be coded. Sarissa will automatically convert it back for you on the front-end.

Finally, we use the function SetErrorNode to echo our error tags as we want to.

Well that’s all I’m going to cover this time around, but I’m sure this can help a lot of begginners out there. Leave a comment if you have any questions (like how to get the front-end to recognise more than 500 characters of data within a single tag, muahaha).

Technorati: php, ajax, xml, tutorial

How to use Sarissa Ajax Library

I’ll spell it out for you. Or even better, I’ll just give you the code! This is the first part of a tutorial covering the Front-end browser part of Ajax’ing and the Back-end part of sending data with PHP. The second part can be found here.

I am only going to explain the minimums for accomplising this goal. A bigginers lesson if you want. I won’t cover more complex options as I don’t need to use them myself, yet.

The Sarissa Ajax library is my choice for Ajax’ing for a few reasons:

  • It’s small (11.8kb!)
  • Cross browser compatible and
  • Escapes the information received nicely

My tutorial will only cover XML request with Sarissa.
What you need to get started:

  • A web development server (consider Wamp if you’re using Windows)
  • Latest versions of your browser (IE 7 not required, the latest IE 6 is fine)
  • The Sarissa Library
  • Cross Browser X Library javascript functions – Optional, for DOM functions you may use.

At this point I should mention that if you’re reading this article, I assume you’re serious about writing your powerful Web 2.0 applications. If you don’t know your DOM or Javascript language very well, I suggest you take a look at this Sitepoint book. The sample chapters are free and they will help you out quite a bit. Purchase the book if you like it.

So, let’s view the major function for our javascript code, assuming you’ve linked to sarissa.js in your document:

//  getUrl
//  Will initate the http request for data.
function getUrl(url,fn) {
  if (url && fn) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET", url, true);
    xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4) {
        fn(xmlhttp.responseXML);
      }
    };
    xmlhttp.send('');
  } else {
    alert('url or function not specified!');
  }
}
 
//  trim functions
//  will trim whitespace from strings
String.prototype.trim=function(){
  return this.replace(/^\\s*|\\s*$/g,'');
}
String.prototype.ltrim=function(){
  return this.replace(/^\\s*/g,'');
}
String.prototype.rtrim=function(){
  return this.replace(/\\s*$/g,'');
}
 
//  CheckResultIsOk
//  Will check the XML data you've return for <error_code>x</error_code>
//  and returns the error to the user if found.
function checkResultIsOk(xml) {
  if (!xml) {
    //xml data was empty. Result was not ok.
    return false;
  }
  if (xml.getElementsByTagName('error_code')[0].firstChild.data &&
    Math.abs(xml.getElementsByTagName('error_code')[0].firstChild.data.trim()) > 0) {
    //error has been supplied
    alert('Error ' + xml.getElementsByTagName('error_code')[0].firstChild.data + ': ' +
      xml.getElementsByTagName('error')[0].firstChild.data.trim());
    return false;
  } else {
    //no errors!
    return true;
  }
}

Download this code: sarissa_tute_1.js

The functions are very simple.

  • getUrl takes an URL, and a function name before opening a http request. The URL should point to the file on your server, from which XML data would be returned. The function name is just the name of a function you’ve already specified. This function gets called once the XML data has been returned. And you’ve probably guessed I like error checking. The alert reponse can be omitted – it’s just there for our testing purposes.
  • trim functions are a few ingenious pieces of code that will save you years of headaches. As some XML documents are formed, whitespace can occur around the data within the tags. These functions can remove this from your strings.
  • checkResultIsOk will check your XML data that you’ve returned for any specified errors. Of course, this only works if your XML document contains a valid error_code tag with an error number in the data, zero being no error. I started using this method of checking so that I could return errors with my back-end system to the user in a friendly manner. Setting an error tag with a description in your XML will also display that to the user.

Believe it or not, but that’s the hard part over. All you need to do now is create your functions to request your data, and additional callback functions to process the data. Here’s an example of a request function:

//  ajaxGetItems
//  Called from clicking a link or button on your page
function ajaxGetItems() {
  //send the request	
  var filetouse = '_ajax.php?mode=getitems';
  getUrl(filetouse,cbGetItems);
}

Download this code: sarissa_tute_2.js

Easy wasn’t it? Notice you can include GET variables in your request. So if your XML document is a dynamic script (as in part two of this tutorial) you can control some aspects of the script. Also notice that when you specify the function name for getUrl, you don’t wrap it in quotes, and you don’t add () to the end of it, otherwise the function would be executed right there at that point in the code.

So let’s assume this is the XML data that has been returned:

<?xml version="1.0" encoding="iso-8859-1"?>
<xmlresponse>
  <item_count> 4 </item_count>
  <item>
    <id> 15 </id>
    <name> Finger Bun </name>
    <price> $0.80 ea </price>
  </item>
  <item>
    <id> 16 </id>
    <name> Donuts </name>
    <price> $0.50 ea </price>
  </item>
  <item>
    <id> 17 </id>
    <name> Apple Pie </name>
    <price> $1.20 slice </price>
  </item>
  <item>
    <id> 18 </id>
    <name> Double Choc Chip Cup Cakes </name>
    <price> $1.00 ea </price>
  </item>
  <error_code> 0 </error_code>
</xmlresponse>

Download this code: sarissa_tute_1.xml

So in our document we have an item_count tag, 4 items tags with sub tags, and our error_code tag. Let’s see how our callback function would handle this:

//  cbGetItems
//  Called when the data from ajaxGetItems has been retrieved.
function cbGetItems(xml) {
  //check result is ok
  if (checkResultIsOk(xml)) {
    //get the item count
    var item_count = Math.abs(xml.getElementsByTagName('item_count')[0].firstChild.data.trim());
 
    //get the items
    var items = xml.getElementsByTagName('items');
    for (i=0;i<items.length;i++) {
    //send an alert of the data received
      alert (i + "  Data recieved:\\n" +
        "id: " + items[i].getElementsByTagName('id')[0].firstChild.data.trim() + "\\n" +
        "name: " + items[i].getElementsByTagName('name')[0].firstChild.data.trim() + "\\n" +
        "price: " + items[i].getElementsByTagName('price')[0].firstChild.data.trim() + "\\n");
    } //end i for		
  } //end check result
} //end function 

Download this code: sarissa_tute_3.js

The first things the function does is check the data for an error with the checkResultIsOk function. If it passes, we extract the data. The item_count tag isn’t being displayed to the user, I’ve only put it in there to show you how to extract a single tag (that doesn’t have an ID). Next we get all the items tags, iterate through them and display the data in alert popups. You can do whatever you like with the data I’ve used in these alerts.

So there you have half of it! Using Sarissa to retrieve XML data and do something with the data, all with cross browser functionality. Now take part two of the tutorial and learn How to dynamically return XML data using PHP

Technorati: javascript, ajax, xml, tutorial, sarissa