NAME
CGI::Info - Information about the CGI environment
VERSION
Version 0.34
SYNOPSIS
All too often Perl programs have information such as the script's name hard-coded into their source. Generally speaking, hard-coding is bad style since it can make programs difficult to read and it reduces readability and portability. CGI::Info attempts to remove that.
Furthermore, to aid script debugging, CGI::Info attempts to do sensible things when you're not running the program in a CGI environment.
use CGI::Info;
my $info = CGI::Info->new();
# ...
SUBROUTINES/METHODS
new
Creates a CGI::Info object.
script_name
Returns the name of the CGI script. This is useful for POSTing, thus avoiding putting hardcoded paths into forms
use CGI::Info;
my $info = CGI::Info->new();
my $script_name = $info->script_name();
# ...
print "<form method=\"POST\" action=$script_name name=\"my_form\">\n";
script_path
Finds the full path name of the script.
use CGI::Info;
my $info = CGI::Info->new();
my $fullname = $info->script_path();
my @statb = stat($fullname);
if(@statb) {
my $mtime = localtime $statb[9];
print "Last-Modified: $mtime\n";
# TODO: only for HTTP/1.1 connections
# $etag = Digest::MD5::md5_hex($html);
printf "ETag: \"%x\"\n", $statb[9];
}
script_dir
Returns the file system directory containing the script.
use CGI::Info;
use File::Spec;
my $info = CGI::Info->new();
my $dir = $info->script_dir();
print 'HTML files are normally stored in ' . $info->script_dir() . '/' . File::Spec->updir() . "\n";
host_name
Return the host-name of the current web server, according to CGI. If the name can't be determined from the web server, the system's host-name is used as a fall back. This may not be the same as the machine that the CGI script is running on, some ISPs and other sites run scripts on different machines from those delivering static content. There is a good chance that this will be domain_name() prepended with either 'www' or 'cgi'.
use CGI::Info;
my $info = CGI::Info->new();
my $host_name = $info->host_name();
my $protocol = $info->protocol();
# ...
print "Thank you for visiting our <A HREF=\"$protocol://$host_name\">Website!</A>";
domain_name
Domain_name is the name of the controlling domain for this website. Usually it will be similar to host_name, but will lack the http:// prefix.
cgi_host_url
Return the URL of the machine running the CGI script.
params
Returns a reference to a hash list of the CGI arguments.
If we're not in a CGI environment (e.g. the script is being tested) then the program's command line arguments are used, if there are no command line arguments then they are read from stdin as a list of key=value lines.
Returns undef if the parameters can't be determined.
If an argument is given twice or more, then the values are put in a comma separated list.
The returned hash value can be passed into CGI::Untaint.
Takes two optional parameters: expect and upload_dir. The parameters are passed in a hash, or a reference to a hash. The latter is more efficient since it puts less on the stack.
Expect is a reference to a list of arguments that you expect to see and pass on. Arguments not in the list are silently ignored. This is useful to help to block attacks on your site.
Upload_dir is a string containing a directory where files being uploaded are to be stored.
The expect list and upload_dir arguments can also be passed to the constructor.
use CGI::Info;
use CGI::Untaint;
# ...
my $info = CGI::Info->new();
my %params;
if($info->params()) {
%params = %{$info->params()};
}
# ...
foreach(keys %params) {
print "$_ => $params{$_}\n";
}
my $u = CGI::Untaint->new(%params);
use CGI::Info;
# ...
my $info = CGI::Info->new();
my @allowed = ('foo', 'bar');
my $paramsref = $info->params(expect => \@allowed);
# or
my $paramsref = $info->params({
expect => \@allowed,
upload_dir = $info->tmpdir()
});
If the request is an XML request, CGI::Info will put the request into the params element 'XML', thus:
use CGI::Info;
...
my $info = CGI::Info->new();
my $paramsref = $info->params();
my $xml = $$paramsref{'XML'};
# ... parse and process the XML request in $XML
is_mobile
Returns a boolean if the website is being viewed on a mobile device such as a smart-phone. All tablets are mobile, but not all mobile devices are tablets.
is_tablet
Returns a boolean if the website is being viewed on a tablet such as an iPad.
as_string
Returns the parameters as a string, which is useful for debugging or generating keys for a cache.
protocol
Returns the connection protocol, presumably 'http' or 'https', or undef if it can't be determined.
tmpdir
Returns the name of a directory that you can use to create temporary files in.
The routine is preferable to "tmpdir" in File::Spec since CGI programs are often running on shared servers. Having said that, tmpdir will fall back to File::Spec->tmpdir() if it can't find somewhere better.
If the parameter 'default' is given, then use that directory as a fall-back rather than the value in File::Spec->tmpdir(). No sanity tests are done, so if you give the default value of '/non-existant', that will be returned.
Init allows a reference of the options to be passed.
use CGI::Info;
my $info = CGI::Info->new();
my $dir = $info->tmpdir(default => '/var/tmp');
my $dir = $info->tmpdir({ default => '/var/tmp' });
is_robot
Is the visitor a real person or a robot?
use CGI::Info;
my $info = CGI::Info->new();
unless($info->is_robot()) {
# update site visitor statistics
}
is_search_engine
Is the visitor a search engine?
use CGI::Info;
my $info = CGI::Info->new();
if($info->is_search_engine()) {
# display generic information about yourself
} else {
# allow the user to pick and choose something to display
}
AUTHOR
Nigel Horne, <njh at bandsman.co.uk>
BUGS
is_tablet() only currently detects the iPad and Windows PCs. Android strings don't differ between tablets and smart-phones.
Please report any bugs or feature requests to bug-cgi-info at rt.cpan.org
, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CGI-Info. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
SEE ALSO
HTTP::BrowserDetect
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc CGI::Info
You can also look for information at:
RT: CPAN's request tracker
AnnoCPAN: Annotated CPAN documentation
CPAN Ratings
Search CPAN
ACKNOWLEDGEMENTS
LICENSE AND COPYRIGHT
Copyright 2010-2012 Nigel Horne.
This program is released under the following licence: GPL