Making email addresses safe from bots on a webpage?

前端 未结 22 1835
不知归路
不知归路 2020-12-02 08:09

When placing email addresses on a webpage do you place them as text like this:

joe.somebody@company.com

or use a clever trick to try and fo

22条回答
  •  遥遥无期
    2020-12-02 08:28

    This is what we use (VB.NET):

    Dim rxEmailLink As New Regex("]*mailto:\b[^>]*>(.*?)")
    Dim m As Match = rxEmailLink.Match(Html)
    While m.Success
        Dim strEntireLinkOrig As String = m.Value
        Dim strEntireLink As String = strEntireLinkOrig
        strEntireLink = strEntireLink.Replace("'", """") ' replace any single quotes with double quotes to make sure the javascript is well formed
        Dim rxLink As New Regex("(]*mailto:)([\w.\-_^@]*@[\w.\-_^@]*)(\b[^>]*?)>(.*?)")
        Dim rxLinkMatch As Match = rxLink.Match(strEntireLink)
        Dim strReplace As String = String.Format("", _
                    RandomlyChopStringJS(rxLinkMatch.Groups(1).ToString), _
                    ConvertToAsciiHex(rxLinkMatch.Groups(2).ToString), _
                    rxLinkMatch.Groups(3), _
                    ConvertToHtmlEntites(rxLinkMatch.Groups(4).ToString))
        Result = Result.Replace(strEntireLinkOrig, strReplace)
        m = m.NextMatch()
    End While
    

    and

        Public Function RandomlyChopStringJS(ByVal s As String) As String
            Dim intChop As Integer = Int(6 * Rnd()) + 1
            Dim intCount As Integer = 0
            RandomlyChopStringJS = ""
            If Not s Is Nothing AndAlso Len(s) > 0 Then
                For Each c As Char In s.ToCharArray()
                    If intCount = intChop Then
                        RandomlyChopStringJS &= "'+'"
                        intChop = Int(6 * Rnd()) + 1
                        intCount = 0
                    End If
                    RandomlyChopStringJS &= c
                    intCount += 1
                Next
            End If
        End Function
    

    We override Render and run the outgoing HTML through this before it goes out the door. This renders email addresses that render normally to a browser, but look like this in the source:

    
    

    Obviously not foolproof, but hopefully cuts down on a certain amount of harvesting without making things hard for the visitor.

提交回复
热议问题