1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

CGI-Perl Tips

Discussion in 'Perl' started by pradeep, Oct 22, 2005.

  1. pradeep

    pradeep Team Leader

    Apr 4, 2005
    Likes Received:
    Trophy Points:
    Kolkata, India
    Home Page:
    How to direct a browser to display a different HTML page This is actually very simple to do in a CGI script. Instead of the usual header

    Content-type: text/html #make your script print this
    Location: URL to display #Don't forget the blank line afterwards. 
    print $query->redirect('http://somewhere.else/in/movie/land');
    using the CGI module.

    How to manipulate browser caching

    Using an Expires header is the best way of achieving this in most cases. Specify a date in the past, and the browser will think the page must be updated next time it requires it.

    You can also specify to certain browsers that they should not cache this document by returning a no-cache pragma.

    The pragma is returned as part of your header. For example, your script might return

    Content-type: text/html expires: Tuesday, 13-Jan-94 12:12:12 GMT 
    pragma: no-cache #(remainder of document)
    Creating HTTP Header

    print $query->header;
    print $query->header('image/gif');
    print $query->header('text/html','204 No response');
    print $query->header(-type=>'image/gif', -nph=>1, -status=>'402 
    Payment required', -expires=>'+3d', -cookie=>$cookie, 
    header() returns the Content-type: header. You can provide your own MIME type if you choose, otherwise it defaults to text/html. An optional second parameter specifies the status code and a human-readable message. For example, you can specify 204, ``No response'' to create a script that tells the browser to do nothing at all. If you want to add additional fields to the header, just tack them on to the end:
     print $query->header('text/html','200 
    OK','Content-Length: 3002');
    Fetching The Environment Variables

    Some of the more useful environment variables can be fetched through this interface. The methods are as follows:

    Return a list of MIME types that the remote browser accepts. If you give this method a single argument corresponding to a MIME type, as in $query->accept('text/html'), it will return a floating point value corresponding to the browser's preference for this type from 0.0 (don't want) to 1.0. Glob types (e.g. text/*) in the browser's accept list are handled correctly.

    Returns the HTTP_COOKIE variable, an HTTP extension implemented by Netscape browsers version 1.1 and higher. Cookies have a special format, and this method call just returns the raw form (?cookie dough). See cookie() for ways of setting and retrieving cooked cookies.

    Returns the HTTP_USER_AGENT variable. If you give this method a single argument, it will attempt to pattern match on it, allowing you to do something like $query->user_agent(netscape);

    Returns additional path information from the script URL. E.G. fetching /cgi-bin/your_script/additional/stuff will result in $query->path_info() returning "additional/stuff".

    As per path_info() but returns the additional path information translated into a physical path, e.g. ``/usr/local/etc/httpd/htdocs/additional/stuff''.

    Returns either the remote host name or IP address. if the former is unavailable.

    Return the script name as a partial URL, for self-refering scripts.

    Return the URL of the page the browser was viewing prior to fetching your script. Not available for all browsers.

    auth_type ()
    Return the authorization/verification method in use for this script, if any.

    server_name ()
    Returns the name of the server, usually the machine's host name.

    virtual_host ()
    When using virtual hosts, returns the name of the host that the browser attempted to contact.

    server_software ()
    Returns the server software and version number.

    remote_user ()
    Return the authorization/verification name used for user verification, if this script is protected.

    user_name ()
    Attempt to obtain the remote user's name, using a variety of different techniques. This only works with older browsers such as Mosaic. Netscape does not reliably report the user name!

    Returns the method used to access your script, usually one of 'POST', 'GET' or 'HEAD'.

    Creating a self-referencing URL which preserves state information:
    	$myself = $query->self_url; print "<A HREF=$myself>I'm talking 
    to myself.";
    self_url() will return a URL, that, when selected, will reinvoke this script with all its state information intact. This is most useful when you want to jump around within the document using internal anchors but you don't want to disrupt the current contents of the form(s). Something like this will do the trick.
    	 $myself = $query->self_url; print "<A HREF=$myself#table1>See 
    table 1"; print "<A HREF=$myself#table2>See table 2"; print "<A 
    HREF=$myself#yourself>See for yourself";
    If you don't want to get the whole query string, call the method url() to return just the URL for the script:
    	$myself = $query->url; print "<A HREF=$myself>No query string in 
    this baby!\n";
    You can also retrieve the unprocessed query string with query_string():
    	$the_string = $query->query_string;

Share This Page