Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > PHP > PHP Forum > ขอสอบถามเรื่องทำ Login Facebook แล้วดึงข้อมูลออกมาไม่ครบครับ



 

ขอสอบถามเรื่องทำ Login Facebook แล้วดึงข้อมูลออกมาไม่ครบครับ

 



Topic : 125244



โพสกระทู้ ( 22 )
บทความ ( 0 )



สถานะออฟไลน์




ขอสอบถามเรื่องทำ Login Facebook แล้วดึงข้อมูลออกมาไม่ครบครับ
พอดีผมทำแล้ว พวกไอดี ชื่อ เพศ มาปกติ เว้นอีเมลกับวันเกิด ต้องแก้ตรงไหนให้ดึงอีเมลกับวันเกิดมาด้วยครับ

facebook_login.php
<?php
require_once("../config.php");
require_once("sdk/facebook.php");

$facebook = new Facebook(array(
  'appId'  => 'xxxxx', 
  'secret' => 'xxxxxx',
));

// Get User ID
$user = $facebook->getUser();

if ($user) {
  try {
    $user_profile = $facebook->api('/me?fields=id,name,birthday,email,first_name,last_name,gender,hometown');
      //https://developers.facebook.com/tools/explorer/202094893551750
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl();
}

// Save to mysql
if ($user) {
	if($_GET["code"] != "")
	{
        $FBD = trim($user_profile["id"]);
        $sex = trim($user_profile["gender"]);
        if($sex == female){
            $sexs = "2";
        }elseif($sex == male){
            $sexs = "1";
        }
        $pass = rand(11111111, 99999999);
        $create_date = date("Y-m-d H:i:s");
        $active = "1";
        
        $select1 = "Select * From members Where user_fb='$FBD' ";
        $query1 = mysqli_query($link,$select1) or die (mysqli_error($link));
        $data1 = mysqli_fetch_array($query1);
      if($FBD == $data1["user_fb"]){
          $_SESSION["test"] = "same";
          $_SESSION['member_id'] = $data1['member_id'];        //เก็บค่าsession หลังกด login  
          $_SESSION['username'] = $data1['username'];
          echo "<script>alert('ยินดีต้อนรับ')</script>";
          
          echo "<script>window.location='$base_path'</script>";
          
          mysqli_close($link);
				exit();
      }else{
          $_SESSION["test"] = "insert";
      
        
				$insert = "Insert Into members (user_fb,name,username,email,password,sex,birthday,create_date,sid,active) Values 
                    ('".trim($user_profile["id"])."',
                    '".trim($user_profile["name"])."',                    
					'".trim($user_profile["first_name"])."',
                    '".trim($user_profile["email"])."',
                    '".$pass."',
                    '".$sexs."',                    
                    '".trim($user_profile["birthday"])."',
                    '".$create_date."',
                    '".session_id()."',
					'".$active."')";
               $objQuery = mysqli_query($link,$insert) or die(mysqli_error($link));
       
        
    /*-------------------------------------------------------------------------*/    

        //echo $FBD;
        $select = "Select * From members Where user_fb='$FBD' "; 
        $rows = mysqli_query($link,$select) or die(mysqli_error($link));    
        $data = mysqli_fetch_array($rows);       
                $_SESSION['member_id'] = $data['member_id'];            
                $_SESSION['username'] = $data['username'];     

   /*-------------------------------------------------------------------------*/       
				echo $_SESSION["test"];
                echo "<script>alert('ยินดีต้อนรับ')</script>";
        
                $mem = md5(md5(md5($_SESSION['member_id'])));
				echo "<script>window.location='$base_path/profile_edit.php?id=$mem'</script>";
             
                mysqli_close($link);
				exit();
      }
	}
}

// Logout
if($_GET["Action"] == "Logout")
{
	$facebook->destroySession();
    session_destroy();
	header("location:$base_path");
	exit();
}

mysqli_close($link);
?>
<!------------------------------------------------------------------>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <head>
    <title>php-sdk</title>
    <style>
      body {
        font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
      }
      h1 a {
        text-decoration: none;
        color: #3b5998;
      }
      h1 a:hover {
        text-decoration: underline;
      }
    </style>
  </head>
  <body>
    <h1>php-sdk</h1>

    <?php if ($user): ?>
      <a href="page2.php">Page 2</a> | <a href="?Action=Logout">Logout</a>
    <?php else: ?>
      <div>
        <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
      </div>
    <?php endif ?>

    <h3>PHP Session</h3>
    <pre><?php print_r($_SESSION); ?></pre>

    <?php if ($user): ?>
      <h3>You</h3>
      <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">

      <h3>Your User Object (/me)</h3>
      <pre><?php print_r($user_profile); ?></pre>
    <?php else: ?>
      <strong><em>You are not Connected.</em></strong>
    <?php endif ?>
<br> <?php echo $_SESSION['member_id']."<br>".$_SESSION['username']; ?>
  </body>
</html>



facebook.php
<?php
/**
 * Copyright 2011 Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

require_once "base_facebook.php";

/**
 * Extends the BaseFacebook class with the intent of using
 * PHP sessions to store user ids and access tokens.
 */
class Facebook extends BaseFacebook
{
  /**
   * Cookie prefix
   */
  const FBSS_COOKIE_NAME = 'fbss';

  /**
   * We can set this to a high number because the main session
   * expiration will trump this.
   */
  const FBSS_COOKIE_EXPIRE = 31556926; // 1 year

  /**
   * Stores the shared session ID if one is set.
   *
   * @var string
   */
  protected $sharedSessionID;

  /**
   * Identical to the parent constructor, except that
   * we start a PHP session to store the user ID and
   * access token if during the course of execution
   * we discover them.
   *
   * @param array $config the application configuration. Additionally
   * accepts "sharedSession" as a boolean to turn on a secondary
   * cookie for environments with a shared session (that is, your app
   * shares the domain with other apps).
   *
   * @see BaseFacebook::__construct
   */
  public function __construct($config) {
    if ((function_exists('session_status') 
      && session_status() !== PHP_SESSION_ACTIVE) || !session_id()) {
      session_start();
    }
    parent::__construct($config);
    if (!empty($config['sharedSession'])) {
      $this->initSharedSession();

      // re-load the persisted state, since parent
      // attempted to read out of non-shared cookie
      $state = $this->getPersistentData('state');
      if (!empty($state)) {
        $this->state = $state;
      } else {
        $this->state = null;
      }

    }
  }

  /**
   * Supported keys for persistent data
   *
   * @var array
   */
  protected static $kSupportedKeys =
    array('state', 'code', 'access_token', 'user_id');

  /**
   * Initiates Shared Session
   */
  protected function initSharedSession() {
    $cookie_name = $this->getSharedSessionCookieName();
    if (isset($_COOKIE[$cookie_name])) {
      $data = $this->parseSignedRequest($_COOKIE[$cookie_name]);
      if ($data && !empty($data['domain']) &&
          self::isAllowedDomain($this->getHttpHost(), $data['domain'])) {
        // good case
        $this->sharedSessionID = $data['id'];
        return;
      }
      // ignoring potentially unreachable data
    }
    // evil/corrupt/missing case
    $base_domain = $this->getBaseDomain();
    $this->sharedSessionID = md5(uniqid(mt_rand(), true));
    $cookie_value = $this->makeSignedRequest(
      array(
        'domain' => $base_domain,
        'id' => $this->sharedSessionID,
      )
    );
    $_COOKIE[$cookie_name] = $cookie_value;
    if (!headers_sent()) {
      $expire = time() + self::FBSS_COOKIE_EXPIRE;
      setcookie($cookie_name, $cookie_value, $expire, '/', '.'.$base_domain);
    } else {
      // @codeCoverageIgnoreStart
      self::errorLog(
        'Shared session ID cookie could not be set! You must ensure you '.
        'create the Facebook instance before headers have been sent. This '.
        'will cause authentication issues after the first request.'
      );
      // @codeCoverageIgnoreEnd
    }
  }

  /**
   * Provides the implementations of the inherited abstract
   * methods. The implementation uses PHP sessions to maintain
   * a store for authorization codes, user ids, CSRF states, and
   * access tokens.
   */

  /**
   * {@inheritdoc}
   *
   * @see BaseFacebook::setPersistentData()
   */
  protected function setPersistentData($key, $value) {
    if (!in_array($key, self::$kSupportedKeys)) {
      self::errorLog('Unsupported key passed to setPersistentData.');
      return;
    }

    $session_var_name = $this->constructSessionVariableName($key);
    $_SESSION[$session_var_name] = $value;
  }

  /**
   * {@inheritdoc}
   *
   * @see BaseFacebook::getPersistentData()
   */
  protected function getPersistentData($key, $default = false) {
    if (!in_array($key, self::$kSupportedKeys)) {
      self::errorLog('Unsupported key passed to getPersistentData.');
      return $default;
    }

    $session_var_name = $this->constructSessionVariableName($key);
    return isset($_SESSION[$session_var_name]) ?
      $_SESSION[$session_var_name] : $default;
  }

  /**
   * {@inheritdoc}
   *
   * @see BaseFacebook::clearPersistentData()
   */
  protected function clearPersistentData($key) {
    if (!in_array($key, self::$kSupportedKeys)) {
      self::errorLog('Unsupported key passed to clearPersistentData.');
      return;
    }

    $session_var_name = $this->constructSessionVariableName($key);
    if (isset($_SESSION[$session_var_name])) {
      unset($_SESSION[$session_var_name]);
    }
  }

  /**
   * {@inheritdoc}
   *
   * @see BaseFacebook::clearAllPersistentData()
   */
  protected function clearAllPersistentData() {
    foreach (self::$kSupportedKeys as $key) {
      $this->clearPersistentData($key);
    }
    if ($this->sharedSessionID) {
      $this->deleteSharedSessionCookie();
    }
  }

  /**
   * Deletes Shared session cookie
   */
  protected function deleteSharedSessionCookie() {
    $cookie_name = $this->getSharedSessionCookieName();
    unset($_COOKIE[$cookie_name]);
    $base_domain = $this->getBaseDomain();
    setcookie($cookie_name, '', 1, '/', '.'.$base_domain);
  }

  /**
   * Returns the Shared session cookie name
   *
   * @return string The Shared session cookie name
   */
  protected function getSharedSessionCookieName() {
    return self::FBSS_COOKIE_NAME . '_' . $this->getAppId();
  }

  /**
   * Constructs and returns the name of the session key.
   *
   * @see setPersistentData()
   * @param string $key The key for which the session variable name to construct.
   *
   * @return string The name of the session key.
   */
  protected function constructSessionVariableName($key) {
    $parts = array('fb', $this->getAppId(), $key);
    if ($this->sharedSessionID) {
      array_unshift($parts, $this->sharedSessionID);
    }
    return implode('_', $parts);
  }
}






Tag : PHP, MySQL, HTML/CSS, JavaScript









ประวัติการแก้ไข
2016-11-05 21:10:57
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2016-11-04 17:32:37 By : no007 View : 1111 Reply : 5
 

 

No. 1



โพสกระทู้ ( 204 )
บทความ ( 0 )



สถานะออฟไลน์
Facebook

โยนใส่แท๊ก PHP ครับ แบบนี้อ่านยาก เยอะด้วย






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-11-05 14:16:30 By : Pla2todkrob
 


 

No. 2



โพสกระทู้ ( 22 )
บทความ ( 0 )



สถานะออฟไลน์


ตอบความคิดเห็นที่ : 1 เขียนโดย : Pla2todkrob เมื่อวันที่ 2016-11-05 14:16:30
รายละเอียดของการตอบ ::
แก้แล้วครับ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-11-05 21:47:09 By : no007
 

 

No. 3



โพสกระทู้ ( 5,105 )
บทความ ( 4 )

สมาชิกที่ใส่เสื้อไทยครีเอท Hall of Fame 2012

สถานะออฟไลน์


อีเมลกับวันเกิด : ถ้าเจ้าของ fb เขาปิดเป็นความลับ ไม่เปิดเผย ก็ไม่น่าจะดึงมาแสดงได้มั้งครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-11-07 08:50:57 By : apisitp
 


 

No. 4



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-11-07 11:33:03 By : mr.win
 


 

No. 5



โพสกระทู้ ( 22 )
บทความ ( 0 )



สถานะออฟไลน์


ตอบความคิดเห็นที่ : 3 เขียนโดย : apisitp เมื่อวันที่ 2016-11-07 08:50:57
รายละเอียดของการตอบ ::
ออโอเคครับ ขอบคุณครับ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-11-07 12:06:06 By : no007
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ขอสอบถามเรื่องทำ Login Facebook แล้วดึงข้อมูลออกมาไม่ครบครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่