Posts Tagged ‘ ASP

ASP randomize number

ASP, set randomize


<%
randomize
StoreNumber = int(rnd*100)+1
%>

  • result is a number between 1 and 100

ASP Select description from large string

You can use below code to select the first n of chars from a string.

This can also be done from an ASP string. (not tested but the command is still left.) – Select LEFT(ColumName,1) from TableName where id = ‘100’ ?? Anyone care to confirm?


<%
String1="MyText"
response.write(LEFT(String1,3))
%>

  • Result is MyT

Replace vbCrLf with <br>

When you create a textara and then save it to a DB, When you go to use it later on the output page, you will find

that if you used the return key when you wrote your textarea is now on one line. It seems to have foggtten the returns, unless you put it back into a textara.

When in a textarea the return key must be key “vbCrLF” but when in html must be “<br>”. Use this code to switch between the two.

  • replace(inputtext,lookfor,replease with)

<%
Replace(StoredVAR,vbCrLf,"<br>")
%>

Make form input safe from SQL Injection

  • Add the following code to a global Function page or the header page

<%
Function clean(inputtext)
dim badChars
clean = inputtext
badChars = array("select", "drop", ";", "--", "insert", "delete", "xp_", "=", "'", ":")
for i = 0 to uBound(badChars)
clean = replace(clean , badChars(i), "")
next
end Function
%>

  • The following code is a static use of the Function

<%
RawText = "asdasd--ad;ddelete=,qw:"
CleanText = clean(RawText)
response.write(RawText & "<br>" & CleanText)
%>

  • output text will have none of the banned chars.

Send emails via objMessage Function (ASP)

This code I have used to send emails from pages like contact.asp

  • Config for Function (incude in global config file or header)

<%
ConfigMailActive = "true"
ConfigMailUsername = "mail"
ConfigMailPassword = "password"
configMailServer = "mail.server"
ConfigMailPort = cint("25")
%>

  • Function (Include in global function file or header)

<%
Function SendEmail(FEmailTo,FEmailFrom,FEmailSub,FEmailMsg)
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = FEmailSub
objMessage.From = FEmailFrom
objMessage.To = FEmailTo
objMessage.htmlBody = FEmailMsg

'==This section provides the configuration information for the remote SMTP server.

objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 1

'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = configMailServer

'Type of authentication, NONE, Basic (Base64 encoded), NTLM
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoencoded

'Your UserID on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = ConfigMailUsername

'Your password on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = ConfigMailPassword

'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = ConfigMailPort

'Use SSL for the connection (False or True)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False

'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60

objMessage.Configuration.Fields.Update

'==End remote SMTP server configuration section==

IF ConfigMailActive="true" then
objMessage.Send
End IF
set objMessage = nothing
End Function
%>

  • Place at point of email

<%
StoreETo = "email@address.co.uk"
StoreEFrom = "email@address.co.uk"
StoreESubject = "Subject of email"
StoreEmsg = "Message String"
call SendEmail(StoreETo,StoreEFrom,StoreESubject,StoreEmsg)
%>

List every ServerVariable stored by the webserver

Normaly added to the bottom of your ASP code (footer.asp) for testing only.


<%
For Each var In Request.ServerVariables
Response.Write var & " = " & Request.ServerVariables(var) & "<br>"
Next
%>

Output is as follows

<span style="font-size: x-small;">ALL_HTTP:<span style="font-size: x-small;">HTTP_ACCEPT<span style="font-size: x-small;">:*/* HTTP_ACCEPT_LANGUAGE:en-gb HTTP_CONNECTION:Keep-Alive HTTP_HOST:mydomain.co.uk HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729) HTTP_COOKIE:ASPSESSIONIDAQSATTBS=BLADACMABJAGMKJPDFPPBONA HTTP_ACCEPT_ENCODING:gzip, deflate
ALL_RAW:Accept: */* Accept-Language: en-gb Connection: Keep-Alive Host: mydomain.co.uk User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729) Cookie: ASPSESSIONIDAQSATTBS=BLADACMABJAGMKJPDFPPBONA Accept-Encoding: gzip, deflate
APPL_MD_PATH:/LM/W3SVC/1/ROOT
APPL_PHYSICAL_PATH:c:\inetpub\wwwroot\
AUTH_PASSWORD:
AUTH_TYPE:
AUTH_USER:
CERT_COOKIE:
CERT_FLAGS:
CERT_ISSUER:
CERT_KEYSIZE:
CERT_SECRETKEYSIZE:
CERT_SERIALNUMBER:
CERT_SERVER_ISSUER:
CERT_SERVER_SUBJECT:
CERT_SUBJECT:
CONTENT_LENGTH:0
CONTENT_TYPE:
GATEWAY_INTERFACE:CGI/1.1
HTTPS:off
HTTPS_KEYSIZE:
HTTPS_SECRETKEYSIZE:
HTTPS_SERVER_ISSUER:
HTTPS_SERVER_SUBJECT:
INSTANCE_ID:1
INSTANCE_META_PATH:/LM/W3SVC/1
LOCAL_ADDR:127.0.0.1
LOGON_USER:
PATH_INFO:/index.asp
PATH_TRANSLATED:c:\inetpub\wwwroot\index.asp
QUERY_STRING:
REMOTE_ADDR:127.0.0.1
REMOTE_HOST:127.0.0.1
REMOTE_USER:
REQUEST_METHOD:GET
SCRIPT_NAME:/index.asp
SERVER_NAME:mydomain.co.uk
SERVER_PORT:80
SERVER_PORT_SECURE:0
SERVER_PROTOCOL:HTTP/1.1
SERVER_SOFTWARE:Microsoft-IIS/5.1
URL:/index.asp
HTTP_ACCEPT:*/*
HTTP_ACCEPT_LANGUAGE:en-gb
HTTP_CONNECTION:Keep-Alive
HTTP_HOST:mydomain.co.uk
HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
HTTP_COOKIE:ASPSESSIONIDAQSATTBS=BLADACMABJAGMKJPDFPPBONA
HTTP_ACCEPT_ENCODING:gzip, deflate