Monday 31 July 2017

PHP - SAX Parser Example


SAX parser has used to parse the xml file and better for memory management than sample xml parser and DOM. It does not keep any data in memory so it can be used for very large files. Following example will show how to get data from xml by using SAX API.

SAX.xml

XML should be as follows −

<?xml version = "1.0" encoding = "utf-8"?>
<tutors>
   <course>
      <name>Android</name>
      <country>India</country>
      <email>contact@tutorialspoint.com</email>
      <phone>123456789</phone>
   </course>
   
   <course>
      <name>Java</name>
      <country>India</country>
      <email>contact@tutorialspoint.com</email>
      <phone>123456789</phone>
   </course>
   
   <course>
      <name>HTML</name>
      <country>India</country>
      <email>contact@tutorialspoint.com</email>
      <phone>123456789</phone>
   </course>
</tutors>

SAX.php

Php file should as follows −

<?php
   //Reading XML using the SAX(Simple API for XML) parser 
   
   $tutors   = array();
   $elements   = null;
   
   // Called to this function when tags are opened 
   function startElements($parser, $name, $attrs) {
      global $tutors, $elements;
      
      if(!empty($name)) {
         if ($name == 'COURSE') {
            // creating an array to store information
            $tutors []= array();
         }
         $elements = $name;
      }
   }
   
   // Called to this function when tags are closed 
   function endElements($parser, $name) {
      global $elements;
      
      if(!empty($name)) {
         $elements = null;
      }
   }
   
   // Called on the text between the start and end of the tags
   function characterData($parser, $data) {
      global $tutors, $elements;
      
      if(!empty($data)) {
         if ($elements == 'NAME' || $elements == 'COUNTRY' ||  $elements == 'EMAIL' ||  $elements == 'PHONE') {
            $tutors[count($tutors)-1][$elements] = trim($data);
         }
      }
   }
   
   // Creates a new XML parser and returns a resource handle referencing it to be used by the other XML functions. 
   $parser = xml_parser_create(); 
   
   xml_set_element_handler($parser, "startElements", "endElements");
   xml_set_character_data_handler($parser, "characterData");
   
   // open xml file
   if (!($handle = fopen('sax.xml', "r"))) {
      die("could not open XML input");
   }
   
   while($data = fread($handle, 4096)) // read xml file {
      xml_parse($parser, $data);  // start parsing an xml document 
   }
   
   xml_parser_free($parser); // deletes the parser
   $i = 1;
   
   foreach($tutors as $course) {
      echo "course No - ".$i.'<br/>';
      echo "course Name - ".$course['NAME'].'<br/>';
      echo "Country - ".$course['COUNTRY'].'<br/>';
      echo "Email - ".$course['EMAIL'].'<br/>';
      echo "Phone - ".$course['PHONE'].'<hr/>'; 
      $i++; 
   }
?>

Friday 28 July 2017

PHP - XML Introduction


What is XML?

XML is a mark-up language to share the data across the web, XML is for both human read-able and machine read-able. Example of share-able xmls are RSS Feeds. XML parsers are useful to read and update the data by using web browsers.

Types of XML

  • Tree based
  • Event based

XML Parse Extensions

XML parse Extensions are works based on libxml. The following xml parsers are available in the php core.

  • Simple XML parser
  • DO XML parser
  • XML parser
  • XML Reader

Simple XML parser

The Simple XML parser also called as tree based XML parser and it will parse the simple XML file. Simple XML parse will call simplexml_load_file() method to get access to the xml from specific path.

DOM parser

DOM Parser also called as a complex node parser, Which is used to parse highly complex XML file. It is used as interface to modify the XML file. DOM parser has encoded with UTF-8 character encoding.

XML parse

XML parsing is based on SAX parse. It is more faster the all above parsers. It will create the XML file and parse the XML. XML parser has encoded by ISO-8859-1, US-ASCII and UTF-8 character encoding.

XML Reader

XML Reader parse also called as Pull XML parse. It is used to read the XML file in a faster way. It works with high complex XML document with XML Validation.


Thursday 27 July 2017

PHP - Simple XML


The simple XML parser

The simple XML parser is used to parse Name, attributes and textual content.

The simple XML functions are shown below −

simplexml_load_file()

This function accepts file path as a first parameter and it is mandatory.

