Jaslabs: High performance Software

High Performance Software

PHP library for Microsoft AJAX

By Justin Silverton

Microsoft has released their AJAX library package for non-windows systems which contains a complete set of client JavaScript components that are included in the full ASP.NET AJAX installation. Developers over at Codeplex have developed a library that allows you to integrate your PHP applications with this package.

requirements

  • PHP 5.2 — 5.2 is required for json_encode/json_decode; For earlier versions, you will need to install php-json

How to install

  1. Download the PHP library from codeplex here
  2. Download the Microsoft AJAX Library here
  3. After extracting both of these, place all the files from the Microsoft Ajax library in a directory called “MicrosoftAjaxLibrary”

Example

The following is a simple example that demonstrates a simple ajax transmission using the microsoft libraries.

(hello.htm)

<html>
<head>
<title>Hello, World!</title>
<script type="text/javascript" src="../../MicrosoftAjaxLibrary/MicrosoftAjax.js"></script>
<script type="text/javascript" src="HelloService.php/js"></script>
</head>
<body>
Name: <input id="name" type="text" /> <input type="button" value="Say Hello" onclick="button_click(); return false;" />
<br />
Response from server: <span id="response"></span>
</body>
<script type="text/javascript">
function button_click() {
HelloService.SayHello($get('name').value, function (result) { $get('response').innerHTML = result; });
}
</script>
</html>

(HelloService.php)

<?php

require_once '../../dist/MSAjaxService.php';

class HelloService extends MSAjaxService
{
function SayHello($name)
{
return "Hello, " . $name . "!";
}
}

$h = new HelloService();
$h->ProcessRequest();

?>

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Slashdot
  • StumbleUpon
  • Technorati
1 comment

Using Ajax across multiple domains

By Justin Silverton

XMLHttpRequest, the main component behind AJAX, does not automatically work across multiple domains. This means that you cannot make a request to an ovject on a domain that is different from the web page’s domain. There is an easy solution to this issue: apache’s mod_rewrite module.

Example

function getXMLHttpObject()
{
if (window.XMLHTTPRequest)
return new XMLHttpRequest();
else if (window.ActiveXObject)
return new ActiveXObject("Microsoft.XMLHTTP");
else
return null;
}

function handleHTTPResponse()
{
if (http.readyState == 4) {
results = http.responseText;
}
}

var http = getXMLHttpObject();

http.open("POST"."http://www.yahoo.com/service");
http.onreadystatechange = handleHttpResponse;

The above example will fail with both Firefox and Internet Explorer (unless you are running it on a web page located on the yahoo domain). There are other ways to allow cross site ajax. Within Internet Exporer, the default security settings can be changed or a host can be added to the “trusted hosts” list. Firefox, on the other hand, has a concept called signed scripts. Both of these methods will not work for most websites on the Internet. This is because it would involve every user coming to your site adding your page to their trusted host list.

Apache setup

  1. Install apache with both mod_rewrite and proxy enabled.
  2. Create the following rule: RewriteRule ^/yahoo_proxy http://www.yahoo.com/service [P]

Note: The [P] indicates a pass-through proxy.

Replace the above line: (http.open("POST"."http://www.yahoo.com/service")) with

http.open("POST"."http://your_host/yahoo_proxy") and a connection will be made to the yahoo domains through your apache server while not violating the security restrictions of IE or Firefox.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Slashdot
  • StumbleUpon
  • Technorati
1 comment

Secret Internet Explorer behaviors

By Justin Silverton

Many people don’t realize that there is a way to store up to 100K (1mb per domain) of data within Internet Explorer (supported by 5.5 and above).

These behaviors can be used to preserve information in the browser’s history, in favorites, in an XML store, or directly within a Web page saved to disk. When a user returns to a persisted page, the state of the page can be restored. By allowing information to safely reside on the client, fewer server transactions are required. Custom start pages and Web applications prosper because information can continue to exist without the paternal support of the server, or repeated server queries. A Web page can remain interactive to a degree, after the connection with the host has been severed, by persisting required information on the client.

Code Example

Below is an code example of how to use these behaviors. After typing some text into the box and clicking save, you will now be able to come back to this page and click load, and it will be loaded from the the IE internal persistence store.

