Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Cookie user ID queston

by koryw (Novice)
on Mar 13, 2003 at 15:54 UTC ( #242721=perlquestion: print w/replies, xml ) Need Help??

koryw has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
•Re: Cookie user ID queston
by merlyn (Sage) on Mar 13, 2003 at 16:10 UTC
Re: Cookie user ID queston
by hardburn (Abbot) on Mar 13, 2003 at 16:09 UTC

    Take a look at Data::UUID.

    Reinvent a rounder wheel.

    Note: All code is untested, unless otherwise stated

Re: Cookie user ID queston
by zby (Vicar) on Mar 13, 2003 at 16:09 UTC
    Try Data::Random - the function rand_chars() is just for you I gues.
Re: Cookie user ID queston
by Hero Zzyzzx (Curate) on Mar 13, 2003 at 18:00 UTC

    Maybe I'm not understanding the question, but in another thread (this one) you're using CGI::Session on the CPAN, which will take care of creating/assigning unique session ids for you automatically.

    -Any sufficiently advanced technology is
    indistinguishable from doubletalk.

      This is a different program and I trying a different method, basically as a backup plan if we decide not to use cgi::session

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://242721]
Approved by sschneid
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2021-09-23 05:29 GMT
Find Nodes?
    Voting Booth?

    No recent polls found