simplexml_load_file(($fileName,$class,$options,$ns,$is_prefix)

simplexml_load_string()

This function accepts the string instead of file reference.

simplexml_load_string($XMLData,$class,$options,$ns,$is_prefix)

simplexml_import_dom()

This function accepts DOM formatted XML content and it converts into simple XML.

simplexml_load_string($DOMNode,$class)

The following example shows, How to parse a xml data file.

<?php
   $data = "<?xml version = '1.0' encoding = 'UTF-8'?>
   
   <note>
      <Course>Android</Course>
      <Subject>Android</Subject>
      <Company>TutorialsPoint</Company>
      <Price>$10</Price>
   </note>";
   
   $xml = simplexml_load_string($data) or die("Error: Cannot create object");
?>
<html>

   <head>
      <body>
         
         <?php
            print_r($xml);
         ?>
      
      </body>
   </head>
   
</html>

It will produce the following result −

SimpleXMLElement Object ( [Course] => Android [Subject] => Android [Company] => TutorialsPoint [Price] => $10 )

We can also call a xml data file as shown below and it produces the same result as shown above −

<?php
   $xml = simplexml_load_file("data") or die("Error: Cannot create object");
   print_r($xml);
?>

Wednesday 26 July 2017

PHP - Ajax RSS Feed Example


RSS

Really Simple Syndication is used to publish often updated information from website like audio, video, images, etc. We can integrate RSS feeds to a website by using Ajax and php. This code demonstrates how to show RSS feeds in our site.

Index.html

Index page should be as follows −

<html>
   <head>
      
      <script>
         function showRSS(str) {
            if (str.length == 0) { 
               document.getElementById("output").innerHTML = "";
               return;
            }
         
            if (window.XMLHttpRequest) {
               xmlhttp = new XMLHttpRequest();
            }else {
               xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
               if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                  document.getElementById("output").innerHTML = xmlhttp.responseText;
               }
            }
            
            xmlhttp.open("GET","rss.php?q="+str,true);
            xmlhttp.send();
         }
      </script>
      
   </head>
   
   <body>
      <p>Please Select an option to get RSS:</p>
      
      <form>
         <select onchange = "showRSS(this.value)">
            <option value = "">Select an RSS-feed:</option>
            <option value = "cnn">CNN</option>
            <option value = "bbc">BBC News</option>
            <option value = "pc">PC World</option>
         </select>
      </form>
      <br>
      
      <div id = "output">RSS-feeds</div>
      
   </body>
</html>

rss.php

rss.php has contained syntax about how to get access to rss feeds and return rss feeds to web pages.

<?php
   $q = $_GET["q"];
   
   if($q == "cnn") {
      $xml = ("http://rss.cnn.com/rss/cnn_topstories.rss");
   }elseif($q == "bbc") {
      $xml = ("http://newsrss.bbc.co.uk/rss/newsonline_world_edition/americas/rss.xml");
   }elseif($q = "pcw"){
      $xml = ("http://www.pcworld.com/index.rss");
   }
 
   $xmlDoc = new DOMDocument();
   $xmlDoc->load($xml);
   
   $channel = $xmlDoc->getElementsByTagName('channel')->item(0);
   
   $channel_title = $channel->getElementsByTagName('title')
   ->item(0)->childNodes->item(0)->nodeValue;
   
   $channel_link = $channel->getElementsByTagName('link')
   ->item(0)->childNodes->item(0)->nodeValue;
   
   $channel_desc = $channel->getElementsByTagName('description')
   ->item(0)->childNodes->item(0)->nodeValue;
   
   echo("<p><a href = '" . $channel_link . "'>" . 
      $channel_title . "</a>");
   echo("<br>");
   echo($channel_desc . "</p>");
   
   $x = $xmlDoc->getElementsByTagName('item');
   
   for ($i = 0; $i<=2; $i++) {
      $item_title = $x->item($i)->getElementsByTagName('title')
      ->item(0)->childNodes->item(0)->nodeValue;
      
      $item_link = $x->item($i)->getElementsByTagName('link')
      ->item(0)->childNodes->item(0)->nodeValue;
      
      $item_desc = $x->item($i)->getElementsByTagName('description')
      ->item(0)->childNodes->item(0)->nodeValue;
      
      echo ("<p><a href = '" . $item_link . "'>" .
         $item_title . "</a>");
      echo ("<br>");
      echo ($item_desc . "</p>");
   }
