Php Add Facebook Share Sdk Upload Photo Complete Example

PHP Script - Login with Facebook using facebook-php-sdk and MySQL example with demo

November 11, 2016 Category : PHP Bootstrap MySql

In this postal service, I am going to share with you How to integrate login with facebook in PHP website using Facebook Graph API.

In Website ordinarily registration class for sign upwards, Merely users are not interested to filling the big registration forms. So you can implement login or annals with facebook login, users don't require to make full big form. This tutorial will help to implement login with facebook in your PHP project.

In this step by step post, i give you example of sign in with facebook account from scratch. In this instance facebook account details will get and store it on MySQL database.

After follow all step, you volition find preview like every bit bellow preview, you tin can also download script and check demo:

Preview:

Step 1: Create Facebook App

In this step, we have to create new facebook app for admission other user details, So commencement create your own facebook app By following link, here give all screen shot so follow bellow screen shot.

Become On Facebook Developer business relationship : https://developers.facebook.com/apps.

Screenshot : i

Next Click on "Add together a New App"

Screenshot : two

At present Add Facebook App name and category.

Screenshot : 3

Now Click "Setting" Link On Sidebar.

Screenshot : 4

Now, you have to add platform. You can add platform like as bellow screenhot. choose "Website" and Add your site URL(For This case add : http://localhost:8000).

Later added platform, you can get App Id and underground from here:

Step 2: Download Facebook Graph API

In this step, we take to download facebook-php-sdk from git. So, click here and download api : facebook-php-sdk.

After download But copy "src" binder and put it on your project root path, and rename it to "api".

Footstep 3: Create Database Table

In this step, we accept to create database with "users" table for store facebook details on that table, so you can create table past post-obit SQL Query:

Create Users Table

            

CREATE Table `users` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`facebook_id` varchar(255) COLLATE utf8_unicode_ci Not Nix,

`first_name` varchar(255) COLLATE utf8_unicode_ci Non NULL,

`last_name` varchar(255) COLLATE utf8_unicode_ci Not Zero,

`e-mail` varchar(255) COLLATE utf8_unicode_ci Non Nil,

`gender` varchar(10) COLLATE utf8_unicode_ci Non Cypher,

`picture` text COLLATE utf8_unicode_ci Not NULL,

PRIMARY Primal (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Step iv: Add PHP files

In this step we take to add four PHP files, as like bellow:

1. config.php : In this file, we have to add facebook app id and underground.

2. alphabetize.php : In this file, we have to add layout file and manage if login then different layout and different for logout user.

3. storeData.php : In this file, i write database logic, We volition insert and check user data from this file.

iv. logout.php : In this file, we destroy session for logout user.

config.php

            

<?php

require 'api/facebook.php';

$config['App_ID'] = 'Facebook App Id';

$config['App_Secret'] = 'Facebook App Secret';

$facebook = new Facebook(array(

'appId' => $config['App_ID'],

'underground' => $config['App_Secret']

));

?>

alphabetize.php

            

<?php

crave 'config.php';

require 'storeData.php';

$user = $facebook->getUser();

?>

<html>

<head>

<title>Login With Facebook</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.three.7/css/bootstrap.min.css">

</head>

<torso>

<div class="container" mode="margin-top:20px;">

<h2 class="text-center" >ItSolutionStuff.com</h2>

<?php if($user){ ?>

<?php

$userProfile = $facebook->api('/me?fields=id,first_name,last_name,e-mail,gender,locale,picture');

$userClass = new User;

$userData = $userClass->checkFBUserData($userProfile);

?>

<?php if(!empty($userData)){ ?>

<h3>Facebook User Details</h3>

<img src="<?php echo $userData['movie']; ?>">

<p><stiff>Fabebook ID:</potent> <?php echo $userData['facebook_id'] ?> </p>

<p><strong>Showtime Name:</stiff> <?php echo $userData['first_name'] ?> </p>

<p><strong>Last Name:</strong> <?php echo $userData['last_name'] ?> </p>

<p><potent>E-mail:</potent> <?php repeat $userData['e-mail'] ?> </p>

<p><potent>Gender:</strong> <?php echo $userData['gender'] ?> </p>

<a href="logout.php?logout">Facebook Logout</a>

<?php }else{ ?>

<p>Something is wrong.</p>

<?php } ?>

<?php }else{ ?>

<?php

$loginUrl = $facebook->getLoginUrl(['scope'=>'e-mail']);

?>

<div id="loginbox" style="margin-top:20px;" class="mainbox col-md-6 col-doctor-start-3 col-sm-8 col-sm-get-go-ii">

<div class="panel console-info" >

<div form="panel-heading">

<div class="panel-championship">Sign In</div>

<div style="float:right; font-size: 80%; position: relative; top:-10px"><a href="#">Forgot countersign?</a></div>

</div>

<div style="padding-peak:30px" form="panel-trunk" >

<div mode="display:none" id="login-alert" class="alert alert-danger col-sm-12"></div>

<course id="loginform" class="form-horizontal" role="form">

<div manner="margin-lesser: 25px" class="input-group">

<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>

<input id="login-username" type="text" class="class-control" name="username" value="" placeholder="username or email">

</div>

<div style="margin-bottom: 25px" class="input-group">

<bridge course="input-grouping-addon"><i class="glyphicon glyphicon-lock"></i></span>

<input id="login-password" type="password" class="form-command" proper name="password" placeholder="password">

</div>

<div grade="input-group">

<div class="checkbox">

<label>

<input id="login-call back" type="checkbox" name="call up" value="i"> Remember me

</label>

</div>

</div>

<div style="margin-tiptop:10px" grade="form-group">

<div class="col-sm-12 controls">

<a id="btn-login" href="#" class="btn btn-success">Login </a>

<a id="btn-fblogin" href="<?php repeat $loginUrl; ?>" course="btn btn-primary">Login with Facebook</a>

</div>

</div>

<div class="form-grouping">

<div grade="col-medico-12 control">

<div mode="border-top: 1px solid#888; padding-top:15px; font-size:85%" >

Don't have an account!

<a href="#">Sign Upwards Hither</a>

</div>

</div>

</div>

</class>

</div>

</div>

</div>

<?php } ?>

</div>

</body>

</html>

storeData.php

            

<?php

class User {

public $table_name = 'users';

function __construct(){

/* database configuration */

$dbServer = 'localhost';

$dbUsername = 'root';

$dbPassword = 'root';

$dbName = 'h_test';

/* connect database */

$con = mysqli_connect($dbServer,$dbUsername,$dbPassword,$dbName);

if(mysqli_connect_errno()){

die("Failed to connect with MySQL: ".mysqli_connect_error());

}else{

$this->connection = $con;

}

}

function checkFBUserData($user){

$prev_query = mysqli_query($this->connection,"SELECT * FROM ".$this->table_name." WHERE facebook_id = '".$user['id']."'") or die(mysql_error($this->connexion));

if(mysqli_num_rows($prev_query)>0){

$update = mysqli_query($this->connection,"UPDATE $this->table_name SET facebook_id = '".$user['id']."', first_name = '".$user['first_name']."', last_name = '".$user['last_name']."', email = '".$user['email']."', gender = '".$user['gender']."', motion-picture show = '".$user['picture']['data']['url']."' WHERE facebook_id = '".$user['id']."'");

}else{

$insert = mysqli_query($this->connection,"INSERT INTO $this->table_name SET facebook_id = '".$user['id']."', first_name = '".$user['first_name']."', last_name = '".$user['last_name']."', email = '".$user['email']."', gender = '".$user['gender']."', picture = '".$user['picture']['data']['url']."'");

}

$query = mysqli_query($this->connection,"SELECT * FROM $this->table_name WHERE facebook_id = '".$user['id']."'");

$result = mysqli_fetch_array($query);

return $result;

}

}

?>

logout.php

            

<?php

require 'config.php';

$facebook->destroySession();

session_start();

unset($_SESSION['userdata']);

session_destroy();

header("Location:alphabetize.php");

?>

Step 5: Run Example

You tin can quick run our example by following command, so run bellow command for run PHP projection.

php -S localhost:8000

Now, you can cheque from your url past bellow URL:

http://localhost:8000

I hope it can help you...

yuranighprehopecon.blogspot.com

Source: https://www.itsolutionstuff.com/post/php-script-login-with-facebook-using-facebook-php-sdk-and-mysql-example-with-demoexample.html

0 Response to "Php Add Facebook Share Sdk Upload Photo Complete Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel