I have this cgi perl script that sets a cookie and then checks to see if the cookie exists and prints out the value back to the browser (which all works fine). In my code below I would like to create a unique user id. I'm not passing any form or query string values to this cgi-bin script (the purpose of this script is not to do that), and I'm not sure what method to use to generate a unique identifier. I want the script to create the identifer and then initialize the cookie the next time the user comes back to the site or does a reload and then I will do some processing, like redirect the user to another site. Here is my code.
#!/opt/bin/perl
use CGI ':standard';
$query = new CGI();
$sweet = $query->cookie(-name=>'answers');#check for a cookie
#Get current Time
$current_time = localtime;
#does cookie exist
if ($sweet) {
print "Content-type: text/html\n\n";
#create html page info
print $query->start_html(-title=>"Session"),
$query->h1("cookie exists $sweet<BR>"),
$query->p("The current time is $current_time<BR>");
end_html;
#cookie doesn't exist, set one and send it to the browser
}else{
$cookie = $query->cookie(-name=>'answers',
-value=>'Register',
-expires=>'+1h',
-path=>'/cgi-bin/');
print $query->header(-type=>'text/html', -cookie=>$cookie);
print "<B>Cookie Set <b>";
}
exit;
edited: Thu Mar 13 15:56:05 2003
by jeffa - code tags
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.