Приглашаем посетить
Горький (gorkiy-lit.ru)

Q&A

Previous Table of Contents Next

Q&A

Q1:

The Telnet example doesn't work.

A1:

If Telnet fails to connect, make sure you're using Telnet against the Web server's name and that you're using the correct port, probably 80. You might need to look at the Telnet client's documentation to set the port number properly.

Another common problem is that you can't see yourself type. Some Telnet clients echo the characters to you, and some do not. Don't worry about this; just type carefully. The characters are still being transmitted carefully. After you type the GET line, make sure that you press the Enter key twice.

Q2:

How do I put more than one piece of information in an HTTP cookie?

A2:

The easiest way is to combine multiple items in a single cookie separated by a field separator, as in this example:


$cookie=cookie(-name => 'preferences',

    -value => 'bgcolor=blue,fgcolor=red,banners=no,java=no');


Later, when you retrieve the cookie, you can use split to separate the items:


    $cookie=cookie('preferences');

    @options=split(/,/,  $cookie);

# Now, make a hash with the option as the key,

# and the option's value as the hash value

foreach $option (@options) {

    ($key,$value)=split(/=/, $option)

    $Options{$key}=$value;

}


Q3:

How do I use cookies to track which links a user clicks on a Web page?

A3:

Before I give you the answer, you must realize that some people consider tracking an invasion of privacy. Having said that, let me show you the general method:

  1. Write your <a href> links so that they go to a CGI program, passing the real target URL as a parameter:

    
    <a
    
    
    
    href="http://server/cgi/redirect.pl?target=http://www.congo.com">Congo</a>
    
    

  2. The redirect.pl program in the example should use the CGI module's param function to get the real URL (http://www.congo.com) from the parameter target:

    
    $target_url=param('target');
    
    

  3. A cookie is then created with the target URL in the value, with a name you can search for later, such as:

    
    $tracking_cookie=cookie(-name => 'tracker',
    
                -value => $target_url,
    
                -expires => '+1w');
    
    

  4. The redirect is then sent to the browser along with the cookie:

    
    print redirect(-uri => $target_url,
    
            -cookie => $tracking_cookie);
    
    

    Later, when the browser returns to your Web site, you can look for the cookie named TRacker, which will contain the URL that the user visited when he or she left your site.

    There are other methods of tracking users, this is just one.

Q4:

Can I transmit a cookie while redirecting the browser to another page?

A4:

Yes. The CGI module's redirect function can also take a -cookie argument such as the header:


my $cookie=cookie(-name => 'target',

            -value => 'redirected to foo.html');

print redirect(-uri => "http://www.server.com/foo.html",

            -cookie => $cookie);


One complication: If you're using IIS as a web server, to send a cookie and a redirect at the same time you will need to enable Non-Parsed-Header scripts (usually done by having nph- at the beginning of the script name).

    Previous Table of Contents Next