Thursday, July 22, 2010

Facebook - How to use facebook connect on your site | facebook connect beginners guide

Recently I came across the facebook connect API when developing a site that uses facebook connect for it's football manager game. There are a lot of tutorials out there that shows how to do this and that but here I wanted to share a simple guideline on the first step to implement facebook connect to your site.

The first thing you have to do of course to register your application in facebook. You can do that here.

When finished, you will be given an API Key and Application Secret codes, these codes will be use later in the connect API.

You can now write your php file like the sample below:


function get_facebook_cookie($app_id, $application_secret) {
  $args = array();
  parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
  $payload = '';
  foreach ($args as $key => $value) {
    if ($key != 'sig') {
      $payload .= $key . '=' . $value;
  if (md5($payload . $application_secret) != $args['sig']) {
    return null;
  return $args;
$fb_cookie = get_facebook_cookie(FACEBOOK_APP_KEY, FACEBOOK_SECRET);

if ($fb_cookie) { //if logged in
<!DOCTYPE html>
<div id="fb-root"></div>
FB.init({appId: "<?php echo FACEBOOK_APP_KEY; ?>", status: true, cookie: true, xfbml: true});
FB.Event.subscribe('auth.login', function(response) {
    document.location.href = 'document.location.href';

function fb_logout() {
    FB.logout(function(response) {
        document.location.href = 'document.location.href';

<?php if (!$fb_cookie) { //if not login ?>
             <fb:login-button perms="publish_stream,user_photos" v="2"><fb:intl>Connect with Facebook</fb:intl></fb:login-button>
      <?php } else { ?>
                  <div class="logout">
                       HI, <?php echo strtoupper(substr($user->first_name, 0, 17)); ?>! | <a href="javascript:fb_logout()">LOGOUT</a>
      <?php } ?>

      <!-- do your things here-->


There you go, simple application that has a connect button and logout button to sign out. 

If you are using Jquery tools plugin, you cannot use the flashembed feature as it would give you error in Internet Explorer "your api key is not valid.... etc". That's the only issue that I found throughout my experience.

Wednesday, July 21, 2010

Using swfobject for fullscreen flash site

This simple guide will show how to use swfobject for a fullscreen flash site.

First you need to of course download swfobject at and extract it to js/ folder for example here.

In this case, the flash file name will be index.swf.

Here's the html code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<link rel="Shortcut Icon" href="favicon.ico">
<script type="text/javascript" src="js/swfobject.js"></script>
<script type="text/javascript">
var flashvars = {};
var params = {wmode: "transparent"};
var attributes = {};
swfobject.embedSWF("index.swf", "flash", "100%", "100%",
"9.0.0","js/expressInstall.swf", flashvars, params, attributes);

function getViewportSize() {
var size = [0, 0];
if (typeof window.innerWidth != "undefined") {
size = [window.innerWidth, window.innerHeight];
else if (typeof document.documentElement != "undefined" && typeof document.documentElement.clientWidth != "undefined" && document.documentElement.clientWidth != 0) {
size = [document.documentElement.clientWidth, document.documentElement.clientHeight];
else {
size = [document.getElementsByTagName("body")[0].clientWidth, document.getElementsByTagName("body")[0].clientHeight];
return size;
<body style="margin:0px; padding:0px;">
<div id="container">
<div id="flash">
<p><a href=""><img src=" " alt="Get Adobe Flash player" /></a></p>
<script type="text/javascript">
window.onresize = function() {
var el = document.getElementById("container");
var size = getViewportSize(); = size[0] < 900 ? "900px" : "100%"; = size[1] < 600 ? "600px" : "100%";

You can specify the minimum size restriction that the flash can be viewed, in this case I specify 900x600.

Posted via email from weirdcalculator's posterous

Thursday, July 15, 2010

PHP - The magic of php eval() function

It's been a while since I last post to my blog and today I would like to share with you my experience with the so called eval() function in PHP.

The function eval() basically:
"Evaluates the string given in code_str as PHP code. Among other things, this can be useful for storing code in a database text field for later execution.
There are some factors to keep in mind when using eval(). Remember that the string passed must be valid PHP code, including things like terminating statements with a semicolon so the parser doesn't die on the line after the eval(), and properly escaping things incode_str. To mix HTML output and PHP code you can use a closing PHP tag to leave PHP mode.
Also remember that variables given values under eval() will retain these values in the main script afterwards."

for example:
<?php eval("echo 'test';"); ?>
will have the same output as
<?php echo "test"; ?>

so by this method, we can do any dynamic programming in php as long as the syntax is correct.

I came out with an experiment where I host a class script somewhere and with just 2 lines of code I can use the class in another web server without any problem.

In the external server I set a script to output the class codes:
In php, to include the class I just have to use these 2 lines:
$class_common = file_get_contents("");
eval("?>".htmlspecialchars_decode($class_common)."<?php ");

So now I can use:
$common = new class_common();

And the function will run as it is as it were on the same server.

The issue on this of course the load time of the script is vary depending on how fast the server can get the script from the other server but it is worthy in some cases.