Prevent hotlinking, detect referrer

Discussion in 'C#' started by eXe, Sep 16, 2006.

  1. #1
    I'm using a script to force the download of a pdf file.

    The script:
    <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
    <%
    '--------------------------------------------
    Response.Buffer = True
    Dim strFilePath, strFileSize, strFileName
    
    Const adTypeBinary = 1
    
    strFilePath = "D:\path\pdffile.pdf"
    'strFileSize = optional
    strFileName = "pdffile.pdf"
    
    Response.Clear
    
    '*******************************
    ' Requires MDAC 2.5 to be stable
    ' I recommend MDAC 2.6 or 2.7
    '*******************************
    
    Set objStream = Server.CreateObject("ADODB.Stream")
    objStream.Open
    objStream.Type = adTypeBinary
    objStream.LoadFromFile strFilePath
    
    strFileType = lcase(Right(strFileName, 4))
    
    ' Feel Free to Add Your Own Content-Types Here
    Select Case strFileType
    Case ".pdf"
    ContentType = "application/pdf"
    End Select
    
    Response.AddHeader "Content-Disposition", "attachment; filename=" & strFileName
    Response.AddHeader "Content-Length", strFileSize
    ' In a Perfect World, Your Client would also have UTF-8 as the default
    ' In Their Browser
    Response.Charset = "UTF-8"
    Response.ContentType = ContentType
    
    Response.BinaryWrite objStream.Read
    Response.Flush
    
    objStream.Close
    Set objStream = Nothing
    
    %> 
    Code (markup):
    Now I want this asp file, let's call it download.asp, to be unable to be hotlinked to, and it shouldn't load if someone just types the url into their browser. What do I add?

    Help appreciated, thanks.
     
    eXe, Sep 16, 2006 IP
  2. ludwig

    ludwig Notable Member

    Messages:
    2,253
    Likes Received:
    66
    Best Answers:
    0
    Trophy Points:
    225
    #2
    you could try passing a session variable to download.asp, if lets say
    if session("check")="" then
    response.redirect "/"
    else
    your script
    end if

    ope works for you
     
    ludwig, Sep 16, 2006 IP