Request class

From PhiWiki
Jump to: navigation, search

The request class contains all data related to a request including POST and GET variables, file uploads, Cookies and HTTP headers.

Usage

The request class is a global object in the server scripting environment.

loadModule('request');
var r=request;

Additionally you can use the request class to set cookies with request.setCookie().

Properties

  • String accept [read]
  • String agent [read]
  • Number agentengine [read|write]
  • Number agentid [read|write]
  • Number contentlength [read]
  • String contenttype [read]
  • Number enginemajorversion [read|write]
  • Number engineminorversion [read|write]
  • String filename [read]
  • String hostname [read]
  • String lang [read]
  • StringArray languages [read]
  • String method [read]
  • Date modified [read]
  • Number ostype [read|write]
  • String password [read]
  • String postdata [read]
  • StringArray properties [read]
  • String scheme [read]
  • Date started [read]
  • String url [read]
  • String user [read]

String accept

Returns the Accept: header ot the request.

var accept=request.accept;

String agent

Returns the user agent (full browser name including the system from the HTTP header).

var browser=request.agent;

See also: request.useragent.

Number agentengine

v1.1.1 Returns or sets the agentengine ID.

  • 0 - Unknown engine
  • 1 - Trident (MS Internet Explorer)
  • 2 - Gecko (Firefox)
  • 3 - WebKit based (Safari, Google Chrome, Amphibia)
  • 4 - Presto (Opera)

Warning: setting the agentengine overwrites the automatic detection of the browser. This makes only sense in an intranet environment where all users have the same browser.

See also: request.enginemajorversion, request.engineminorversion, request.ostype, request.userengine.

Number agentid

v1.1.1 Returns or sets the agent ID.

  • 0 - Unknown browser
  • 1 - Internet Explorer
  • 2 - Firefox
  • 3 - Konqueror
  • 4 - Safari
  • 5 - Chrome
  • 6 - Opera
  • 7 - Amphibia
  • 8 - SeaMonkey

Warning: setting the agentid and viewing it with a different browser type causes unpredictable results. Phi creates special HTML code (in HTML mode) for each browser to benefit from the available browser extensions (like -webkit-*, -ms-*, -o-*, etc.).

See also: request.useragent

Number contentlength

Returns the content length of a POST request or '0' if there no data is sent with the request.

var l=request.contentlength;

String contenttype

Returns the content type of a POST request (usally this will be multipart/form-data) or an empty string with a GET request.

var ctype=request.contenttype;

Number enginemajorversion

v1.1.1 Returs or sets the engine major version.

Typical engine versions used in browsers today are:

  • Trident: 3.x, 4.0, 5.0, 6.0 (Trident based like Internet Explorer)
  • Gecko: 1.9, 2.0, 10.0 (Mozilla based, like Firefox)
  • WebKit: 532.x, 533.x, 534.x (Apple Webkit based like Safari, Chrome, Amphibia)
  • Presto: 2.2, 2.3 (Opera)

Note: Reading the major and minor version in conjunction with the request.agentengine enables you to react differently on browser types and their features.

Number engineminorversion

v1.1.1 Returs or sets the engine minor version.

String filename

Returns the canonical file name (complete path on the local disk) of the requested page.

var name=request.filename;

String hostname

Returns the hostname the client requested (usually the domain name of the server) in the Host: header field.

var name=request.hostname;

String lang

Returns the first matching language of the Accept-Language header in the request or C (the default language) if it does not match any. The GET, POST or COOKIE variable philang has precedence.

var lang=request.lang;

See also: request.languages.

StringArray languages

Returns the languages the browser accepts from the Accept-Language header of the request.

var langs=request.languages;

You can browse through the array with:

var i=0;
var langs=request.languages;
while ( langs[i] ) print( langs[i++] );

Note: the array does not contain any qualifiers ie. 'de;q=0.3'. If you need the qualifier use request.header.Accept-Language instead.

See also: request.lang.

String method

Returns the method ot the request (HEAD, GET or POST).

var method=request.method;

Date modified

Returns the last modification of the requested file.

var modified=request.modified;

Number ostype

v1.1.1 Returns or sets the operating system type the browser (client) is running on.

  • 0 Unknown OS
  • 1 Windows
  • 2 Linux
  • 3 Mac OS X
  • 4 iOS (iPhone and iPad based)
  • 5 Symbian
  • 6 WindowsMobile
  • 7 Android
  • 8 Solaris
  • 9 AIX
  • 10 HPUX

