Showing posts with label advanced php. Show all posts
Showing posts with label advanced php. Show all posts

Wednesday, July 13, 2011

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.