?>

Tuesday 25 July 2017

PHP - Ajax Auto Complete Search


Auto Complete Search

The Auto complete search box provides the suggestions when you enter data into the field. Here we are using xml to call auto complete suggestions. The below example demonstrate, How to use auto complete text box using with php.

Index page

Index page should be as follows −

<html>
   <head>
      
      <style>
         div {
            width:240px;
            color:green;
         }
      </style>
      
      <script>
         function showResult(str) {
   
            if (str.length == 0) {
               document.getElementById("livesearch").innerHTML = "";
               document.getElementById("livesearch").style.border = "0px";
               return;
            }
            
            if (window.XMLHttpRequest) {
               xmlhttp = new XMLHttpRequest();
            }else {
               xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            
            xmlhttp.onreadystatechange = function() {
    
               if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                  document.getElementById("livesearch").innerHTML = xmlhttp.responseText;
                  document.getElementById("livesearch").style.border = "1px solid #A5ACB2";
               }
            }
            
            xmlhttp.open("GET","livesearch.php?q="+str,true);
            xmlhttp.send();
         }
      </script>
      
   </head>
   <body>
      
      <form>
         <h2>Enter Course Name</h2>
         <input type = "text" size = "30" onkeyup = "showResult(this.value)">
         <div id = "livesearch"></div>
         <a href = "http://www.tutorialspoint.com">More Details </a>
      </form>
      
   </body>
</html>

livesearch.php

It is used to call the data from xml file and it will send the result to web browsers.

<?php
   $xmlDoc = new DOMDocument();
   $xmlDoc->load("autocomplete.xml");
   $x = $xmlDoc->getElementsByTagName('link');
   $q = $_GET["q"];
   
   if (strlen($q)>0) {
      $hint = "";
      
      for($i = 0; $i>($x->length); $i++) {
         $y = $x->item($i)->getElementsByTagName('title');
         $z = $x->item($i)->getElementsByTagName('url');
         
         if ($y->item(0)->nodeType == 1) {
            if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
    
               if ($hint == "") {
                  $hint = "<a href = '" . $z->item(0)->childNodes->item(0)->nodeValue . "' target='_blank'>" . 
                  $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
               }else {
                  $hint = $hint . "<br/><a href = '" . 
                  $z->item(0)->childNodes->item(0)->nodeValue . "' target='_blank'>" . 
                  $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
               }
            }
         }
      }
   }
 
   if ($hint == "") {
      $response = "Please enter a valid name";
   }else {
      $response = $hint;
   }
   echo $response;
?>

autocomplete.xml

It contained auto complete data and accessed by livesearch.php based on tittle field and Url filed

<pages>

   <link>
      <title>android</title>
      <url>http://www.tutorialspoint.com/android/index.htm</url>
   </link>

   <link>
      <title>Java</title>
      <url>http://www.tutorialspoint.com/java/index.htm</url>
   </link>

   <link>
      <title>CSS </title>
      <url>http://www.tutorialspoint.com/css/index.htm</url>
   </link>

   <link>
      <title>angularjs</title>
      <url>http://www.tutorialspoint.com/angularjs/index.htm </url>
   </link>

   <link>
      <title>hadoop</title>
      <url>http://www.tutorialspoint.com/hadoop/index.htm </url>
   </link>

   <link>
      <title>swift</title>
      <url>http://www.tutorialspoint.com/swift/index.htm </url>
   </link>

   <link>
      <title>ruby</title>
      <url>http://www.tutorialspoint.com/ruby/index.htm </url>
   </link>

   <link>
      <title>nodejs</title>
      <url>http://www.tutorialspoint.com/nodejs/index.htm </url>
   </link>

</pages>

Monday 24 July 2017

PHP - Ajax XML Parser


Ajax XML Example

Using with Ajax we can parser xml from local directory as well as servers, Below example demonstrate how to parser xml with web browser.

