Hello friends, on the last Facebook PHP SDK Tutorial we have created a simple login app. In this Facebook PHP SDK Tutorial we will do some more interesting things. We will get the email, username and the profile pic from the facebook account. So lets get into it.
Setting UP Things for Facebook PHP SDK Tutorial
- We have already done this but if you haven’t read the last tutorial you should check it from the link below.
Facebook PHP SDK Tutorial – Part 1
- So lets move ahead.
Designing Your HTML with CSS
- In this part of Facebook PHP SDK Tutorial we will try to make our layout looks good, So we will do some CSS as well.
- Here is my CSS Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
body{ width:100%; margin:0; } .facebooklogin{ font-size:100%; font-family:arial; display:block; width:200px; height:20px; background-color:#142dac; color: #FFFFFF; text-decoration:none; padding:20px; border-radius:5px; margin:0 auto; text-align:center; } .facebooklogin:hover{ background-color:#4158cc; } .user{ font-size:100%; font-family:arial; display:block; width:100%; background-color:#142dac; color: #FFFFFF; text-decoration:none; border-radius:5px; } .wrapper{ width:60%; margin:0 auto; padding:20px; } .tg {border-collapse:collapse;border-spacing:0; width:100%;} .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;} .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;} |
Using Session to Check a User is Logged in Or Not
- If the user is logged in successful we will set a session, so that we don’t need to login everytime.
1 2 3 |
$_SESSION['token']=$sess->getToken(); |
- Now at the top we will check, if the session is already set. If the session is already set we will initialize our session so we do not need to login again.
1 2 3 4 5 |
if(isset($_SESSION['token'])){ $sess = new FacebookSession($_SESSION['token']); } |
- Now we will create a logout link.
1 2 3 |
$logout = 'http://localhost/FacebookLoginPHP?logout=true'; |
- On logout we will unset the session.
1 2 3 4 5 |
if(isset($_REQUEST['logout'])){ unset($_SESSION['token']); } |
- We will be accessing the birthday and email of the user this time, so we need to set permission.
- On getLoginUrl we will pass an array as argument.
1 2 3 4 5 6 7 8 9 |
<a class="facebooklogin" href=' <?php echo $helper->getLoginUrl(array('scope'=>'email,user_birthday')); ?> '> <i class="fa fa-facebook"></i> Sign in with Facebook </a> |
- Finally we will get the user information
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
if(isset($sess)){ $_SESSION['token']=$sess->getToken(); $request = new FacebookRequest($sess, 'GET', '/me'); $response = $request->execute(); $graph = $response->getGraphObject(GraphUser::className()); $name = $graph->getName(); $id = $graph->getId(); $img = 'https://graph.facebook.com/'.$id.'/picture?width=300'; $email = $graph->getProperty('email'); $birthday = $graph->getProperty('birthday'); $sex = $graph->getProperty('gender'); |
- We have all the details now.
- So we only need to display the details in our page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<div class='user'> <div class='wrapper'> <table class="tg"> <tr> <th class="tg-031e" rowspan="5"><img src='<?php echo $img;?>' /></th> <th class="tg-031e" ><?php ?></th> <th class="tg-031e"><?php echo $name; ?></th> </tr> <tr> <td class="tg-031e">User ID</td> <td class="tg-031e"><?php echo $id; ?></td> </tr> <tr> <td class="tg-031e">Email</td> <td class="tg-031e"><?php echo $email; ?></td> </tr> <tr> <td class="tg-031e">Gender</td> <td class="tg-031e"><?php echo $sex; ?></td> </tr> <tr> <td class="tg-031e">Date of Birth</td> <td class="tg-031e"><?php echo $birthday; ?></td> </tr> </table> </div> </div> |
- We will also create a logout button with the URL we already defined.
1 2 3 4 5 |
<a class="facebooklogin" href='<?php echo $logout; ?>'> <i class="fa fa-facebook"></i> Logout </a> |
- Thats it, you can see the final code for our index.php below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
<?php define('APP_ID', 'PUT YOUR APP ID'); define('APP_SECRET', 'PUT YOUR APP SECRET'); define('REDIRECT_URL','http://localhost/FacebookLoginPHP/'); ?> <!DOCTYPE html> <html> <head> <title>Facebook Login Sample</title> <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css"> <style> body{ width:100%; margin:0; } .facebooklogin{ font-size:100%; font-family:arial; display:block; width:200px; height:20px; background-color:#142dac; color: #FFFFFF; text-decoration:none; padding:20px; border-radius:5px; margin:0 auto; text-align:center; } .facebooklogin:hover{ background-color:#4158cc; } .user{ font-size:100%; font-family:arial; display:block; width:100%; background-color:#142dac; color: #FFFFFF; text-decoration:none; border-radius:5px; } .wrapper{ width:60%; margin:0 auto; padding:20px; } .tg {border-collapse:collapse;border-spacing:0; width:100%;} .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;} .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;} </style> </head> <body> <?php /* Author: Belal Khan website: www.simplifiedcoding.net */ //INCLUDING LIBRARIES require_once('lib/Facebook/FacebookSession.php'); require_once('lib/Facebook/FacebookRequest.php'); require_once('lib/Facebook/FacebookResponse.php'); require_once('lib/Facebook/FacebookSDKException.php'); require_once('lib/Facebook/FacebookRequestException.php'); require_once('lib/Facebook/FacebookRedirectLoginHelper.php'); require_once('lib/Facebook/FacebookAuthorizationException.php'); require_once('lib/Facebook/FacebookAuthorizationException.php'); require_once('lib/Facebook/GraphObject.php'); require_once('lib/Facebook/GraphUser.php'); require_once('lib/Facebook/GraphSessionInfo.php'); require_once('lib/Facebook/Entities/AccessToken.php'); require_once('lib/Facebook/HttpClients/FacebookCurl.php'); require_once('lib/Facebook/HttpClients/FacebookHttpable.php'); require_once('lib/Facebook/HttpClients/FacebookCurlHttpClient.php'); //USING NAMESPACES 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\GraphUser; use Facebook\GraphSessionInfo; use Facebook\HttpClients\FacebookHttpable; use Facebook\HttpClients\FacebookCurlHttpClient; use Facebook\HttpClients\FacebookCurl; //STARTING SESSION session_start(); FacebookSession::setDefaultApplication(APP_ID,APP_SECRET); $helper = new FacebookRedirectLoginHelper(REDIRECT_URL); $sess = $helper->getSessionFromRedirect(); if(isset($_REQUEST['logout'])){ unset($_SESSION['token']); } if(isset($_SESSION['token'])){ $sess = new FacebookSession($_SESSION['token']); } $logout = 'http://localhost/FacebookLoginPHP?logout=true'; if(isset($sess)){ $_SESSION['token']=$sess->getToken(); $request = new FacebookRequest($sess, 'GET', '/me'); $response = $request->execute(); $graph = $response->getGraphObject(GraphUser::className()); $name = $graph->getName(); $id = $graph->getId(); $img = 'https://graph.facebook.com/'.$id.'/picture?width=300'; $email = $graph->getProperty('email'); $birthday = $graph->getProperty('birthday'); $sex = $graph->getProperty('gender'); ?> <div class='user'> <div class='wrapper'> <table class="tg"> <tr> <th class="tg-031e" rowspan="5"><img src='<?php echo $img;?>' /></th> <th class="tg-031e" ><?php ?></th> <th class="tg-031e"><?php echo $name; ?></th> </tr> <tr> <td class="tg-031e">User ID</td> <td class="tg-031e"><?php echo $id; ?></td> </tr> <tr> <td class="tg-031e">Email</td> <td class="tg-031e"><?php echo $email; ?></td> </tr> <tr> <td class="tg-031e">Gender</td> <td class="tg-031e"><?php echo $sex; ?></td> </tr> <tr> <td class="tg-031e">Date of Birth</td> <td class="tg-031e"><?php echo $birthday; ?></td> </tr> </table> </div> </div> <br /> <a class="facebooklogin" href='<?php echo $logout; ?>'> <i class="fa fa-facebook"></i> Logout </a> <?php }else{ ?> <a class="facebooklogin" href='<?php echo $helper->getLoginUrl(array('scope'=>'email,user_birthday'));?>'> <i class="fa fa-facebook"></i> Sign in with Facebook </a> <?php } ?> </body> </html> |
- Try running your project.
- Bravo! Its working, so thats it for this Facebook PHP SDK Tutorial friends. Comment if having any confusions or troubles. Thank You 🙂
- You can also download the source code of this Facebook PHP SDK Tutorial from the link given below.
[easy_media_download url=”https://dl.dropboxusercontent.com/s/kmbcm7zpfzqwe2f/Facebook-PHP-SDK-Tutorial.zip?dl=0″ text=”Download Source”]