cookies....

LukaszR1

Fully Optimized
Messages
2,833
Hey there, i need help regarding Cookies using .ASP pages.

So what i am required to do is this, I need to be able to read the cookie and place the values from the cookie into the web page textboxes so they are displayed when the page is opened.

Here is the code i have up to now;
<%
Response.Cookies("Log")
Response.Cookies("Log").Expires=DATE() + 5

'setup some variables
Dim UserName, Password, Address
'read a cookie file. First the User Name
UserName=Request.Cookies("UserName")
'Next the Password
Password=Request.Cookies("Password")
'Next the Address
Address=Request.Cookies("Address")






%>


<html>
<head>
<title>Accounts</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF">
<h4 align="center"><font face="Arial, Helvetica, sans-serif" size="5"><b><font color="#990000">Accounts
</font></b></font><font face="Arial, Helvetica, sans-serif"><br>
Client Accounts</font></h4>

<form method="post" action="">
<table width="96%" border="0" align="center">
<tr>
<td width="37%">User Name :</td>
<td width="63%">
<input type="text" name="txtname" size="40">

</td>
</tr>
<tr>
<td width="37%">Password:</td>
<td width="63%">
<input type="password" name="txtpassword">
</td>
</tr>
<tr>

</tr>
<tr>
<td width="37%">Address:</td>
<td width="63%">
<input type="text" name="txtaddress">
</td>
</tr>
<tr>
<td width="37%"> </td>
<td width="63%">
<input type="submit" name="Submit" value="Save Data">

</td>
</tr>
<tr>
<td width="37%"> </td>
<td width="63%"> </td>
</tr>
</table>
</form>
<h4 align="center"><font size="1" face="Arial, Helvetica, sans-serif">Please show the lab assistant when completed
</body>
</html>

Now, my cookie is called "Log". How do i view the cookies content? Also, when i visit the .asp page in my web browser, and input all the fields...and choose to save the log in info...when i reload the page..its all there except for the address field. Could someone double check my coding please.

Any help would be appreciated. Thanks.
 
for the text on your input's:

<input type="text" id="uName" name="uName" width="20%"><%=Request.Cookies("Log")("uName")%></input>

where fieldName is the name of the input element.

When you create the cookie, you should add different "items" to the single cookie, instead of creating several different ones (unless you're supposed to). So for example When you are creating your cookies at the top, check to make sure a cookie hasn't been set first:
Code:
Dim uName As String = ""
uName = Request.Cookies("Log")("uName")
if uName.Length > 0 then
 ' Do nothing, we're all set here
Else
 'The cookie hasn't been set yet, so try to set the cookies value to whatever they put in the form.
  Request.Cookies("Log")("uName") = Request.Form("uName")
end if
Hope that helps.
 
oo...ok i think i understand what you are doing there. Thanks for your input, i will rewrite the code and test it out. I'll let you know how it goes...and whether or not i get the full marks for the lab.

Thanks again.
 
Back
Top Bottom