Advertisement
freddy0512

facebook

Sep 16th, 2015
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. <!--controller -->
  2.  
  3. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  4. class Welcome extends CI_Controller {
  5.  
  6. public function __construct(){
  7. parent::__construct();
  8. // To use site_url and redirect on this controller.
  9. $this->load->helper('url');
  10. }
  11.  
  12. public function login(){
  13. $this->load->library('facebook'); // Automatically picks appId and secret from config
  14. $user = $this->facebook->getUser();
  15.  
  16. if ($user) {
  17. try {
  18. $data['user_profile'] = $this->facebook->api('/me');
  19. } catch (FacebookApiException $e) {
  20. $user = null;
  21. }
  22. }else {
  23. $this->facebook->destroySession();
  24. }
  25. if ($user) {
  26. $data['logout_url'] = site_url('welcome/logout'); // Logs off application
  27. } else {
  28. $data['login_url'] = $this->facebook->getLoginUrl(array(
  29. 'redirect_uri' => site_url('welcome/login'),
  30. 'scope' => array("email") // permissions here
  31. ));
  32. }
  33. $this->load->view('login',$data);
  34.  
  35. }
  36.  
  37. public function logout(){
  38. $this->load->library('facebook');
  39. $this->facebook->destroySession();
  40. redirect('welcome/login');
  41. }
  42.  
  43. }
  44.  
  45.  
  46.  
  47.  
  48.  
  49. <!--view--->
  50. <?php if (@$user_profile): // call var_dump($user_profile) to view all data ?>
  51.  
  52. <img src="https://graph.facebook.com/<?=$user_profile['id']?>/picture?type=large" style="width: 140px; height: 140px;">
  53.  
  54. <h2><?=$user_profile['name']?></h2>
  55.  
  56. <!--emailnya gak ke tarik om--->
  57. <h2><?=$user_profile['email']?></h2>
  58.  
  59.  
  60. <!-- Create link logout -->
  61. <a href="<?= $logout_url ?>">Logout</a>
  62. <?php else: ?>
  63. <h2>Login with Facebook Using CodeIgniter</h2>
  64. <a href="<?= $login_url ?>">Login</a>
  65. <?php endif; ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement