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.

  1.  
  2. < %
  3. Function Is_Mobile()
  4. Set Regex = New RegExp
  5. With Regex
  6. .Pattern = "(up.browser|up.link|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. %>
  18.  
  19. //USAGE
  20.  
  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%>
  26.  

Validare una querystring con VB.NET

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

  1.  
  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")
  9.  
  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
  19.  
  20. ' Querysting validated, add code here...
  21.  
  22. End If
  23. End Sub
  24.  

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

'videoform.asp

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

<p>
<input type="text" name="v" size="20" value="http://www.youtube.com/watch?v=ro5Xl4qLl8o">
<input type="submit" value="Submit" name="B1">

</p>
</form>

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

<%
'Script by Lil Peck lilpeck@gmail.com - http://equineexpo.com/aspexamples/youtu/
'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) = "http://equineexpo.com/aspexamples/youtu/videoform.asp"
sMyArray(1) = "http://www.equineexpo.com/aspexamples/youtu/videoform.asp"
sMyArray(2) = "http://qhtimes.com/aspexamples/youtu/videoform.asp"
sMyArray(3) = "http://www.qhtimes.com/aspexamples/youtu/videoform.asp"

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

Next

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

If NOT Cal = "YES" then

'replace following urls with your form page url
response.redirect "http://equineexpo.com/aspexamples/youtu/videoform.asp"
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 = "http://www.youtube.com/v/" & 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 = "http://www.youtube.com/jp.swf"
.IgnoreCase = True
.Global = True
End With

ReplacedString = RegularExpressionObject.Replace(InitialString, "http://www.youtube.com/get_video")

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 http://authors.aspalliance.com/brettb/VBScriptRegularExpressions.asp
'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

%>

<html>

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

</head>

<body style="font-family: Verdana">
<b><font size="4">ASP script to save/download YouTube videos (v. 4)</font></b><p>
<b>
<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>
<p>

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
objXMLHTTP.Send()
objBinaryStream.Type = 1

objBinaryStream.Open
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>http://equineexpo.com/aspexamples/youtu/" & strLocalFileName & strFileExtension &"<br>"
Response.Write("<a href=""download.asp?FileName=http://equineexpo.com/aspexamples/youtu/" & http://equineexpo.com/aspexamples/youtu/" & strLocalFileName & strFileExtension & ">" & vbNewline & _
"Download</a>" & vbNewline )

Response.write(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">
     <channel>
          <title>channel title</title>
          <link>link to channel</link>
          <description>description</description>
          <language>en-us</language> 
          <item>
               <title>item title</title>
               <link>link url</link>
               <guid>unique id</guid>
               <description>item description</description>
          </item>
     </channel>
</rss>

Leggi l'articolo completo: http://www.codebeach.com/tutorials/displaying-rss-feeds-using-asp.asp

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 :

<channel>

</channel>

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

<item>

</item>

Per maggiori info, potete leggere http://www.sastgroup.com/tutorials/cosa-e-un-feed-rss.

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
xmlDOM.Load("file_export.asp")

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
  Next
  '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>"
Next
strHTML = strHTML & "</ul>"
Set xmlDOM = Nothing
Set itemList = Nothing
Response.Write(strHTML)