Facebook Like Buttons with Dynamic URLs in PHP and JavaScript

Facebook Like buttons are popping up all over the Web as a way for people to integrate a little bit of Facebook into their site. I thought it’d be a fun experiment to try on my site here, but I wanted a dynamic URL to pass. There are a ton of blogs out there for putting a Facebook “Like” button your pages using a dynamic URL with a bit of PHP trickery. Here’s the code I ended up using for this site:

<iframe src="https://www.facebook.com/plugins/like.php?href=<?php echo 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];?>&layout=button_count&show_faces=false&width=80&action=like&font=verdana&colorscheme=light" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:80px; height:22px" allowTransparency="true"></iframe>

However, on another site I’m working on getting this implemented on, I don’t have access to the raw code, and can’t use server-side scripting or variables. So I came up with a JavaScript solution that I’m sharing here as I couldn’t find an equivalent version online:

<SCRIPT LANGUAGE="JavaScript">

document.write("<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href="+document.URL+"&layout=button_count&show_faces=false&width=80&action=like&font=verdana&colorscheme=light\" scrolling=\"no\" frameborder=\"0\" style=\"border:none; overflow:hidden; width:80px; height:22px\" allowTransparency=\"true\"><\/iframe>");

</SCRIPT>

It’s probably not the cleanest/best code in the world (I would never consider myself a programmer) and there are probably 100 different ways of doing this, but this worked for me and hopefully that will help somebody out there on the InterWebs (and feel free to “Like” this post — the button’s on the permalinked URL — just to see if it works). You also might want to adjust the width of the frame and the code a bit. I personally just wanted a small button with just the number of likes (and am considering even getting rid of that, tightening it up a bit).

And for my future reference, these two tools were very useful in doing this — escaping the code for posting here, and creating that document.write statement.

Note: I’m now using the add-this.com service for this, as it provides me better tracking and features than my code above does, but I’m leaving this here for reference.

Reading Material

Time Killer For The Evening

While this game is just a simple (but nice looking) mini-golf game, it’s intriguing because of the way they monetize it — by branding the holes.

(And apparently, looking a bit more, the initial sponsors are decided on by who sends them the most new visitors, so click away, folks.)

Reading Material

Hitler “Downfall Meme” gets taken down

The oh-so-awesome DMCA strikes again.

Adobe Gives up on Apple, Welcomes Android

Adobe says farewell to the iPhone. Yet another reason that my next phone will not be an iPhone, or a Windows Mobile phone (though I like the latter far better than the former) but an Android phone — ideally this one, but I can only imagine how much that’s going to cost.

Reading Material

Apple Wants Its Secret iPhone Back

I mentioned yesterday about Gizmodo having Apple’s new iPhone. Looks like Apple’s a bit miffed and wants it back.

One Button Time Killer For The Day

Poto and Cabenga is a game controlled entirely by the space bar, and is insanely difficult. Link via Waxy.

Gizmodo Has An iPhone 4G, And Paid Big For It

They reportedly have the new phone, and apparently paid a cool $10,000 for it. There’s apparently more to the story, the Gawker editor foretells. So this could get interesting, and they could pay bigger.

Update: Giz profiles the guy who lost the phone.