String password

Returns the password the user typed in during user authentication (ie. related to .htaccess files).

var passwd=request.password;

String postdata

returns the POST data as an URL with a prefixed '?'.

var pdata=request.postdata;

Note: this function is slow. You should prefer request.post.myitem to obtain the POST value for myitem.

StringArray properties

Returns the properties array of the request class.

var props=request.properties;

You can browse through the array with:

var i=0;
while ( request.properties[i] ) print( request.properties[i++] );

String referer

v1.1.0 Returns the referer URL.

var referer=request.referer;

This is the same as request.header.Referer;

String scheme

Returns the scheme of the request. This could be http or https.

var scheme=request.scheme;

String self

Returns the path of the current requested url.

 var path=request.self; // url=http://domain.com/mypath/mypage.phis?philang=de
 // path=/mypath/mypage.phis

Date started

Returns the date and time in local server time when the request started.

var start=request.started;

Use the server.nowutc to get the current date time in UTC (GMT) as a string representation.

See also: Date class, server.nowutc, server.utc.

String url

Returns the complete URL string of the request but without domain and scheme.

var url=request.url;

See also: request.postdata.

String user

Returns the user name (ie. related to .htaccess files for user authentication).

var user=request.user;

String useragent

v1.4.1 Returns the user agent ID as a string.

See also: request.agentid.

String userengine

v1.4.1 Returns the user engine ID as a string.

See also: request.agentengine.

String useros

v1.4.1 Returns the user operating system as a string.

See also: request.ostype.

String version

v1.4.1 Returns the Phi version as a string.

Functions

There are currently no functions in this class defined.

Additional notes

The GET, POST, COOKIE, FILE and HEADER variables are available as sub classes from request.

// returns the properties array of the GET variables:
var get=request.get.properties;
// returns the POST value of the item with ID 'myinput'
var value=request.post.myinput;

In Text sources you can make use of the $PHIREQUEST[item_name] variable. Simply replace item_name with your item ID/name to get the appropriate replacement. $PHIREQUEST[] queries all POST, GET and COOKIE variables. Use $PHIPOST[], $PHIGET[] or $PHICOOKIE[] if you just want to query the specific type.

GET variables

All GET variables are available in the array request.get.properties. Each variable value is accessible throught the variable name:

var value=request.get.myvar;

Note: all variables starting with phi are reserved. Often used Phi variables are philang (selected language) and phisid (session ID).

Warning: request.get.* represents string arrays with the first entry as the value. This works fine for asignments and using them for example in SQL statements. However if you want to use string properties or functions you need to specify the first value in the array explicitly:

var length=request.get.myvar[0].length; // or use:
var value=new String( request.get.myvar );
var length=value.length;

POST variables

All item names are available in the array request.post.properties.

var value=request.post.myitem;

The item name represents the item ID in Phi and the name attribute in HTML. Asume the following input tag:

<input type="text" name="mytext" value="" />

Then you can query the value of mytext with:

var value=request.post.mytext; // array of strings!
var length=request.post.mytext[0].length;

Note: Since request.post.* are string arrays you may use (for example for checkboxes and radio buttons):

var checked=request.post.mycheckboxes;
var i=0;
while ( checked[i] ) {
 value=checked[i++];
 doSomething( value );
}

See also: Meta data - form action.

Cookies

You have access to all cookies for this page in the array request.cookie.properties.

var cookies=request.cookie.properties;

If you want to query the value of a specific cookie named mycookie use:

var value=request.cookie.mycookie;

See also: reply.setCookie().

File upload

Files which are uploaded are provided in request.file. They are stored temporarly in the systems TEMP directory. You have to copy the file from the TEMP directory using the File class if you want to keep it.

Asume the HTML input tag:

<input type="file" name="myfile">

Now you are able to query the file parameters:

var tmpName=request.file.myfile.tmpfile;
var orgName=request.file.myfile.filename;
var size=request.file.myfile.size;

if ( file.copy( tmpName, server.documentroot+'/uploads/'+orgName ) ) {
 //copying was successful
} else {
 //an error occured
}

See also: file.copy.

HTTP header

All HTTP headers sent by the client request are available in request.header.properties.

var headers=request.header.properties;
var i=0;
// prints out the header names:
while ( headers[i] ) print( headers[i++] );