<STYLE>
.storeuserData {behavior:url(#default#userData);}
</STYLE>
<SCRIPT>
function fnSaveInput(){
var oPersist=oPersistForm.oPersistInput;
oPersist.setAttribute("sPersist",oPersist.value);
oPersist.save("oXMLBranch");
}
function fnLoadInput(){
var oPersist=oPersistForm.oPersistInput;
oPersist.load("oXMLBranch");
oPersist.value=oPersist.getAttribute("sPersist");
}
</SCRIPT>
<FORM ID="oPersistForm">
<INPUT CLASS="storeuserData" TYPE="text" ID="oPersistInput">
<table>
<td><INPUT TYPE="button" VALUE="Load" onclick="fnLoadInput()"></td>
<td><INPUT TYPE="button" VALUE="Save" onclick="fnSaveInput()"></td>
</table>
</FORM>

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Slashdot
  • StumbleUpon
  • Technorati
4 comments

How to test a web application

By Justin Silverton

When creating and releasing a web application, testing is a very important step in the overall process. It can determine the success of an application and is many times a very time consuming process. There is an open source application called selenium that can help automate these steps and help reduce the risk of missing anything important while testing.

How it works

Selenium uses JavaScript and Iframes to embed a test automation engine in your browser. This technique should work with any JavaScript-enabled browser. Because different browsers handle JavaScript somewhat differently, we usually have to tweak the engine to support a wide range of browsers on Windows, Mac OS X and Linux. The beauty of this design is that it can also be used to test any web page or script (independent of the the back-end technology used to generate it).

Features

  • Easy record and playback
  • Intelligent field selection will use IDs, names, or XPath as needed
  • Autocomplete for all common Selenium commands
  • Walk through tests
  • Debug and set breakpoints
  • Save tests as HTML, Ruby scripts, or any other format
  • Support for Selenium user-extensions.js file
  • Option to automatically assert the title of every page

How to test your application

Testing comes in three flavors. The first is a simple GUI interface (either a firefox extension or through a back-end application for Internet Explorer) that allows the direct recording of any action in the browser. Below is a screenshot of this interface.

Test scripts can be created by the following steps:

  1. Install the Firefox extension
  2. Go to the website/page that you would like to start testing
  3. Click the and go through all of the steps for the test
  4. When you are finished, click the button from step 3 again to finish the test

You will notice some commands listed on the main screen. These are the commands that were recorded from above. These can also be added manually.

Another method for testing is using the selenium core. Selenium Core uses a unique mechanism which allows it to run on so many platforms. Written in pure JavaScript/DHTML, you copy Selenium Core tests directly into your your application webserver, allowing the tests to run in any supported browser on the client-side. It allows you to do the following:

  • Browser compatibility testing. Test your application to see if it works correctly on different browsers and operating systems. The same script can run on any Selenium platform.
  • System functional testing. Create regression tests to verify application functionality and user acceptance.

The third method for testing is through the Selenium Remote Control. Selenium Remote Control provides a Selenium Server, which can automatically start/stop/control any supported browser.

The Selenium Server communicates directly with the browser using AJAX (XmlHttpRequest). You can send commands directly to the Server using simple HTTP GET/POST requests; that means that you can use any programming language that can make HTTP requests to automate Selenium tests on the browser. Wrapper objects are also included for Java, .NET, Perl, Python, and Ruby.

Download

Selenium can be downloaded for free Here

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Slashdot
  • StumbleUpon
  • Technorati
1 comment

A PHP compatible MD5 function in c#

By Justin Silverton

The following is a function written in c# that will return a PHP compatible MD5 hash of a file, given the name. The equivalent function in PHP is md5_file().


public string MD5Hash( string sFilePath )
{
try
{
MD5CryptoServiceProvider md5Provider
= new MD5CryptoServiceProvider();
FileStream fs
= new FileStream(sFilePath, FileMode.Open, FileAccess.Read);
Byte[] hashCode
= md5Provider.ComputeHash(fs);

string ret = "";

foreach (byte a in hashCode)
{
if (a<16)
ret += "0" + a.ToString ("x");
else
ret += a.ToString ("x");
}

fs.Close();
return ret;
}
catch( Exception ex )
{
throw ex;
}
}

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Slashdot
  • StumbleUpon
  • Technorati
3 comments

« Previous PageNext Page »