Facebook connect using graph api

An example of using facebook graph api to authenticate users:

Setup a new application in facebook apps and get the app id and secret.

Now follow the steps:

Remember to give the same url in the below code, as that of the app settings.

<?php

if( !isset($_GET['code'])) { ?>

<a href=”https://graph.facebook.com/oauth/authorize?client_id=xxxxxxxx&redirect_uri=http://xxxxxxxxxxxxxxxx/”>Connect using Facebook</a>
<?php }

$code = trim($_GET['code']);
if(isset($_GET['code']))

$token = file_get_contents(“https://graph.facebook.com/oauth/access_token?client_id=xxxxxxxx&redirect_uri=http://xxxxxxxxxxxxxxx&client_secret=xxxxxxxxxxxx&code=$code”);
$token = explode(“=”, $token);
if(isset($_GET['code']))

$user = json_decode(file_get_contents(‘https://graph.facebook.com/me?access_token=’.$token[1]));  print_r($user);

?>

dropdown menu over flash banner

It is often annoying in some browsers when the dropdown menu at the top of the flash banner is displayed at the back of the flash.

It can be corrected with some minor changes in the flash embed code:

1. for embed tag, add wmode=”transparent”

2. add param to the object tag: <param name=”wmode” value=”transparent” />

php delete files in a folder and the folder

function rrmdir($dir)

{

if (is_dir($dir))  {

$objects = scandir($dir);

foreach ($objects as $object) {     if ($object != “.” && $object != “..”)  {  if (filetype($dir.”/”.$object) == “dir”) rrmdir($dir.”/”.$object); else unlink($dir.”/”.$object);       }     }     reset($objects);     rmdir($dir);   }

}

$delpath = “user_data”; // directory that has the files

rrmdir($delpath);  // deletes all files inside the directory

// Remove the directory
if(is_dir($delpath))  rmdir($delpath);

php loop images in a folder, display them

This code will loop all the images inside “images” folder and display them. For every 7 images, a new row is added.

<table>
<tr>
<?php
$photo_dir = “images”;
$files = glob($photo_dir.”/*.*”);

for ($i=0; $i<count($files); $i++)
{
$j = $i+1;
?>
<td height=”25″ align=”center”>
<?php
$num = $files[$i];
$img_name = explode(“.”, basename($num));
echo ‘<a href=”‘.$num.’”>
<img src=”‘.$num.’” width=”120″ height=”120″ style=”border:4px solid #FFF;”></a>’; // .$img_name[0];
?>
</td>
<?php
if($j % 7 == 0)
echo ‘</tr><tr>’;
}
?>
</tr>
</table>

PHP get, set and convert Timezone

Get the default (script) timezone: date_default_timezone_get();

Get ini set timezone: ini_get(‘date.timezone’);

Get timezone name: date(“T”);

Set timezone example: date_default_timezone_set(‘Europe/London’); // GMT

GMT to IST conversion example:

$event_length = 330; // IST is 5:30 ahead of GMT, so add 330 minutes
$timestamp = strtotime(date(“H:i:s”));
$etime = strtotime(“+$event_length minutes”, $timestamp);
$ist_time = date(‘H:i:s’, $etime);
echo ‘<br>’.'GMT – IST: ‘.$ist_time;

Jquery css hacks

An example to control the style in various browsers:

$(document).ready(function(){
if($.browser.msie) {    // IE
$(“#box”).css({
“background”: “none”,
“margin-top”: “60px”
});
};

if($.browser.mozilla) {   // Firefox
$(“#box”).css({
“margin-top”: “40px”,
“margin-left”: “30px”
});
};

if($.browser.safari) {    // Chrome
$(“#box”).css({
“margin-top”: “100px”,
“margin-left”: “30px”
});
};
});