Wednesday, July 13, 2011

iframe - ing the facebook dialog display

There is at least 3 kind of display for the facebook dialog:

  • page. the dialog will display with facebook header and footer taking all area of the page.
  • popup. the dialog rendered in a window.
  • iframe. we can render the dialog in an iframe element.

For the last display mode, we cannot find anywhere in the documentation, a good example. But, i think we can do this.

First as the documentation said, iframe display mode require an access_token. You must have this code in hand.

facebook application php SDK: Automatic Authorization

when user of facebook open your application for the first time, the SDK will authenticate the user and request for authorization.

the typical SDK suggestion is:

redirection method is up to the programmer. From the documentation, they use the login button or a link to the login page. The login url was constructed using the SDK function getLoginUrl().

if ($user) {
    $logoutUrl = facebook->getLogoutUrl();
} else {
   $loginUrl = facebook->getLoginUrl();
   echo "<a href='" . $loginUrl . "'>login</a>";
}

So user must click the login hyperlink. But using header function from PHP, we can change the code:

if ($user) {
    $logoutUrl = facebook->getLogoutUrl();
} else {
   $loginUrl = facebook->getLoginUrl();
   header("Location: $loginUrl") //remember to use double quote as we enclosed a variable
}

this code work fine, until you use it in facebook application canvas. In the canvas, it will redirect you to the facebook logo, and again user must click the "go to facebook" link.

I think there is no serverside solution for this, so our option is javascript:

if ($user) {
    $logoutUrl = facebook->getLogoutUrl();
} else {
   $loginUrl = facebook->getLoginUrl();
   echo "<script> top.location.href='" . $loginUrl . "'</script>";
}

this will entirely redirect the browser to the login or authorization dialog. So you think we done already. Not quite...

Tuesday, July 12, 2011

Note On Using header function in PHP

When using header function to redirect a request, make sure you are using double quotes in the parameter that you supply. Example:

header("Location: http://blogmodif.blogspot.com/2011/07/note-on-using-header-function-in-php.html")

and not like this:

header('Location: http://blogmodif.blogspot.com/2011/07/note-on-using-header-function-in-php.html')

which is using single quote. On both the above example, they will work though. But, when you want to use variable to set the URL, for example:

$myURL= "http://blogmodif.blogspot.com/2011/07/note-on-using-header-function-in-php.html"

and then do:

header('Location: $myURL');

will cause an error because php didn't evaluate the variable between the single quote. Only when you wrap them in the double quote, then the variable will be evaluated:

header("Location: $myURL");

will redirect to this post.
I hope this is usefull.

Sunday, July 10, 2011

Writing Your second PHP page

In our previous lesson, we saw only a single line of code

echo "this is my PHP enabled page";

What is done by this code? It simply display the text "this is my PHP enabled page" to the browser.

Now that we have learn the "echo" thing, we are ready to do something more advanced.


PHP Variable

What is variable?? If you something like $var or $name or $pass, that how variables looks like in PHP. Variable stores data in your program. For example, we want to store a name "John", we could say:


$myName="John";


$myName which is a variable, store the text for latter use. Now to display the content of the variable, we say:

 

echo "Hi, my name is ".$myName;

Thursday, July 7, 2011

Writing your first php page

So you want to know how to create web pages with PHP.
But first, you must already familiar about HTML language. I will not discuss about HTML in this paper. Readers are advised to learn about this through a lot of material available in books or the internet.

Escaping from HTML
Lets have a look at some of the following lines:

<?php
echo "this is my php enabled page";
?>

You are looking at a very simple line of PHP code I'll explain in the next lesson