p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

proasp_howto thread: RE: Generate a unique session ID


Message #1 by "MIck Work" <mmcdonald@g...> on Wed, 31 Jan 2001 18:38:03 -0500
I forgot to mention, the database is an Access database.  Here is the solution I
ended up going with:



function logVisit()
     Randomize
     genid = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"

     V_ID = ""

     Do While len(V_ID) = 0
          ' Create 3 random characters that will start V_ID
          for count = 1 to 3
               x = Cint((Len(genid) * Rnd) + 1)
               V_ID = V_ID & Mid(genid,x,1)
          next

          ' Create a unique string starting with the 3 random characters we
created,
          ' the servers SessionID, and the current date and time (separating
these
          ' 3 sections with a period - not really necessary, but good for
visually
          ' identifying the 3 parts of the unique string)
          V_ID = Cstr(V_ID) & "." & Cstr(session.SessionID) & "." &
Cstr(year(date())) & Cstr(month(date())) & Cstr(day(date())) & Cstr(hour(now()))
 & Cstr(minute(now())) & Cstr(second(now()))

          ' Before we update session, we should count to make sure this
sessionID isnt already in the db
          SQLstr = "SELECT COUNT(*) AS rcount " & _
                     "FROM LOG " & _
                     "WHERE SESSIONID='" & Cstr(V_ID) & "'

          set cRS = dconn.execute(SQLstr, ,adCmdText)

          if not cRS("rcount") = 0 then
               ' This VID is already in the database, so reset V_ID to nothing
so the loop will go again
               V_ID = ""
               cRS.close
          else
               ' Otherwise, update the database and keep this value
               sqlstr = "INSERT INTO LOG (VDATE,SESSIONID) VALUES (' "&now&"',
'" & Cstr(V_ID) & "' ) ;"
               dconn.Execute sqlstr,,adcmdtext
               cRS.close
               set cRS = nothing
          end if

     Loop

     session("VID") = Cstr(V_ID)

end function






"Xu Gary" <Gary.Xu@p...> on 01/31/2001 11:39:36 AM

Please respond to "How To" <proasp_howto@p...>

To:   "How To" <proasp_howto@p...>
cc:    (bcc: Peter Foti)

Subject:  RE: Generate a unique session ID



Why don't you simply use an identity field in SQL table to do it?




-----Original Message-----
From: pdf@b... [mailto:pdf@b...]
Sent: Wednesday, January 31, 2001 7:59 AM
To: How To
Subject: Generate a unique session ID




I need to create a unique session ID for each visitor, and store this ID
(along
with some other info) in a database.  The way I have been doing this is to
see
if the session variable "VID" had a length greater than zero.  If not, then
it
would call the function below (which ultimately sets session("VID") and puts
this into the database).  I have modified it some from what it was, but the
problem that I was seeing was that the same session ID variable was used
multiple times in the database.  Because of that, I added the section below
that
counts the number of records with this session ID before giving it to the
new
visitor.

Is there a better way to do this?  Any problems with using Randomize like
this?

Note, dconn is a connection to the database setup before this function call.
Thanks,
Peter Foti


function logVisit()
     Randomize
     genid 
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"
     V_ID = ""

     Do While len(V_ID) = 0
          ' Generate a 20 character ID
          for count = 1 to 20
               x = Cint((Len(genid) * Rnd) + 1)
               V_ID = V_ID & Mid(genid,x,1)
          next

          ' Before we update session, we should count to make sure this
sessionID isnt already in the db
          SQLstr = "SELECT COUNT(*) AS rcount " & _
                     "FROM LOG " & _
                     "WHERE SESSIONID='" & Cstr(V_ID) & "'"

          set cRS = dconn.execute(SQLstr, ,adCmdText)

          if not cRS("rcount") = 0 then
               ' This VID is already in the database, so reset V_ID to
nothing
so the loop will go again
               V_ID=""
               cRS.close
          else
               sqlstr = "INSERT INTO LOG (VDATE,SESSIONID) VALUES ('
"&now&"',
'"& V_ID &"' ) ;"
               dconn.Execute sqlstr,,adcmdtext
               cRS.close
               set cRS = nothing
          end if

     Loop
     session("VID") = Cstr(V_ID)
end function


  Return to Index