SonghaySystem(::)

Translating between ASP and PHP

This discussion is confined to all versions of Internet Information Server hosting Active Server Pages (ASPs) up to version 5 (just before ASP.NET) and PHP 4.1.0 (specifically referring to the new autoglobals). Flippantly speaking, of course, ASP is all about creating objects to access their respective properties and methods while PHP is largely about calling functions—many, many functions—or accessing a predefined variable.

The table below summarizes common ASP object methods/properties and the equivalent PHP function or predefined variable:

ASP Method/PropertyPHP Function Or Predefined Variable
Request.Cookies.Item(vKeyName)$_COOKIE[$keyName];
Request.Form.Item(vKeyName)$_POST[$keyName];
Request.QueryString.Item(vKeyName)$_GET[$keyName];
Request.ServerVariables.Item("SERVER_NAME")$_SERVER['SERVER_NAME'];
Response.Cookies.Item(vKeyName) = vCookieValuesetcookie($keyName, $cookieValue);
Response.Redirect(vURI)

header("Location:" . $URI);

where $URI contains an absolute or relative URI. Recall that absolute URIs are preferred unless we use this workaround:

header("Location:
http://" . $_SERVER['HTTP_HOST']
. dirname($_SERVER['PHP_SELF'])
. "/" . $relative_url);
Response.Write(vURI)

echo $URI;

print $URI;

printf($URI);

print_r($URI);

sprintf($URI);

The absence of parenthesis for echo and print reminds us that these “functions” are actually language constructs (what Microsoft calls statements).

echo can output one or more strings in a comma-delimited list of arguments so print is actually the closest equivalent to the Write method of the Response object.

The other PHP functions listed here for the sake of completion are far more powerful and complex than what we expect from ASP.

Server.CreatObject(vProgID)

VBScript example:

Set objCNN = Server.CreatObject("ADODB.Connection")
Response.Write(objCNN.Version)

$objCNN = new COM("ADODB.Connection");echo $objCNN->Version;

This function is not documented as being in all versions of PHP but part of the Concurrent Versions System. The assumption here is that this function may be in the win32 build of PHP but not others.

Server.HTMLEncode(vStr)

htmlspecialchars($str);

A more powerful (and “typographically correct”) function is htmlentities(). Conversely, the strip_tags() function promises to undo the encoding, removing both HTML and PHP tags.

Server.MapPath(vStr)realpath($str);
Server.ScriptTimeout(60)set_time_limit(60);
Server.URLEncode(vURI)

urlencode($URI);

Conversely, the urldecode() function removes the encoding.

These functions encodes according to the application/x-www-form-urlencoded media type (the same format submitted by HTML forms).

PHP supports the RFC1738 standard through the rawurlencode() and rawurldecode() functions.

Buffering

PHP has over a half-dozen “ob” functions, referring to the output buffer. This increased functionality allows PHP to distinguish between destroying the output buffer and simply erasing it (among other things). However, the following table centers upon direct translation:

ASPPHP
Response.Buffer = Trueob_start();
Response.Flushob_end_flush();

Server-Side Includes

Unlike the ASP directive, the PHP functions governing server-side includes allows both absolute and relative paths to included files. There are four PHP functions used to include files: include(), include_once(), require() and require_once(). These functions depend on the configuration directive include_path being set to resolve relative paths (so we can see a directive behind these functions).

ASPPHP
<!--#include file="./foo.asp"-->

require('./foo.asp');

The require() function translates directly from ASP due to the fatal errors it generates when it fails to find files, while the include() function only generates warnings.

<!--#include virtual="./inc/foo.asp"-->

require('inc/foo.asp');

Note that the relative path here does not have leading dots or slashes. This is because of the dependency on the include_path directive.

The include_path directive would contain an absolute path leading to the inc folder in its list of paths. If this is not the case then an absolute path must be used:

require('c:/web/inc/foo.asp');

This example uses Windows paths.

Session State

ASPPHP
<%@ ENABLESESSIONSSTATE=True %>session_start();
Session.Abandonsession_destroy();
Session.Contents.Item(vKeyName)$_SESSION[$keyName];
Session.Contents.Remove(vKeyName)unset($_SESSION[$keyName]);
Session.Contents.RemoveAll

session_unset();

Or

$_SESSION = array();

Error Handling

PHP 4.x allows setting the level of error reporting, raising errors and specifying an error handling function. There is even an error control operator @. These features do not directly relate to ASP—especially VBScript ASP. The try…catch…finally statement of JScript and the Error Resume Next statement of VBScript both suggest the existence of an exception object or an error object, respectively. These ASP error objects hold error numbers and descriptions. There are no such things in PHP.

 
This document was last reviewed on Tuesday, April 26, 2005 at 07:08 PM PDT.
Copyright© 2008 by Bryan D. Wilhite All rights reserved. No part of this material may be used or reproduced in any form or by any means, or stored in a database or retrieval system, without prior written permission of the publisher except in the case of brief quotations embodied in critical articles and reviews. Making copies of any part of this material for any purpose other than your own personal use is a violation of United States copyright laws.

The information provided by Bryan D. Wilhite at kintespace.com is provided “as is” without warranty of any kind. In no event shall Bryan D. Wilhite or any of his affiliates be liable for any damages whatsoever including, but not limited to, direct, indirect, incidental, consequential, loss of business profits or special damages due to material published by Bryan D. Wilhite or any of his affiliates.