Emoji appears as black diamond when output from mid function - asp-classic

I have a very simple Classic ASP page here:
<%#LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
response.codepage = 65001
response.charset = "UTF-8"
%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
<title>testing 123</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
</head>
<body>
<%
my_string = "str 😁"
my_string_len = len(my_string)
response.write "<hr>my_string: " & my_string & "<hr>" & vbcrlf & vbcrlf
response.write "<hr>my_string_len: " & my_string_len & "<hr>" & vbcrlf & vbcrlf
for i = 1 to len(my_string)
c = mid(my_string, i, 1)
response.write "CHAR: " & ASC(c) & " = " & c & "<br>" & vbcrlf
next
%>
</body>
</html>
This appears when I run the page:
my_string: str 😁
my_string_len: 6
CHAR: 115 = s
CHAR: 116 = t
CHAR: 114 = r
CHAR: 32 =
CHAR: 15 = �
CHAR: 15 = �
I want to be able to split my_string into its constituent letters.
The test confirms that the ASP page can display native emoji characters, since the 😁 character renders correctly when I response.write my_string.
However, when the mid() function splits out the string, the emoji is split out into two black diamonds.
I presume that's because the emoji isn't a single character long, since the string str 😁 has a length of 6 rather than 5 characters.
I wondered if there is any way to be able to split out the string, in a way that emoji characters are still rendered correctly?

You need to make sure you are saving the ASP file in UTF-8 format because this is how IIS determines the data type (ASCII / UTF-8 or otherwise) inside the file. As emoji characters are double byte and text functions need to know to process using double bytes.
Many older ASP files were created using older versions of IDE's that might have saved the files in ASCII.

Related

How to return a link for given text using asp code

I am trying to return a link for the given text.
Here is the code:
<%
dim t
t = Request.QueryString("txt")
Response.Write("<a href = \" page2.html \" >" & t & "<\a>")
%>
Can anybody tell me what's wrong with the code?, it is not working.
To escape the " in vbscript, you have to double it. That is use "" for printing ".
Response.Write("<a href = "" page2.html "" >" & t & "<\a>")

ASP URLDecode - Incorrect Conversion

I have a website where google pics up some links on my site and sends throught the following URL
http://www.globalpropertyonline.net/test.asp?town=gand%EDa
I have one of the URLDecode Functions I found on the net to Decode the %ED
However, this seems not to be working the correct way.
The %ED should be this : í
So when I Decode it the word should be gandía
But instead I get the following word : gand�a
On the page where google gets the link, it is displayed correctly as gandía but it prompts me in webmaster tools that the following link has a error 500 and this is because when I try to take this name and sends it to the MYSQL query it crash with error 500 but if I would have had gandía then it worked !
So, my main problem is that I am getting the %ED sent as URLEncode as it seems and I want to Decode it before I send it to my database query.
In my ASP Page I am using UTF-8 Encodeing as follows.
<%# CodePage=65001 Language="VBScript"%>
<%
Response.CodePage = 65001
Response.CharSet = "utf-8"
This is the Code I am using to Decode it but instead if getting the gandía I get this : gand�a
any help will be much apreciated
<%# CodePage=65001 Language="VBScript"%>
<%
Response.CodePage = 65001
Response.CharSet = "utf-8"
MyTown = Request("town")
response.write URLDecode(MyTown)
Function URLDecode(str)
str = Replace(str, "+", " ")
For i = 1 To Len(str)
sT = Mid(str, i, 1)
If sT = "%" Then
If i+2 < Len(str) Then
sR = sR & _
Chr(CLng("&H" & Mid(str, i+1, 2)))
i = i+2
End If
Else
sR = sR & sT
End If
Next
URLDecode = sR
End Function
%>
It looks like you have not defined the page as UTF-8 client-side. You can this by adding this inside <head></head>:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

vbscript not retrieving all server variables

