title: PHP Facebook Login With Mysql
body: <p>I'm trying to make a website with a facebook login button, I tried using php sdk and I have a problem: when I login the session isn't saved so it doesn't find it so it acts like it's not set.</p>
  
  <pre><code>&lt;?php
  if(!isset($_SESSION['user']))
  {
      //Application Configurations
      $app_id     = "MY_API_KEY_HERE";
      $app_secret = "MY_SECRET_KEY_HERE";
      $site_url   = "http://localhost/";
  
      try{
          include_once "src/facebook.php";
      }catch(Exception $e){
          error_log($e);
      }
      // Create our application instance
      $facebook = new Facebook(array(
          'appId'     =&gt; $app_id,
          'secret'    =&gt; $app_secret,
          ));
  
      // Get User ID
      $user = $facebook-&gt;getUser();
      // We may or may not have this data based 
      // on whether the user is logged in.
      // If we have a $user id here, it means we know 
      // the user is logged into
      // Facebook, but we don’t know if the access token is valid. An access
      // token is invalid if the user logged out of Facebook.
      //print_r($user);
      if($user){
          // Get logout URL
          $logoutUrl = $facebook-&gt;getLogoutUrl();
      }else{
          // Get login URL
          $loginUrl = $facebook-&gt;getLoginUrl(array(
              'scope'         =&gt; 'read_stream, publish_stream, email, user_about_me',
              'redirect_uri'  =&gt; $site_url,
              ));
      }
  
      if($user){
  
          try{
          // Proceed knowing you have a logged in user who's authenticated.
          $user_profile = $facebook-&gt;api('/me');
          //Connecting to the database. You would need to make the required changes in the common.php file
          //In the common.php file you would need to add your Hostname, username, password and database name!
          mysqlc();
  
          $name = GetSQLValueString($user_profile['name'], "text");
          $email = GetSQLValueString($user_profile['email'], "text");
          $gender = GetSQLValueString($user_profile['gender'], "text");
          $bio = GetSQLValueString($user_profile['bio'], "text");
          $query = sprintf("SELECT * FROM newmember WHERE email = %s",$email);
          $res = mysql_query($query) or die('Query failed: ' . mysql_error() . "&lt;br /&gt;\n$sql");
          if(mysql_num_rows($res) == 0)
          {
              $iquery = sprintf("INSERT INTO newmember values('',%s,%s,%s,%s,'yes')",$name,$email,$gender,$bio);
              $ires = mysql_query($iquery) or die('Query failed: ' . mysql_error() . "&lt;br /&gt;\n$sql");
              $_SESSION['user'] = $user_profile['email'];
              $_SESSION['id'] = $user_profile['id'];
          }
          else
          {
              $row = mysql_fetch_array($res);
              $_SESSION['user'] = $row['email'];
              $_SESSION['id'] = $user_profile['id'];
          }
          }catch(FacebookApiException $e){
                  error_log($e);
                  $user = NULL;
              }
  
      }
  }
  ?&gt;
  </code></pre>