Category Archives: asp

Come rilevare un browser mobile in ASP

Nel tutorial di oggi, vi mostreremo come creare una funzione che permette di rilevare se l'utente sta utilizzando un browser mobile per visualizzare il vostro sito.

  2. < %
  3. Function Is_Mobile()
  4. Set Regex = New RegExp
  5. With Regex
  6. .Pattern = "(up.browser||mmp|symbian|smartphone|midp|wap|phone|windows ce|pda|mobile|mini|palm|ipad)"
  7. .IgnoreCase = True
  8. .Global = True
  9. End With
  10. Match = Regex.test(Request.ServerVariables("HTTP_USER_AGENT"))
  11. If Match then
  12. Is_Mobile = True
  13. Else
  14. Is_Mobile = False
  15. End If
  16. End Function
  17. %>
  19. //USAGE
  21. < %If Is_Mobile() then%>
  22. <h1>You are using a mobile device</h1>
  23. < %Else%>
  24. <h1>You are not using a mobile device</h1>
  25. < %End If%>

Validare una querystring con VB.NET

In questo semplice tutorial imparerete come validare i parametri di una querystring con VB.NET.

  2. Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
  3. ' Validate Querystrings
  4. ' Set raw querystring values to local variables, by definition,
  5. ' these are strings.
  6. Dim RawSubjectID As String = Request.QueryString("SubjectID")
  7. Dim RawGroupID As String = Request.QueryString("GroupID")
  8. Dim RawTypeID As String = Request.QueryString("TypeID")
  10. ' Check for empty string values (querystring value not passed) and
  11. ' validate the expected data type:
  12. ' Array: IsArray(<obj to validate>)
  13. ' Date: IsDate(</obj><obj to validate>)
  14. ' Integer: Int32.TryParse(<value to parse>, <variable to set if successful)
  15. '
  16. If (Not String.IsNullOrEmpty(RawSubjectID) And Int32.TryParse(RawSubjectID, SubjectID)) And _
  17. (Not String.IsNullOrEmpty(RawGroupID) And Int32.TryParse(RawGroupID, GroupID)) And _
  18. (Not String.IsNullOrEmpty(RawSubjectID) And Int32.TryParse(RawTypeID, TypeID)) Then
  20. ' Querysting validated, add code here...
  22. End If
  23. End Sub

Stringe pulite da codice maligno con asp

