Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cookies for ASP 1

Status
Not open for further replies.

obrien623

Programmer
Jun 2, 2000
1
US
I am very new to ASP and am trying to write out the number of times a site is visited.&nbsp;&nbsp;Everything in my code is okay except in the returnCust.asp file, I am unable to write out the variable &quot;howmany&quot;.&nbsp;&nbsp;What should I do?<br><br><A HREF="mailto:eek:brien623@excite.com">obrien623@excite.com</A><br><br><br>&lt;%@ language=&quot;jscript&quot;%&gt;<br>&lt;!-- #include file = &quot;cookiesLib.asp&quot; --&gt;<br>&lt;% Response.Buffer = true; %&gt;<br>&lt;html&gt;<br>&nbsp;&nbsp;&lt;head&gt;&lt;title&gt;Number of Visits&lt;/title&gt;&lt;/head&gt;<br>&nbsp;&nbsp;&lt;body background=&quot;bgb5.jpg&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;center&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;font size=&quot;4&quot; face=&quot;Verdana, Arial, Helvetica&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;b&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var key = Request.Form( &quot;uname&quot; );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var val =&nbsp;&nbsp;Request.Form( &quot;passw&quot; );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var howmany; <br><br>&nbsp;&nbsp;&nbsp;Response.Write(&quot;Your name is&quot; + &quot;&lt;br&gt;&quot; );<br>&nbsp;&nbsp;&nbsp;Response.Write(key + &quot;&lt;br&gt;&quot;);<br>&nbsp;&nbsp;&nbsp;Response.Write( getCookieValue( key ) +&nbsp;&nbsp;&nbsp;&nbsp;&quot;&lt;br&gt;&quot; );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( getCookieValue( key ) != val ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response.Write( &quot;Welcome! &quot; + key)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response.Write(&quot;, I see this is&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;your first visit to this site!&quot; +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;&lt;br&gt;&quot; );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;putCookie( key, val, 10 );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response.Cookies(&quot;howmany&quot;) = 1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}else {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response.Cookies(&quot;howmany&quot;)=Response.Cookies<br>&quot;howmany&quot;) + 1;<br> &nbsp;&nbsp;&nbsp;Response.Redirect(&quot;returnCust.asp&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/b&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/font&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/center&gt;<br>&nbsp;&nbsp;&lt;/body&gt;<br>&lt;/html&gt;<br><br><br><br><br>&lt;%@ language=&quot;jscript&quot;%&gt;<br>&lt;!-- #include file = &quot;cookiesLib.asp&quot; --&gt;<br>&lt;% Response.Buffer = true; %&gt;<br>&lt;html&gt;<br>&nbsp;&nbsp;&lt;head&gt;&lt;title&gt;ReturnCust.ASP for Cookies.HTML&lt;/title&gt;&lt;/head&gt;<br>&nbsp;&nbsp;&lt;body background=&quot;bgb2.jpg&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;center&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;font size=&quot;4&quot; face=&quot;Verdana, Arial, Helvetica&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;b&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;%&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var howmany;<br><br>Response.Write(&quot;Welcome Back to this page.&nbsp;&nbsp;&nbsp;You have visited this site &quot;);<br>Response.Write(&quot;&lt;br&gt;&quot;)<br>Response.Write(howmany)<br>Response.Write(&quot;&nbsp;&nbsp;times&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/b&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/font&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/center&gt;<br>&nbsp;&nbsp;&lt;/body&gt;<br>&lt;/html&gt;<br><br>
 
There are two cookies collections: response.cookies and request.cookies.&nbsp;&nbsp;When reading cookies, use the request object.&nbsp;&nbsp;When writing, use the response object:<br><br>Response.cookies(&quot;howmany&quot;) = request.cookies(&quot;howmany&quot;) + 1 <p>nick bulka<br><a href=mailto:nick@bulka.com>nick@bulka.com</a><br><a href= > </a><br>
 
I think you've missed out a line in the bottom page.<br><br>You declare the var howmany and then just response.write it. Thus it's going to be empty!<br><br>Used in conjunction with Nicks entry, you should be able to get it right! <p>Ben Marshalsea<br><a href=mailto: > </a><br><a href= > </a><br>
 
Something else you need to consider is the expiration of the cookie.&nbsp;&nbsp;Unless you specify otherwise, the cookie's lifetime is limited to the browser instance's lifetime.&nbsp;&nbsp;In other words,&nbsp;&nbsp;if you close the browser, the cookie is gone. If you want to have an accurate counter of the number of visits,&nbsp;&nbsp;set the expiration date to some time in the future. <p>nick bulka<br><a href=mailto: > </a><br><a href= > </a><br>
 
Erk, I can't remember the exact Syntax, but you can set it up so a Cookie never expires.<br><br>Maybe it's if you don't specify any.<br><br>This is how most online shops work, and thus your Cookies Folder expands until it takes over your entire hard drive!<br>BWAHAAHAHAAA...<br><br>Sorry, got carried away... (It's a Friday!!)<br><br> <p>Ben Marshalsea<br><a href=mailto: > </a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top