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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If then else statement with cookies, data type mismatch

Status
Not open for further replies.

protomatteruk

Technical User
Mar 7, 2002
2
GB
Hi All,

I am pretty much a novice when it comes to VBScript and this is now officially driving me mad. When I upload the following code in an asp page to my web-server it just displays HTTP 500 error. It works locally on IIS and win2k. I think it has something to do with the CInt function and data type mismatches, but am unsure. If anyone can point me in the right direction I
would be v. grateful, or let me know where I can find samples of this kind of coding.

The basic idea is that if the cookie doesn't exist one is set with a count of 1 and the sendSMS() function is called. If a cookie does exist then check to see if its value is less than maxperday. If it is call the sendSMS()
function and increment the count by one. If it is equal or greater than maxperday, then don't send.

That's what I know I want in my head, but getting it to work here is proving more difficult than I imagined.

Any help greatly appreciated,
Colin Mc Mahon.


Code:
<%@LANGUAGE=&quot;VBSCRIPT&quot;%>
<%
dim numberSent
dim maxperday
maxperday = 10

function sendSMS()
 Set CDOMail = Server.CreateObject(&quot;CDONTS.NewMail&quot;)
 CDOMail.From = &quot;sms@whatever.co.uk&quot;
 CDOMail.To = &quot;me@sms.me.co.uk&quot;
 CDOMail.Subject = Request.Form(&quot;name&quot;)
 CDOMail.Body = Request.Form(&quot;msg&quot;)
 CDOMail.Send
 Set CDOMail = nothing
End Function

If Len (Request.Cookies(&quot;sentmessages&quot;)) <> 0 Then
 numberSent= CInt(Request.Cookies(&quot;sentmessages&quot;))
 If numberSent <= maxperday then
  Response.Cookies(&quot;sentmessages&quot;) = Request.Cookies(&quot;sentmessages&quot;) + 1
  sendSMS()
%>
<!--#include file=&quot;sent.asp&quot; -->
<%
 else
%>
<!--#include file=&quot;overQuota.asp&quot; -->
<%
 End if

else
Response.Cookies(&quot;sentmessages&quot;) = 1
Response.Cookies(&quot;sentmessagers&quot;).Expires = Date + 1
sendSMS()
%>
<!--#include file=&quot;sent.asp&quot; -->
<%
End If
%>

 
I haven't yet worked with cookies in an ASP script, but I do have one question. It looks like you're using the Len() function to determine whether or not a cookie is set. What would happen the Len() function tries to determine the length of a cookie that's not there? Could this be generating the 500 error?
 
Set your browser to give more meaningful errors.
(In IE untick 'friendly error messages')
codestorm
Fire bad. Tree pretty. - Buffy
Hey, I'm operating on a limited mental budget here.
<insert witticism here>
 
Hi,
Thanks for your comments, I have tried other methods of determining whether determining whether there is a cookie there or not, like
Code:
If Request.Cookies(&quot;sentmessages&quot;) = &quot;&quot;
etc but it doesn't make a difference. I did as you advised and set the browser to give me more meaningful errors, but all I got back was:
The server has encountered an error while loading an application during the processing of your request. Please refer to the event log for more detail information. Please contact the server administrator for assistance.

Which helps me not a jot.
Any more thoughts would be appreciated.
 
You might try putting in some response.write stmts to see how far your prog is getting before the problem arises.

Usually I put the #includes at the top. Investigate file= vs virtual= in your include stmts. The interpreter may not be able to fine your files. Looks like you are trying to avoid include unless you really need the code. I usually put subs that are used in various places in a small app in one util.asp file. Easier to maintian.

Your email function looks ok.

Does your request form immediately precedt this script? If not, this script will not know what you are talking about.

Let me know how it works out.

Good luck
 
Never trust input from the client to be &quot;anything&quot;, especially a valid number. Always &quot;bulletproof&quot;. Get your data into local variables first, the proceess it. You'll find it simplifies code, runs faster and is easier to debug.
Dim strNumberSent
' Fetch once, use twice
strNumber = Request.Cookies(&quot;sentmessages&quot;))
On Error resume next
numberSent = Cint(strNumber)
if Err.Number <> 0 then strSent = &quot;&quot; ' No Input
On error goto 0
If Len (strSent) <> 0 Then
If numberSent <= maxperday then
Generate Forms/Controls Resizing/Tabbing Class
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
OK I have re-written the code as per advice, (I think). I am still getting the same response from the server. I am a novice with this, so it's quite possible I haven't done what you meant me to. Here is what I have at the moment.

Code:
<%@LANGUAGE=&quot;VBSCRIPT&quot;%>
<%

function sendSMS()
	Set CDOMail = Server.CreateObject(&quot;CDONTS.NewMail&quot;)
	CDOMail.From = &quot;sms@me.co.uk&quot;
	CDOMail.To = &quot;me@sms.genie.co.uk&quot;
	CDOMail.Subject = Request.Form(&quot;name&quot;)
	CDOMail.Body = Request.Form(&quot;msg&quot;)
	CDOMail.Send
	Set CDOMail = nothing
End Function


dim maxperday
maxperday = 10

Dim strNumberSent
' Fetch once, use twice
strNumberSent = Request.Cookies(&quot;sentmessages&quot;)
On Error resume next
    numberSent = Cint(strNumberSent)
    if Err.Number <> 0 then strNumberSent = &quot;&quot; ' No Input
On error goto 0 
If Len (strNumberSent) <> 0 Then
    If numberSent <= maxperday then
		Response.Cookies(&quot;sentmessages&quot;) = numberSent + 1
		sendSMS()
response.write &quot;Sent&quot;
	else
response.write &quot;Over Quota&quot;	
	End if
else
Response.Cookies(&quot;sentmessages&quot;) = 1
Response.Cookies(&quot;sentmessages&quot;).Expires = Date + 1
sendSMS()
response.write &quot;Sent&quot;
End If
%>

The form is directly preceeding the asp page, and I have removed the includes in case it were they that were causing the problem.
Is it possible it's a permissions issue on the server side. Although that said I have no problem simply setting and retriving cookies.

Sorry about all this, but I just can't seem to get anywhere with it!!
 
You can't have a function in front of &quot;Main-line&quot; code. The Script is entered at the top and all code at the top to the first procedure is in a &quot;virtual&quot; main procedure. Once a Function, Sub or Class statement is encountered, all code must be in Function, Sub or Class blocks. That is your problem. Code starting at
dim maxperday
maxperday = 10
is not contained in a procedure and is not in front of all procedures so it cause an error during compilation. Generate Forms/Controls Resizing/Tabbing Class
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top