function RTESafe(strText)
    'returns safe code for preloading in the RTE
    dim tmpString
    tmpString = trim(strText)
    'convert all types of single quotes
    tmpString = replace(tmpString, chr(145), chr(39))
    tmpString = replace(tmpString, chr(146), chr(39))
    tmpString = replace(tmpString, "'", "'")
    'convert all types of double quotes
    tmpString = replace(tmpString, chr(147), chr(34))
    tmpString = replace(tmpString, chr(148), chr(34))
    tmpString = replace(tmpString, """", "\""")
    'replace carriage returns &amp; line feeds
    tmpString = replace(tmpString, chr(10), " ")
    tmpString = replace(tmpString, chr(13), " ")
    RTESafe = tmpString
end function

Script ASP per salvare/scaricare YouTube videos


<form method="POST" action="getrealurl.asp">

<input type="text" name="v" size="20" value="">
<input type="submit" value="Submit" name="B1">


'getrealurl.asp Updated as of June 29, 2007.

'Script by Lil Peck -
'You may use this in your applications but please leave acknowlegement for me.
'Finds real URL of YouTube flash video file.

'---check referring form page

Dim strDuh 'declaring the variable, a good habit
strDuh = Request.ServerVariables("HTTP_REFERER")'the string we want to compare to the items in the array
'response.write strDuh

Dim sMyArray(4)'what we name our array and how many items in it
'replace following urls with your form page urls
sMyArray(0) = ""
sMyArray(1) = ""
sMyArray(2) = ""
sMyArray(3) = ""

For Each i In sMyArray 'i can be any designation for the items in the array like z or bugguts or whatever

'now we need to set up a Case with with to compare our querystring to the items in the array
Select Case MatchArray 'give your case a name for identification purposes

Case YES

if i = strDuh then
Cal = "YES"

end if
End select


If Cal = "YES" then response.write""

If NOT Cal = "YES" then

'replace following urls with your form page url
response.redirect ""
end if

'---end check referring form page

if request.form("v") = "" then
response.redirect "videoform.asp"
end if

dim strVid
strVid = request.form("v")

dim vidID
vidID = Right(strVid, Len(strVid) - InStrRev(strVid, "=") ) 'gets id number from url

Server.ScriptTimeout = 240

Set HttpObj = Server.CreateObject("AspHTTP.Conn")

HttpObj.Url = "" & vidID

HttpObj.FollowRedirects = true

HttpObj.RequestMethod = "GET"

HttpObj.UserAgent = "Mozilla/2.0 (compatible; MSIE 3.0B; Windows NT)"

strResult = HttpObj.GetURL
curURL = HttpObj.Url


InitialString = curURL

Set RegularExpressionObject = New RegExp

With RegularExpressionObject
.Pattern = ""
.IgnoreCase = True
.Global = True
End With

ReplacedString = RegularExpressionObject.Replace(InitialString, "")

Set RegularExpressionObject = nothing

Set HttpObj = Server.CreateObject("AspHTTP.Conn")

HttpObj.Url = ReplacedString

HttpObj.FollowRedirects = true

HttpObj.RequestMethod = "GET"

HttpObj.UserAgent = "Mozilla/2.0 (compatible; MSIE 3.0B; Windows NT)"

strResult = HttpObj.GetURL

nextURL = HttpObj.Url


Set HttpObj = Server.CreateObject("AspHTTP.Conn")

HttpObj.Url = nextURL

HttpObj.FollowRedirects = true

HttpObj.RequestMethod = "GET"

HttpObj.UserAgent = "Mozilla/2.0 (compatible; MSIE 3.0B; Windows NT)"

strResult = HttpObj.GetURL

fnlURL = HttpObj.Url


'regex script from
'converts real flv file url from get_video to get_video_abc123.flv so user doesn't have to rename
InitialString = fnlURL

Set RegularExpressionObject = New RegExp

With RegularExpressionObject
.Pattern = "get_video?"
.IgnoreCase = True
.Global = True
End With

MyRep = "get_video_" & vidID & ".flv"

ReplacedString = RegularExpressionObject.Replace(InitialString, MyRep)

Set RegularExpressionObject = nothing



<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>asp script video downloader asphttp youtube</title>


<body style="font-family: Verdana">
<b><font size="4">ASP script to save/download YouTube videos (v. 4)</font></b><p>
<br><a href="<%
response.write ReplacedString %>">Click to download video file.</a><br>

<a href="savetosite.asp?vid=<%=vid%>&file=<% response.write ReplacedString %>">Click to save to server.</a><br>

savetosite.asp - OPTIONAL, allows the files to be saved directly to your website

<%@ Language=VBScript %>

'change folders and urls to your own
'if you use this option you may want to put in safeguards (referer check and so on) to prevent abuse and
'also change the variable strURL to be inputted by form rather than querystring
' Set the URL of the file you want to save
strURL = request.querystring("file")
strFileExtension = ".flv"
' Set the Local path on your server
strLocalPath = "D:\Clients\public_html\aspexamples\youtu\"
strLocalFileName = request.querystring("vid")

' Create Work Object
Set objXMLHTTP = Server.CreateObject("Msxml2.ServerXMLHTTP")
Set objBinaryStream = CreateObject("ADODB.Stream")

' Get the file
objXMLHTTP.Open "GET", strURL & strFileName & strFileExtension , false
objBinaryStream.Type = 1

objBinaryStream.Write objXMLHTTP.responseBody
' Save the file on the server
objBinaryStream.SaveToFile server.MapPath("./" & strLocalFileName & strFileExtension), 2

Set objXMLHTTP = Nothing
Set objBinaryStream = Nothing
response.write "Complete. <BR>" & strLocalFileName & strFileExtension &"<br>"
Response.Write("<a href=""download.asp?FileName=" &" & strLocalFileName & strFileExtension & ">" & vbNewline & _
"Download</a>" & vbNewline )



Mostrare un feed rss usando asp

Questo tutorial spiega come aggiungere contenuti dinamici da un feed 2.0. RSS è un formato XML per mostrare nuovi contenuti, update di siti web eblogs.

Il formato RSS 2.0
RSS 2.0 è semplicemente un file xml.In un fedd RSS,<item> viene ripetuto per ciascuna news. Il seguente è un esempio di feed rss. vedi Resources per le specifiche complete.

<rss version="2.0">
          <title>channel title</title>
          <link>link to channel</link>
               <title>item title</title>
               <link>link url</link>
               <guid>unique id</guid>
               <description>item description</description>

Leggi l'articolo completo:

Mostrare feed rss usando asp

In questo articolo vedremo come mostrare i nostri feed rss leggendo i dati dal db. Un feed rss è un file xml che ha dei tag particolari. Un feed rss è un qualcosa del tipo:

Un nuovo file rss deve iniziare con

<rss version=”0.92″>

che specifica la versione del file rss in uso. Tutte le informazioni che metteremo nel nostro file rss devono essere
racchiuse tra i tag :



Poi dobbiamo mettere un blocco di tag che identificano la provenienza delle informazioni

- <title></title> indica il titolo del documento rss
- <description></description> indica la descrizione
- <link></link> indica il sito di provenienza
- <language></language> indica la lingua
- <copyright></copyright> indica il copyright
- <managingEditor></managingEditor> indica il responsabile delle informazioni

e poi mettere le singole informazioni/notizie tra i tag. Infatti ogni nuova notizia deve iniziare tra i tag



Per maggiori info, potete leggere

Per leggere un feed rss è necessario istanziare un oggetto del tipo MSXML2.DOMDocument . questo oggetto ti permette di accedere a questi file XML tramite DOM (Document Object Model). Ad esempio per leggere un file RSS feed con l'oggetto XML DOM:

Set xmlDOM = Server.CreateObject("MSXML2.DOMDocument")
xmlDOM.async = False
xmlDOM.setProperty "ServerHTTPRequest", True

Fatto ciò realizziamo il nostro file asp

'prende tutti gli <item> tags nel feed
Set itemList = xmlDOM.getElementsByTagName("item")
strHTML = strHTML & "<ul>"
'esegui una iteraione
For Each item In itemList
  'Parsa i figli
  For each child in item.childNodes
    Select case lcase(child.nodeName)
        case "title"
          title = child.text
        case "link"
          link = child.text
        case "description"
          description = child.text
    End Select
  'costruisci l'output xml
  strHTML = strHTML & "<li>"
  strHTML = strHTML & "<a href='" & Server.HTMLEncode(link) & "'>"
  strHTML = strHTML & Server.HTMLEncode(title)  strHTML = strHTML & "</a>"
  strHTML = strHTML & "<br>"
  strHTML = strHTML & description
  strHTML = strHTML & "<br>&nbsp;"
  strHTML = strHTML & "</li>"
strHTML = strHTML & "</ul>"
Set xmlDOM = Nothing
Set itemList = Nothing