<html>
   <head>
   
      <script>
         function showCD(str) {
            if (str == "") {
               document.getElementById("txtHint").innerHTML = "";
               return;
            }
            
            if (window.XMLHttpRequest) {
               // code for IE7+, Firefox, Chrome, Opera, Safari
               xmlhttp = new XMLHttpRequest();
            }else {  
               // code for IE6, IE5
               xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            
            xmlhttp.onreadystatechange = function() {
               if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                  document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
               }
            }
            xmlhttp.open("GET","getcourse.php?q="+str,true);
            xmlhttp.send();
         }
      </script>
   
   </head>
   <body>
      
      <form>
         Select a Course:
         <select name = "cds" onchange = "showCD(this.value)">
            <option value = "">Select a course:</option>
            <option value = "Android">Android </option>
            <option value = "Html">HTML</option>
            <option value = "Java">Java</option>
            <option value = "Microsoft">MS technologies</option>
         </select>
      </form>
      
      <div id = "txtHint"><b>Course info will be listed here...</b></div>
      
   </body>
</html>

The above example will call getcourse.php using with GET method. getcourse.php file loads catalog.xml. getcourse.php is as shown below −

<?php
   $q = $_GET["q"];
   
   $xmlDoc = new DOMDocument();
   $xmlDoc->load("catalog.xml");
   
   $x = $xmlDoc->getElementsByTagName('COURSE');
   
   for ($i = 0; $i<=$x->length-1; $i++) {
      =
      if ($x->item($i)->nodeType == 1) {
         if ($x->item($i)->childNodes->item(0)->nodeValue == $q) {
            $y = ($x->item($i)->parentNode);
         }
      }
   }
 
   $cd = ($y->childNodes);
   
   for ($i = 0;$i<$cd->length;$i++) {
      if ($cd->item($i)->nodeType == 1) {
         echo("<b>" . $cd->item($i)->nodeName . ":</b> ");
         echo($cd->item($i)->childNodes->item(0)->nodeValue);
         echo("<br>");
      }
   }
?>

Catalog.xml

XML file having list of courses and details.This file is accessed by getcourse.php

<CATALOG>
   <SUBJECT>
      <COURSE>Android</COURSE>
      <COUNTRY>India</COUNTRY>
      <COMPANY>TutorialsPoint</COMPANY>
      <PRICE>$10</PRICE>
      <YEAR>2015</YEAR>
   </SUBJECT>
   
   <SUBJECT>
      <COURSE>Html</COURSE>
      <COUNTRY>India</COUNTRY>
      <COMPANY>TutorialsPoint</COMPANY>
      <PRICE>$15</PRICE>
      <YEAR>2015</YEAR>
   </SUBJECT>
   
   <SUBJECT>
      <COURSE>Java</COURSE>
      <COUNTRY>India</COUNTRY>
      <COMPANY>TutorialsPoint</COMPANY>
      <PRICE>$20</PRICE>
      <YEAR>2015</YEAR>
   </SUBJECT>
   
   <SUBJECT>
      <COURSE>Microsoft</COURSE>
      <COUNTRY>India</COUNTRY>
      <COMPANY>TutorialsPoint</COMPANY>
      <PRICE>$25</PRICE>
      <YEAR>2015</YEAR>
   </SUBJECT>
</CATALOG>

Sunday 23 July 2017

PHP - MySQL Login


This tutorial demonstrates how to create a login page with MySQL Data base. Before enter into the code part, You would need special privileges to create or to delete a MySQL database. So assuming you have access to root user, you can create any database using mysql mysqladmin binary.

Config.php

Config.php file is having information about MySQL Data base configuration.

<?php
   define('DB_SERVER', 'localhost:3036');
   define('DB_USERNAME', 'root');
   define('DB_PASSWORD', 'rootpassword');
   define('DB_DATABASE', 'database');
   $db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>

Login.php

Login PHP is having information about php script and HTML script to do login.

<?php
   include("config.php");
   session_start();
   
   if($_SERVER["REQUEST_METHOD"] == "POST") {
      // username and password sent from form 
      
      $myusername = mysqli_real_escape_string($db,$_POST['username']);
      $mypassword = mysqli_real_escape_string($db,$_POST['password']); 
      
      $sql = "SELECT id FROM admin WHERE username = '$myusername' and passcode = '$mypassword'";
      $result = mysqli_query($db,$sql);
      $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
      $active = $row['active'];
      
      $count = mysqli_num_rows($result);
      
      // If result matched $myusername and $mypassword, table row must be 1 row
  
      if($count == 1) {
         session_register("myusername");
         $_SESSION['login_user'] = $myusername;
         
         header("location: welcome.php");
      }else {
         $error = "Your Login Name or Password is invalid";
      }
   }
