Handling Form Input
Input comes in a number of different forms beyond simply form data. There are cookies, HTTP headers, client information and more. The following will introduce you to the primary APIs for reading from these input sources.
Form Input
The $app->{query}->param()
provides direct access to the form input parameters, either by a GET or POST request. Parameters are accessed by passing in the name of the form input parameter like so:
sub foo {
my $app = shift;
my $query = $app->{query};
my $input = $query->param('first_name');
# do something
}
Query Strings
The query string of a request refers to the data passed to a script via the URL used to access the script. It typically consists of a series of input name/value pairs like so:
http://www.somedomain.com/cgi-bin/mt/mt.cgi?__mode=method&foo=123&bar=abc
In the example above the query string refers to everything following the ?
. If you require direct access to this data, you can access it via the $app->querystring()
subroutine.
sub foo {
my $app = shift;
my $qs = $app->querystring();
foreach my $pair (split(/&/,$qs)) {
foreach my ($k,$v) (split(/=/,$pair)) {
MT->log('User entered $k=$v');
}
}
}
Path Info
If a query string refers to everything that follows a ?
in a URL, then the path info refers to everything that precedes it.
sub foo {
my $app = shift;
my $path = $app->pathinfo;
MT->log('Movable Type's mt.cgi lives at $path');
}
HTTP or HTTPS?
If you require to know if Movable Type, or the form submitted was send via a secure connection (e.g. HTTPS) then the $app->is_secure()
method will return true of false depending.
Remote User’s IP Address
The $app->remote_ip()
method returns the IP address of the connecting client, browser or user. Keep in mind that if the user is connecting via a proxy of some kind, or often from behind a corporate firewall, the IP address may not reflect their actual IP address. In such circumstances it is possible for a number of users to access the application to exhibit the same IP address. To account for this, please consult the TODO
configuration directive.
Document Root
The document root, or “doc root” as it is commonly referred to, is the location on the file system that your web server serves content from. To access the document root for your web server use the $app->document_root()
method.
Raw Request Content
If you need direct access to the data submitted via a form post, then the $app->request_content()
method will return all of the content following the HTTP headers of the request - verbatim.
Request Method
The $app->request_method()
returns either GET, PUT, POST or DELETE referring to the HTTP method used to submit form input.