Easy Cross Domain Cookies (Sharing cookies between domains)

I own several websites that need memberships to post comments, and recently I wanted the ability to have a single login - so once the user is logged into one site they are automatically logged into the others.

Ideally, I could just write the login cookies for both domains from one location, or somehow share the same login cookie between the domains - but you quickly come up against browser security which (for good reason) doesn’t allow this sort of thing.

There are plenty of ways of solving this problem - but this is the simplest I could find. It uses an IFrame to set cookies on the foreign domain.

In the example, you have Domain A which the visitor is currently on, and Domain B, which you want to set cookies on.

Add a page ‘FrameLogin.aspx’ to your Domain B website.


Public Partial Class FrameLogin
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim sUserID As String

        HttpContext.Current.Response.AddHeader(”p3p”, “CP=\”"IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\”"”)

        sUserID = Request.QueryString(”userid”)

        Dim oCookie As New HttpCookie(”UserID”)
        oCookie.Value = sUserID
        oCookie.Expires = DateTime.Now.AddDays(1000)
        HttpContext.Current.Response.Cookies.Add(oCookie)
        oCookie = Nothing

    End Sub

End Class

Note that the p3p header has been added, this is to allow 3rd party cookies.

To call this page, we insert code into the appropriate ‘Login’ function in your Domain A website.


    Private Function Login(ByVal sUserName As String, ByVal sPassword As String) As Boolean
        Dim iUserID As Integer

        iUserID = CheckLoginOKAndGetUserdID(sUserName, sPassword)

        If iUserID <> 0 Then
            Login = True
            Response.Write(”<IFRAME style=’WIDTH:1px;HEIGHT:1px’ src=’http://www.DomainB.com/FrameLogin.aspx’ frameBorder=’0′></IFRAME>”)
        Else
            Login = False
        End If

    End Function

Once the cookie has been set on Domain B I can use it to auto-login my visitors when they get there.

Also: For simplicity, I am using an IFrame to call a page. If I wanted faster execution I could substitute an iHttpHandler for the page.

We Got Domain - over 10,000 aged domains for sale

We Got Domain is a quick site that I built to make buying domains from forums like dnforum, digitalpoint etc easier. It extracts domain names from the sale threads and then finds out extra info like Backlinks, Pagerank, Creation date etc. Domains can only be bought on the forums so hopefully We Got Domain will save time for people buying domains while driving extra traffic to the forums (and they get tons of do-follow links as a small bonus).

The site was partly inspired by a the post Purchasing Domains on EarnersBlog.com as well as Shoemoney Marketplace and bizmp.com

I hope you find the site useful - send me an email if you have any suggestions or improvements.

Your own Lolcats widget (I Can Has Cheezburger?)

OK, I’m the first to admit that the whole lolcats thing might not be for everyone. But it’s for me. If it’s for you, too, then enjoy :)
If you would like this widget on your site/blog, just add the code:
<script src='http://lolcats.com.89.seekdotnet.com' type='text/javascript'></script>
And here’s how it was done:


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim sCacheKey As String = "CACHE_ICanHasCheezburger"
        Dim oLolCat As clsLolCat
        Dim sHTML As String        

        oLolCat = CType(Cache(sCacheKey), clsLolCat)        

        If oLolCat Is Nothing Then
            oLolCat = PicAndCaptionGet()
            Cache.Insert(sCacheKey, oLolCat, Nothing, Now.AddHours(1), System.Web.Caching.Cache.NoSlidingExpiration)
        End If        

        sHTML = "<div><a href='" & oLolCat.sLinkURL & "'><img border='0' src='" & oLolCat.sPicURL & "'/></a>"
        sHTML = sHTML & "<div>More at LolCats at <a href='http://icanhascheezburger.com'>I Can Has Cheezburger</a></div></div>"        

        sHTML = "document.write(""" & sHTML & """);"
        Response.Write(sHTML)        

    End Sub

This first part just checks to see if we have the LolCat HTML in the cache. If we don’t then generate the HTML and put it in the cache again. Saves us hitting the http://icanhascheezburger.com site all the time (or in this case, feedburner).


    Private Function PicAndCaptionGet() As clsLolCat
        Dim Http As HttpWebRequest
        Dim sRequestURL As String
        Dim WebResponse As HttpWebResponse
        Dim responseStream As Stream
        Dim Reader As StreamReader
        Dim sText As String
        Dim oRSSDoc As New System.Xml.XmlDocument
        Dim oFirstNode As System.Xml.XmlNode
        Dim oLolCat As New clsLolCat        

        sRequestURL = "http://feeds.feedburner.com/ICanHasCheezburger?format=xml"
        Http = CType(WebRequest.Create(sRequestURL), HttpWebRequest)
        Http.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate")
        WebResponse = CType(Http.GetResponse(), HttpWebResponse)
        responseStream = WebResponse.GetResponseStream        

        If (WebResponse.ContentEncoding.ToLower().Contains("gzip")) Then
            responseStream = New GZipStream(responseStream, CompressionMode.Decompress)
        ElseIf (WebResponse.ContentEncoding.ToLower().Contains("deflate")) Then
            responseStream = New DeflateStream(responseStream, CompressionMode.Decompress)
        End If        

        Reader = New StreamReader(responseStream, Encoding.Default)
        sText = Reader.ReadToEnd()
        WebResponse.Close()
        responseStream.Close()        

        oRSSDoc.LoadXml(sText)
        oFirstNode = oRSSDoc.SelectSingleNode("//item")
        oLolCat.sCaption = oFirstNode("title").InnerText
        oLolCat.sLinkURL = oFirstNode("link").InnerText
        oLolCat.sPicURL = oFirstNode("media:content").Attributes("url").Value        

        PicAndCaptionGet = oLolCat
    End Function        

End Class        

Public Class clsLolCat
    Public sPicURL As String
    Public sCaption As String
    Public sLinkURL As String
End Class

This is the code that visits the icanhascheezburger RSS feed and extracts the latest post details. This is all pretty simple stuff, with one cool bit that takes care of the gzip compression that is on the feed. Full credit must go to Rick Strahl for his post talking about gzip: http://www.west-wind.com/WebLog/posts/102969.aspx

Convert MySQL to MS SQL Server Part 2

Following on from my last post on converting MySQL tables to MS SQL, here is the code to convert Insert SQL statements from MySQL to MS SQL. Try it out here: The code for this is really simple. Again, if you have any improvements just post the code here and I’ll update the original and give you credit.


        Dim ConvertedSQL As String = ""  

        ConvertedSQL = Me.txtMySQL.Text  

        ConvertedSQL = ConvertedSQL.Replace("rn", "")
        ConvertedSQL = ConvertedSQL.Replace("'", "''")
        ConvertedSQL = ConvertedSQL.Replace("\" & Chr(34), Chr(34))
        ConvertedSQL = ConvertedSQL.Replace("INTO " & Me.txtOldTableName.Text, "INTO " & Me.txtNewTableName.Text)
        Me.txtMSSQL.Text = ConvertedSQL  

Aussie Estate Agents fight Google in Court (and get what they want, sorta)

I noticed in the paper over the weekend that two Melbourne real estate agents sought legal action in December to force Google to not display posts from a certain blog when their names were ‘googled’. The blog posts apparently showed the agents (Paul Castran and Mark Forytarz) in a bit of a bad light - suggesting one bullied a mentally disabled man into selling his house to earn the agent commission and that the other agent participated in illegal dummy bidding. Interestingly, there was no legal action brought against the blogger who wrote the posts. Also, the newspaper article stated that the posts were not removed from the Google index.

Suing Google has been tried several times before, and rarely goes well, but when I did a search the agents names:
http://www.google.com.au/search?hl=en&q=mark+forytarz&btnG=Google+Search&meta=

The defamatory article is not there, and there is this line at the bottom of the page:
In response to a legal request submitted to Google, we have removed 1 result(s) from this page. If you wish, you may read more about the request at ChillingEffects.org.
Notice that the search was done in google.com.au, if you do it in google.com then the defamatory blog post is the first result (though perhaps this will be gone soon, too).

So what caused what here? Did legal action have no effect as claimed by the newspaper? Did the two agents just approach google through other channels (similar to a DMCA takedown)? Given that the initial action reportedly failed, did the recent publicity have anything to do with google removing the blog posts?

But now the posts have been removed from google.com.au, everything is fine for the real estate agents, right?
Not quite.
Now, if you search for ‘Paul Castran’ the #2 result is Paul Castran Wins Today’s Stupid Prize!. Time for more legal action? :)

Convert MySQL to MS Sql Server

I’ve been setting up some small database-driven sites lately and I needed to convert some MySQL tables to MS Sql Server. It’s not too hard convert them by hand, but it is oh so tedious…

So I wrote a small app to convert the MySQL tables to MS SQL, which you can try out here:

Now this was a quick & dirty app, so it really doesn’t do much else other than convert the datatypes and get the syntax right. It doesn’t convert keys/constraints/indexes or any of that stuff. It doesn’t even convert all the datatypes, but it has the main ones (ie - the ones I needed) covered. Note: If you add more datatypes or add support for indexes etc just email/post your changes and I will update this code as well as giving link credit.

Here is the code to do the conversion:


    Private Function ConvertTable(ByVal sMySQL As String, _
    ByRef sMSSQL As String) As String
        Dim sLine As String = ""
        Dim iC As Integer
        Dim sVarName As String
        Dim sDatatype As String
        Dim sLen As String
        Dim iLen As Integer
        Dim sNewDataType As String = ""
        Dim sOutput As String = ""
        Dim iLen2 As Integer

        For iC = 0 To sMySQL.Split(vbLf).GetUpperBound(0)
            sLine = sMySQL.Split(vbLf)(iC)
            sLine = Trim$(sLine)
            If sLine.StartsWith("CREATE TABLE") = True Then
                sMSSQL = sMSSQL & sLine & vbCrLf

            ElseIf sLine.StartsWith("KEY") = True Or sLine.StartsWith("PRIMARY KEY") = True Then
                'Ignore this
            ElseIf sLine.StartsWith(")") = True Then
                sMSSQL = Strings.Left$(sMSSQL, Len(sMSSQL) - 3)
                sMSSQL = sMSSQL & vbCrLf & ")"
                Exit For
            Else
                'sLine = sLine.Replace(",", " ")
                sVarName = sLine.Split(" ")(0)
                sDatatype = sLine.Split(" ")(1)
                If InStr(sDatatype, "(") > 0 Then
                    sLen = Mid$(sDatatype, InStr(sDatatype, "("))
                    sLen = sLen.Replace("(", "").Replace(")", "")
                    If InStr(sLen, ",") > 0 Then
                        iLen = Val(sLen.Split(",")(0))
                        iLen2 = Val(sLen.Split(",")(1))
                    Else
                        iLen = Val(sLen)
                    End If
                    sDatatype = Strings.Left$(sDatatype, InStr(sDatatype, "(") - 1)
                End If
                If InStr(sDatatype, ",") > 0 Then
                    sDatatype = sDatatype.Split(",")(0)
                End If
                Select Case LCase(sDatatype)
                    Case "int", "smallint", "year", "tinyint"
                        sNewDataType = "int"
                    Case "blob", "text"
                        sNewDataType = "text"
                    Case "decimal"
                        sNewDataType = "decimal(" & iLen & "," & iLen2 & ")"
                    Case "double"
                        sNewDataType = "float(10)"
                    Case "date", "datetime"
                        sNewDataType = "datetime"
                    Case "timestamp"
                        sNewDataType = "bigint"
                    Case "varchar"
                        sNewDataType = "varchar(" & iLen & ")"
                    Case "char"
                        sNewDataType = "char(" & iLen & ")"
                    Case Else
                        sOutput = sOutput & "Unknown datatype = '" & sDatatype & "'"
                        sMSSQL = ""
                        Exit For
                End Select
                sMSSQL = sMSSQL & sVarName & " " & sNewDataType & "," & vbCrLf

            End If
        Next

        If sOutput = "" Then
            sOutput = "Conversion Complete"
        End If
        ConvertTable = sOutput

    End Function

Note: I have now made another post which shows how to Convert MySQL Inserts to MS SQL Server

Using Google Translate to generate new content from old

All the search engines love unique content, but rewriting articles is such hard work… there has to be an easier way…

This is a simple method that alters your original text, while still remaining (relatively) readable.

Feed your original text into Google Translate, going from English to French. Then take the French text and translate it back to English.

Original
All popups are bad news, right? Nothing gets rid of visitors faster than a crappy ad for casinoviagraporn getting in their face.

But what about visitors that found your site through a search engine, spend 5 seconds on 1 page of your site & then press the back button?

They didn’t find what they want.
They aren’t coming back.
Don’t they deserve some popup love?

New
All of the pop-ups are bad news, right? Nothing gets rid of visitors faster than a crappy ad for casinoviagraporn get in their face.

But what about the visitors found your site through a search engine, spend 5 seconds on 1 page on your site and then press the button back?

They did not find what they want.
They are not to return.
Do they not deserve some popup love?

Voila! What is old is new again :)

Bargains to be had at DNForum

I was just checking through DNForum for cheap domain names and I noticed that a PR8 domain with 293,000 backlinks was just sold for around $200!!

That has got to be the bargain of the year! I’m kicking myself that I missed it.

On the other hand, don’t believe everything:
There is this sales thread on sitepoint, for a domain listed as PR6 and 1800+ backlinks. On further inspection of the:
PR
and the
backlinks
maybe it isn’t quite such a good deal.

AGLOCO goes belly up

I just got this email.

Dear Gath,

We would like to update you on the status of AGLOCO’s operations. We continue to believe in the AGLOCO concept, but our revenue is currently not sufficient to give Members a meaningful distribution. And though there are increases in membership, the resulting revenue is not enough to support operating costs. As a development team we are unable to continue to use our savings to fund the operations. If any Member would like to pursue continuing the operations of AGLOCO, you may contact us at agloco1@live.com .

We would like to thank every Member for supporting our effort to bring a piece of the Internet directly to the user. We hope that we can find a way to keep the operations going.

AGLOCO Development Team

So, it looks like all the people who said ‘If it seems too good to be true….’ were right. Looks like I’m going to cancel my order for the ferrari.

Check 1000s of Images for duplicates quickly

I came up against a problem recently where I had over 4000 images and I needed to check to see if there were any duplicates and remove them. The images were all different sizes/resolutions and duplicate images could have the same picture, but have different sizes/resolutions.

I might also add that I’m not exactly ‘Dr Maths’ or have any clue about image transformations - but I needed to get rid of the dupes in a hurry.

The method I came up with in the end was pretty simple, and while not foolproof, I haven’t seen it make a mistake yet. It was also quick enough for my needs, removing all the duplicates in around 20 minutes.

The Screenshot

If you want to compare 2 directories, then fill out both directory boxes. Any duplicates you choose to delete will be removed from directory 2. If all the files are in one directory, then leave the directory 2 textbox empty.

The Downloads

CompareImages.exe - You can run this
CompareImages.zip - Full code

The Solution:

1. build 50×50 copies of all images

Under the root directory of the images I add another directory called ’small’. I then make 50×50 copies of all the images and put them in this directory. I do this for 2 reasons.

  • It normalises the size of all the images - I couldn’t compare 200×200 to 300×300 images before, but now they are both 50×50.
  • Using pixel-by-pixel comparison, 50×50 is a lot less comparisons than 300×300 (2500 vs 90,000) and is still accurate enough to detect duplicates

2. Compare the 50×50 images with each other

Here is where the people that know something about images will most likely shake their heads (if they haven’t started already). For the rest;

For both images, I compare every pixel to see if they have ‘almos the same’ RGB value. If the RGB values are not similar, then I increment difference count. Once the number of differences reaches 50 these images are not the same, and I stop comparing (which saves a lot of time).


iDiff = 20

For iY = 0 To iHeight - 1
    For iX = 0 To iWidth - 1
	oOrigPixel = bmOrig.GetPixel(iX, iY)
	oNewPixel = bmNew.GetPixel(iX, iY)

	If Math.Abs(Int(oOrigPixel.R) - Int(oNewPixel.R)) > iDiff OrElse _
    	Math.Abs(Int(oOrigPixel.G) - Int(oNewPixel.G)) > iDiff OrElse _
   	Math.Abs(Int(oOrigPixel.B) - Int(oNewPixel.B)) > iDiff Then
    		iDiffCount = iDiffCount + 1
    		If iDiffCount > 50 Then
			'Images are not duplicates
			Exit Function
    		End If
	End If
    Next
Next

'Images ARE duplicates