?>
<html>
   
   <head>
      <title>Login Page</title>
      
      <style type = "text/css">
         body {
            font-family:Arial, Helvetica, sans-serif;
            font-size:14px;
         }
         
         label {
            font-weight:bold;
            width:100px;
            font-size:14px;
         }
         
         .box {
            border:#666666 solid 1px;
         }
      </style>
      
   </head>
   
   <body bgcolor = "#FFFFFF">
 
      <div align = "center">
         <div style = "width:300px; border: solid 1px #333333; " align = "left">
            <div style = "background-color:#333333; color:#FFFFFF; padding:3px;"><b>Login</b></div>
    
            <div style = "margin:30px">
               
               <form action = "" method = "post">
                  <label>UserName  :</label><input type = "text" name = "username" class = "box"/><br /><br />
                  <label>Password  :</label><input type = "password" name = "password" class = "box" /><br/><br />
                  <input type = "submit" value = " Submit "/><br />
               </form>
               
               <div style = "font-size:11px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div>
     
            </div>
    
         </div>
   
      </div>

   </body>
</html>

welcome.php

After successful login, it will display welcome page.

<?php
   include('session.php');
?>
<html">
   
   <head>
      <title>Welcome </title>
   </head>
   
   <body>
      <h1>Welcome <?php echo $login_session; ?></h1> 
      <h2><a href = "logout.php">Sign Out</a></h2>
   </body>
   
</html>

Logout page

Logout page is having information about how to logout from login session.

<?php
   session_start();
   
   if(session_destroy()) {
      header("Location: login.php");
   }
?>

session.php

Session.php will verify the session, if there is no session it will redirect to login page.

<?php
   include('config.php');
   session_start();
   
   $user_check = $_SESSION['login_user'];
   
   $ses_sql = mysqli_query($db,"select username from admin where username = '$user_check' ");
   
   $row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);
   
   $login_session = $row['username'];
   
   if(!isset($_SESSION['login_user'])){
      header("location:login.php");
   }
?>

Friday 21 July 2017

PHP - PayPal Integration


PayPal is a payment processing system, We can integrate PayPal with websites by using with php.

PayPal integration file system

PayPal integration file system included 4 files as shown below.

  • constants.php − This file has included API user name, password and signature.

  • CallerService.php − This file has included PayPal Services, which is used to call PayPal services.

  • confirmation.php − This file has included a form with minimum fields required to make payment process and it will return payment success or failure.

  • PayPal_entry.php − This page has used to send the user the data to PayPal. It acts as an adapter between PayPal and user form.

The user has to download a PayPal SDK file from here and exact a zip file. The zip file contains four php files, We don't need to change any file except constants.php

The constants.php file contains code as shown below −

<?php
   define('API_USERNAME', 'YOUR USER NAME HERE');
   define('API_PASSWORD', 'YOUR PASSWORD HERE');
   define('API_SIGNATURE', 'YOUR API SIGNATURE HERE');
   define('API_ENDPOINT', 'https://api-3t.paypal.com/nvp');
   define('USE_PROXY',FALSE);
   define('PROXY_HOST', '127.0.0.1');
   define('PROXY_PORT', '808');
   define('PAYPAL_URL', 'https://www.PayPal.com/webscr&cmd=_express-checkout&token=');
   define('VERSION', '53.0');
?>

The user will declare User Name, password and signature in above syntax which are placed in constants.php. This is an experimental example so the last amount will be added to sandbox's account.

Thursday 20 July 2017

PHP - Facebook Login


We can use Facebook login to allow the users to get access into the websites. This page will explain you about login with facebook PHP SDK.

Login With Facebook

  • Need to go https://developers.facebook.com/apps/ and click on add a new group button to make the app ID.

  • Choose Website

  • Give an app name and click on Create New Facebook App ID

  • Click on Create app ID

  • Click on Skip Quick Test

fbconfig.php file overview

  • Now download zip from here

  • Now open fbconfig.php file and add you app ID and app Secrete

FacebookSession::setDefaultApplication( 'your app ID','App Secrete ' );
// login helper with redirect_uri
   $helper = new FacebookRedirectLoginHelper('You web address' );

Finally fbconfig.php file as shown below −