I have a website that was written in VBSCRIPT that I am moving over to VB.NET. Until I have time to get to rewriting some pages/applications, I would like to update some of the code so they work a bit better. I am trying to grab a server variable on the VBSCRIPT page that contains our username from the enterprise login.
I have 2 test pages here, one with language="VB" at the top and the other language="VBSCRIPT".
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Server Variables</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%
For Each var In Request.ServerVariables
Response.Write("<b>" + var + "</b>= " + Request.ServerVariables(var) + "<br />")
Next
%>
</div>
</form>
</body>
</html>
The VBSCRIPT page will list variables with no data when they have data on the VB page and in ALL_HTTP. Is there any way I can get VBSCRIPT to gather the correct info for all the server variables?
Thank You
EDIT: These are the variables that vary between VB and VBS
AUTH_USER, REMOTE_USER - logged in user on VBS, iis_anon on VB
REMOTE_PORT - not listed in VBS
The following are listed in VBS, but have no data
HTTP_SERVER_PROTOCOL
HTTP_SM_TRANSACTIONID
HTTP_SM_CLIENT_IP
HTTP_REQUEST_METHOD
HTTP_SM_SDOMAIN
HTTP_SM_REALM
HTTP_SM_REALMOID
HTTP_SM_AUTHTYPE
HTTP_SM_AUTHREASON
HTTP_SM_UNIVERSALID
HTTP_SM_AUTHDIROID
HTTP_SM_AUTHDIRNAME
HTTP_SM_AUTHDIRSERVER
HTTP_SM_AUTHDIRNAMESPACE
HTTP_SM_USER
HTTP_SM_USERDN
HTTP_SM_SERVERSESSIONID
HTTP_SM_SERVERSESSIONSPEC
HTTP_SM_TIMETOEXPIRE
HTTP_SM_SERVERIDENTITYSPEC
HTTP_REMOTE_USER
Also there is a custom on I wish to get that is used for our enterprise authentication.
I've had this issue before with custom HTTP headers that have underscores in the raw header name (which can be seen in the ALL_RAW server variable). The HTTP_ converts underscores to dashes (e.g., HTTP_USER_AGENT corresponds to an actual header called User-Agent). In order to retrieve the values of any headers that have underscores in their names, you have to use the HEADER_ prefix, which will search for the exact header name (e.g., HEADER_USER_AGENT will search for a header called User_Agent).
By the way, this blog post offers some more background information on why there are two prefixes.
here a classic ASP page i have running on various IIS versions.
For the user, i have this in most of my projects which makes userinfo available on the server and client side. If you are on a domain replace DMAIN with yours, otherwise you can drop the replace.
NTLM security has to be enabled (windows authentication) on your IIS site, no anonymous access.
The list of servarvariables returned by this code is different depending on OS and IIS versions, enabled features, settings etc. Some keys will have empty values, so eg REMOTE_PORT is not avaiable on mine (i use the standard 80 port) but SERVER_PORT is.
<%
Dim user
user = Replace(uCase(Request.ServerVariables("AUTH_USER")), "DOMAIN\", "")
%>
<script type="text/javascript">var user = <%=user%></script>
Here the contents of servervariables.asp
<html>
<%#language=VBScript%>
<%
With Response
.Expires=-1
.Clear
End With
dim teller
response.write "<h1>Servervariables</h1>"
response.write "<table>"
teller = 1
for each subkey in Request.Servervariables
response.write "<tr><td>"
response.write teller
response.write "</td><td>"
response.write (Request.Servervariables.Key(teller))
response.write "</td><td>"
response.write (Request.Servervariables.Item(teller))
response.write "</td></tr>"
teller = teller + 1
next
response.write "</table>"
response.write request.servervariables("path_translated") & "<br>"
response.write "USER cookie:" & Request.Cookies("USER") & "<br>"
response.write "USER Sessionvariable:" & Session("USER") & "<br>"
%>
</html>

How to convert a string to XML & Get that XML ChildNode values in Classic asp?

`Here I have converted one string to XML:
xmlString =
" <?xml version='1.0' encoding='UTF-8' standalone='yes'?>" & _
" <hub:notifications>" & _
" <hub:notificationId>728dc361-8b4f-4acc-ad2d-9a63125c5114</hub:notificationId>" & _
" <hub:notificationId>5b7c6989-ee27-422c-bbed-2f2c36136c5b</hub:notificationId>" & _
" <hub:notificationId>67d1fffe-ab3f-43e3-bb03-24926debe2dc</hub:notificationId>" & _
" </hub:notifications>"
objXML.LoadXml(xmlString)
set Node = objXML.selectSingleNode("hub:notifications/hub:notificationId")
i = 0
Count = 0
For Each Node In objXML.selectNodes("hub:notifications")
ReDim Preserve aryNotificationIDs (i + 1)
aryNotificationIDs(i) = Node.selectSingleNode("hub:notificationId").text
Count++
Next
Response.write Count
In above, I am not getting Count of Child nodes
and How to get the child node values.
Can any one help me?
Thanks,
Jagadi`
There are many problems with your posted code.
FirstWhat language are you using? There seems to be styles from VBScript and JScript. It is predominately VBScript so I'm going to assume that is what you meant to use throughout.
Second
The XML declaration needs to be the first characters in the string.
That is:
"<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" & _
not
" <?xml version='1.0' encoding='UTF-8' standalone='yes'?>" & _
Third
XML with namespaces requires an xml namespace declaration in the top-level node that use the namespace.
For example the root node.
<hub:notifications>
would become
<hub:notifications xmlns:hub='http://stackoverflow.com'>
But you would replace the stackoverflow URL with one appropriate to you.
Fourth
If you want to iterate through the child nodes of hub:notifications then you need to change the FOR deceleration to:
For Each Node In objXML.selectSingleNode("hub:notifications").childNodes
Fifth
i is not increasing in your loop, so you are setting aryNotificationIDs(1) to the different values of the nodes.
Sixth
Related to the first. There is no ++ operator in VBScript. And you don't need both i and Count in the For loop.
Additionally
You don't need to cycle through the nodes to get a count. You can use an xpath selector, and the length property. E.g. objXML.selectNodes("hub:notifications/hub:notificationId").length
Finally
I have taken you code and applied the above suggestions, I've also included an error checking part that checks that the xml has correctly loaded. The code below will output the count of hub:notificationId nodes, and list all the values in the array aryNotificationIDs. I've removed other superfluous code.
xmlString = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" & _
" <hub:notifications xmlns:hub='http://stackoverflow.com'>" & _
" <hub:notificationId>728dc361-8b4f-4acc-ad2d-9a63125c5114</hub:notificationId>" & _
" <hub:notificationId>5b7c6989-ee27-422c-bbed-2f2c36136c5b</hub:notificationId>" & _
" <hub:notificationId>67d1fffe-ab3f-43e3-bb03-24926debe2dc</hub:notificationId>" & _
" </hub:notifications>"
Set objXML = Server.CreateObject("Msxml2.DOMDocument")
objXML.LoadXml(xmlString)
If objXML.parseError.errorCode <> 0 Then
Response.Write "<p>Parse Error Reason: " & objXML.parseError.reason & "</p>"
Else
For Each node In objXML.selectSingleNode("hub:notifications").childNodes
ReDim Preserve aryNotificationIDs(i)
aryNotificationIDs(i) = node.text
i = i + 1
Next
Response.Write "<p>Count: " & i & "</p>"
For j = 0 to i - 1
Response.Write "<p>aryNotificationIDs(" & j & ") = " & aryNotificationIDs(j) & "</p>"
Next
End If

ASP - Printing the entire request contents

I'm debugging some ASP code and I need to get a quick printout of the current Request datastructure, which I believe is an array of key/value pairs.
I see that Request.Form("key") is the method for extracting individual elements.
Any tips on printing out the entire thing?
Try this
For Each item In Request.Form
Response.Write "Key: " & item & " - Value: " & Request.Form(item) & "<BR />"
Next
Working:
For x = 1 to Request.Form.Count
Response.Write x & ": " _
& Request.Form.Key(x) & "=" & Request.Form.Item(x) & "<BR>"
Next
I have other Classic ASP code snippets here:
https://github.com/RaviRamDhali/programming-procedure/tree/master/Snippets/ASP
Try a FOR/EACH loop:
for each x in Request.Form
Response.Write(x)
Next

Resources