Login with Facebook and Paypal Integration in PHP

Last updated on May 31 2022
Aridam Das

Table of Contents

Login with Facebook and Paypal Integration in PHP

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 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.tecklearn.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.tecklearn.com" 

                  title = "Login with facebook">More information about Tecklearn</a>

            </div>

         </div>

        

      <?php endif ?>

     

   </body>

</html>

It will produce the result. Before trying this example, please logout your face book account in your browser.

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");       

?>

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");     
die();  
}
?>

 

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.

  • php − This file has included API user name, password and signature.
  • php − This file has included PayPal Services, which is used to call PayPal services.
  • php − This file has included a form with minimum fields required to make payment process and it will return payment success or failure.
  • 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.

So, this brings us to the end of blog. This Tecklearn ‘Login with Facebook and Paypal Integration in PHP’ blog helps you with commonly asked questions if you are looking out for a job in PHP Programming. If you wish to learn Java and build a career Java Programming domain, then check out our interactive, Java and JEE Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/java-and-jee-training/

Java and JEE Training

About the Course

Java and JEE Certification Training is designed by professionals as per the industrial requirements and demands. This training encompasses comprehensive knowledge on basic and advanced concepts of core Java & J2EE along with popular frameworks like Hibernate, Spring & SOA. In this course, you will gain expertise in concepts like Java Array, Java OOPs, Java Function, Java Loops, Java Collections, Java Thread, Java Servlet, and Web Services using industry use-cases and this will help you to become a certified Java expert.

Why Should you take Java and JEE Training?

  • Java developers are in great demand in the job market. With average pay going between $90,000/- to $120,000/- depending on your experience and the employers.
  • Used by more than 10 Million developers worldwide to develop applications for 15 Billion devices.
  • Java is one of the most popular programming languages in the software world. Rated #1 in TIOBE Popular programming languages index (15th Consecutive Year)

What you will Learn in this Course?

Introduction to Java

  • Java Fundamentals
  • Introduction to Java Basics
  • Features of Java
  • Various components of Java language
  • Benefits of Java over other programming languages
  • Key Benefits of Java

Installation and IDE’s for Java Programming Language

  • Installation of Java
  • Setting up of Eclipse IDE
  • Components of Java Program
  • Editors and IDEs used for Java Programming
  • Writing a Simple Java Program

Data Handling and Functions

  • Data types, Operations, Compilation process, Class files, Loops, Conditions
  • Using Loop Constructs
  • Arrays- Single Dimensional and Multi-Dimensional
  • Functions
  • Functions with Arguments

OOPS in Java: Concept of Object Orientation

  • Object Oriented Programming in Java
  • Implement classes and objects in Java
  • Create Class Constructors
  • Overload Constructors
  • Inheritance
  • Inherit Classes and create sub-classes
  • Implement abstract classes and methods
  • Use static keyword
  • Implement Interfaces and use it

Polymorphism, Packages and String Handling

  • Concept of Static and Run time Polymorphism
  • Function Overloading
  • String Handling –String Class
  • Java Packages

Exception Handling and Multi-Threading

  • Exception handling
  • Various Types of Exception Handling
  • Introduction to multi-threading in Java
  • Extending the thread class
  • Synchronizing the thread

File Handling in Java

  • Input Output Streams
  • io Package
  • File Handling in Java

Java Collections

  • Wrapper Classes and Inner Classes: Integer, Character, Boolean, Float etc
  • Applet Programs: How to write UI programs with Applet, Java.lang, Java.io, Java.util
  • Collections: ArrayList, Vector, HashSet, TreeSet, HashMap, HashTable

Java Database Connectivity (JDBC)

  • Introduction to SQL: Connect, Insert, Update, Delete, Select
  • Introduction to JDBC and Architecture of JDBC
  • Insert/Update/Delete/Select Operations using JDBC
  • Batch Processing Transaction
  • Management: Commit and Rollback

Java Enterprise Edition – Servlets

  • Introduction to J2EE
  • Client Server architecture
  • URL, Port Number, Request, Response
  • Need for servlets
  • Servlet fundamentals
  • Setting up a web project in Eclipse
  • Configuring and running the web app with servlets
  • GET and POST request in web application with demo
  • Servlet lifecycle
  • Servlets Continued
  • Session tracking and filter
  • Forward and include Servlet request dispatchers

Java Server Pages (JSP)

  • Fundamentals of Java Server Page
  • Writing a code using JSP
  • The architecture of JSP
  • JSP Continued
  • JSP elements: Scriptlets, expressions, declaration
  • JSP standard actions
  • JSP directives
  • Introduction to JavaBeans
  • ServletConfig and ServletContext
  • Servlet Chaining
  • Cookies Management
  • Session Management

Hibernate

  • Introduction to Hibernate
  • Introduction to ORM
  • ORM features
  • Hibernate as an ORM framework
  • Hibernate features
  • Setting up a project with Hibernate framework
  • Basic APIs needed to do CRUD operations with Hibernate
  • Hibernate Architecture

POJO (Plain Old Java Object)

  • POJO (Plain Old Java Object)
  • Persistent Objects
  • Lifecycle of Persistent Object

Spring

  • Introduction to Spring
  • Spring Fundamentals
  • Advanced Spring

Got a question for us? Please mention it in the comments section and we will get back to you.

 

 

 

 

0 responses on "Login with Facebook and Paypal Integration in PHP"

Leave a Message

Your email address will not be published. Required fields are marked *