Connecting Tech Pros Worldwide Forums | Help | Site Map

Using Cookies and Session ID's

Member
 
Join Date: Jul 2008
Posts: 43
#1: Jul 29 '08
How to store username and password,cookies

I want to create cookies in which the text written by anyone in textbox should be stored?

I have this code:-
Expand|Select|Wrap|Line Numbers
  1.  <% 
  2. dim numvisits
  3. response.cookies("NumVisits").Expires=date+365 
  4. numvisits=request.cookies("NumVisits")
  5.  
  6. if numvisits="" then
  7. response.cookies("NumVisits")=1
  8. response.write("Welcome! This is the first time you are visiting this Web page.")
  9. else
  10. response.cookies("NumVisits")=numvisits+1
  11. response.write("You have visited this ")
  12. response.write("Web page " & numvisits)
  13. if numvisits=1 then
  14. response.write " time before!"
  15. else
  16. response.write " times before!"
  17. end if
  18. end if
  19. %>
  20.  

DrBunchman's Avatar
Moderator
 
Join Date: Jan 2008
Location: Winchester, UK
Posts: 930
#2: Jul 29 '08

re: Using Cookies and Session ID's


Then all you need to do is put a textbox in a form with a submit button. Once it's submitted you can request the contents of the textbox and assign it to the cookie variable.

Hope this helps,

Dr B
Member
 
Join Date: Jul 2008
Posts: 43
#3: Jul 29 '08

re: Using Cookies and Session ID's


Quote:

Originally Posted by DrBunchman

Then all you need to do is put a textbox in a form with a submit button. Once it's submitted you can request the contents of the textbox and assign it to the cookie variable.

Hope this helps,

Dr B

I have both in my form, textbox as well as submt button
Is this ok?

response.cookies("a")=b

If ok, then where will be the cookie variable, in a or in b??
DrBunchman's Avatar
Moderator
 
Join Date: Jan 2008
Location: Winchester, UK
Posts: 930
#4: Jul 30 '08

re: Using Cookies and Session ID's


Say you have the following in your html:
Expand|Select|Wrap|Line Numbers
  1.  
  2. <form name="Form1" method="post" action="thispage.asp">
  3.       <input name="b" type="text" />
  4.       <input type="submit" />
  5. </form>
Then you can use this to store the value typed into the text box as a cookie once the form is submitted:
Expand|Select|Wrap|Line Numbers
  1.  Response.Cookies("a") = Request.Form("b")
And you can retrieve the value of the cookie on a subsequent visit to the page using:
Expand|Select|Wrap|Line Numbers
  1.  c = Request.Cookies("a")
Does this make sense?

Dr B
Member
 
Join Date: Jul 2008
Posts: 43
#5: Aug 4 '08

re: Using Cookies and Session ID's


Dr B,

Thanx,It Works.

I also want u help in coding of session ID's. I want to create different id for every session opened by each user.

Could u Plz help me...?
DrBunchman's Avatar
Moderator
 
Join Date: Jan 2008
Location: Winchester, UK
Posts: 930
#6: Aug 5 '08

re: Using Cookies and Session ID's


No problem.

A Session ID is automatically created at the beginning of a user's Session and can be retrieved with the following code snippet:
Expand|Select|Wrap|Line Numbers
  1. Dim sSessionID
  2. sSessionID = Session.SessionID
Hope this helps.

Dr B
Member
 
Join Date: Jul 2008
Posts: 43
#7: Aug 7 '08

re: Using Cookies and Session ID's


Your code helped me a lot.But my last question to u------

And where could i see the Session Id??

Thanks Dr B
DrBunchman's Avatar
Moderator
 
Join Date: Jan 2008
Location: Winchester, UK
Posts: 930
#8: Aug 7 '08

re: Using Cookies and Session ID's


What do you mean by where can you see it? You can retrieve it using the code above and do what you like with it, including writing it to the browser if required.

Have I misunderstood?

Dr B
Member
 
Join Date: Jul 2008
Posts: 43
#9: Aug 7 '08

re: Using Cookies and Session ID's


Thanks Dr B,

but one thing i want to ask, where could i check this session Id
DrBunchman's Avatar
Moderator
 
Join Date: Jan 2008
Location: Winchester, UK
Posts: 930
#10: Aug 7 '08

re: Using Cookies and Session ID's


I don't understand what you mean by 'check' - check it against what?
Member
 
Join Date: Jul 2008
Posts: 43
#11: Aug 9 '08

re: Using Cookies and Session ID's


means where could i see that which session IDs is allotted to which session???
DrBunchman's Avatar
Moderator
 
Join Date: Jan 2008
Location: Winchester, UK
Posts: 930
#12: Aug 10 '08

re: Using Cookies and Session ID's


Ah I see. I don't think that you can 'see' this information anywhere but you can create the facility to view the information yourself.

It depends what sort of information you want to store about each session. If it's just a list of session ID's then you can probably use an array but if you want to store other information about each users session then you might need to use a database.

Use the Session_OnStart and Session_OnEnd events in the Global.asa file to add and remove information from your session table.

Expand|Select|Wrap|Line Numbers
  1. Sub Session_OnStart ()
  2.      Application.Lock
  3.      ' Add some code here to store the relevant information somewhere.
  4.      Application.UnLock
  5. End Sub
  6.  
  7. Sub Session_OnEnd ()
  8.      Application.Lock
  9.      ' Add some code here to remove the information.
  10.      Application.UnLock
  11. End Sub
  12.  
Hope this helps,

Dr B
Reply