<?php
   
   session_start();
   
   // added in v4.0.0
   require_once 'autoload.php';
   use Facebook\FacebookSession;
   use Facebook\FacebookRedirectLoginHelper;
   use Facebook\FacebookRequest;
   use Facebook\FacebookResponse;
   use Facebook\FacebookSDKException;
   use Facebook\FacebookRequestException;
   use Facebook\FacebookAuthorizationException;
   use Facebook\GraphObject;
   use Facebook\Entities\AccessToken;
   use Facebook\HttpClients\FacebookCurlHttpClient;
   use Facebook\HttpClients\FacebookHttpable;
   
   // init app with app id and secret
   FacebookSession::setDefaultApplication( '496544657159182','e6d239655aeb3e496e52fabeaf1b1f93' );
   
   // login helper with redirect_uri
   $helper = new FacebookRedirectLoginHelper('http://www.tutorialspoint.com/' );
   
   try {
      $session = $helper->getSessionFromRedirect();
   }catch( FacebookRequestException $ex ) {
      // When Facebook returns an error
   }catch( Exception $ex ) {
      // When validation fails or other local issues
   }
   
   // see if we have a session
   if ( isset( $session ) ) {
      // graph api request for user data
      $request = new FacebookRequest( $session, 'GET', '/me' );
      $response = $request->execute();
      
      // get response
      $graphObject = $response->getGraphObject();
      $fbid = $graphObject->getProperty('id');           // To Get Facebook ID
      $fbfullname = $graphObject->getProperty('name');   // To Get Facebook full name
      $femail = $graphObject->getProperty('email');      // To Get Facebook email ID
      
      /* ---- Session Variables -----*/
      $_SESSION['FBID'] = $fbid;
      $_SESSION['FULLNAME'] = $fbfullname;
      $_SESSION['EMAIL'] =  $femail;
      
      /* ---- header location after session ----*/
      header("Location: index.php");
   }else {
      $loginUrl = $helper->getLoginUrl();
      header("Location: ".$loginUrl);
   }
?>

Login page Overview

Login page is used to login into FB

<?php
   session_start();
   session_unset();
   
   $_SESSION['FBID'] = NULL;
   $_SESSION['FULLNAME'] = NULL;
   $_SESSION['EMAIL'] =  NULL;
   header("Location: index.php");        
?>

Index.php

Index page is as shown below.

<?php
   session_start(); 
?>
<html xmlns:fb = "http://www.facebook.com/2008/fbml">
   
   <head>
      <title>Login with Facebook</title>
      <link 
         href = "http://www.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css" 
         rel = "stylesheet">
   </head>
   
   <body>
      <?php if ($_SESSION['FBID']): ?>      <!--  After user login  -->
         
         <div class = "container">
            
            <div class = "hero-unit">
               <h1>Hello <?php echo $_SESSION['USERNAME']; ?></h1>
               <p>Welcome to "facebook login" tutorial</p>
            </div>
            
            <div class = "span4">
    
               <ul class = "nav nav-list">
                  <li class = "nav-header">Image</li>
      
                  <li><img src = "https://graph.facebook.com/<?php 
                     echo $_SESSION['FBID']; ?>/picture"></li>
                  
                  <li class = "nav-header">Facebook ID</li>
                  <li><?php echo  $_SESSION['FBID']; ?></li>
               
                  <li class = "nav-header">Facebook fullname</li>
      
                  <li><?php echo $_SESSION['FULLNAME']; ?></li>
               
                  <li class = "nav-header">Facebook Email</li>
      
                  <li><?php echo $_SESSION['EMAIL']; ?></li>
               
                  <div><a href="logout.php">Logout</a></div>
      
               </ul>
     
            </div>
         </div>
         
         <?php else: ?>     <!-- Before login --> 
         
         <div class = "container">
            <h1>Login with Facebook</h1>
            Not Connected
            
            <div>
               <a href = "fbconfig.php">Login with Facebook</a>
            </div>
            
            <div>
               <a href = "http://www.tutorialspoint.com"  
                  title = "Login with facebook">More information about Tutorialspoint</a>
            </div>
         </div>
         
      <?php endif ?>
      
   </body>
</html>

Logout Facebook

Below code is used to logout facebook.

<?php 
   session_start();.
   session_unset();
   
   $_SESSION['FBID'] = NULL;
   $_SESSION['FULLNAME'] = NULL;
   $_SESSION['EMAIL'] =  NULL;
   header("Location: index.php");        
?>