title: How to get access token from facebook
body: <h2>I'm unable to get the token and record in the database. because like facebook updated the privacy policy and now the token lasts 60 days. what should I do? ##</h2>
  
  <pre><code>&lt;?php
  include('connect.ini.php');
  require_once 'facebook.php'; 
  /** Create our Application instance. */ 
  $query = "SELECT app_id,app_secret FROM f_settings WHERE id=1";
  $result = mysql_fetch_array(mysql_query($query));
  $application_id = $result[0];
  $application_secret = $result[1];
  $facebook = new Facebook(array('appId' =&gt; $application_id,'secret' =&gt; $application_secret,'cookie' =&gt; true,));
  
  
  
  if(isset($_POST['user_id']) &amp;&amp; isset($_POST['status']) &amp;&amp; isset($_POST['page_id']))
  {
  if($_POST['status'] == 'connected') // User is already connected with our         facebook application so he will be redirected to survey url
  {
      $page_id = $_POST['page_id'];
      $query = "SELECT * FROM f_urls WHERE shrinked_url_id='$page_id'";
      $apply = mysql_query($query) or die('error 1');
      $result = mysql_fetch_array($apply) or die('error 2');
      $survey_url = $result['survey_url'];                
      die($survey_url);    // redirecting him to survey url 
  }   
  if($_POST['status'] == 'connecting')  // user is first time connecting to our application
  {
  
      $me = $facebook-&gt;api('/me/permissions');        // Double checking wheather use has allowed our application     
      if($me['data']['0']['publish_stream'] == 1)
      {
          $user_id = $_POST['user_id'];       
          $query = "SELECT COUNT(*) FROM f_users WHERE facebook_id='$user_id'";   // for fraud detection we check wheather user is really first time connecting to our app by searching him in our database
          $apply = mysql_query($query) or die('e1');
          $result = mysql_fetch_array($apply) or die('e2');       
          if($result[0] == 0)                 // if user is first time connected we will redirect him to content url :-)  
          {
              $me = $facebook-&gt;api('/me');
              $facebook_id = $me['id'];
              $gender = $me['gender'];
              $email = $me['email'];
              $name = $me['namez'];               
              $date = date('d-m-Y');
              $ip = $_SERVER['REMOTE_ADDR'];              
              // Checking for updating facebook status
  
              $query = "SELECT * FROM f_settings WHERE id=1";
              $apply = mysql_query($query);
              $result = mysql_fetch_array($apply);
  
              $web_link = $result['web_link'];
              $pic_link = $result['pic_link'];        
              $status_msg = $result['status_message'];    
              $update_status = $result['update_status'];
  
              if($update_status == 'true')
              {
                  try {                       
                      $facebook-&gt;api('/me/feed','POST',array('message'=&gt;$status_msg,'link'=&gt;$web_link,'picture'=&gt;$pic_link)); 
                  } catch (FacebookApiException $e){
  
                      }                       
              }
  
              $send_email = $result['send_email'];
              $message_subject = $result['message_subject'];
              $sender_name = $result['sender_name'];
              $reply_to = $result['reply_to'];
              $email_address = $result['email_address'];
              $message_body = $result['message_body'];
  
              if($send_email == 'true')
              {
                  if($reply_to == false)
                  {
                      $reply_to='no-reply@website.com';   
                  }
                  else
                  {
                      $reply_to = $email_address; 
                  }
                  $headers = 'From:'.$sender_name.'&lt;'.$email_address.'&gt;'."\r\n";
                  $headers .= 'To:'.$name.'&lt;'.$email.'&gt;'."\r\n".'Reply-To:'.$reply_to."\r\n";
                  $headers .= "MIME-Version: 1.0\r\n";
                  $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
                  $headers .= "X-Priority: 3\r\n";
                  $headers .= "X-Mailer: PHP". phpversion() ."\r\n";
  
                  mail($email,$message_subject, str_replace('[user_name]',$name,$message_body) ,$headers);
  
              }               
              $id = $_POST['page_id'];                
              $query = "INSERT INTO f_users SET unlocked_url='$id',facebook_id='$facebook_id',name='$name',email='$email',gender='$gender',date='$date',ip='$ip'";
              if(mysql_query($query)) // Inserting this new user details into our database
              {
                  $page_id = $_POST['page_id'];
                  $query = "SELECT * FROM f_urls WHERE shrinked_url_id='$page_id'";
                  $apply = mysql_query($query) or die('error 1');
                  $result = mysql_fetch_array($apply) or die('error 2');
                  die($result['content_url']);    // redirecting user to content url
              }
          }           
          else                // if user is trying to connect to our application for the second time he will be redirected to survey url
          {
              $page_id = $_POST['page_id'];
              $query = "SELECT * FROM f_urls WHERE shrinked_url_id='$page_id'";
              $apply = mysql_query($query) or die('error 1');
              $result = mysql_fetch_array($apply) or die('error 2');
              $survey_url = $result['survey_url'];                
              die($survey_url);    // redirecting him to survey url       
          }
      }
      else
      {
          die('you must connect');
      }
  }
  }
  ?&gt;
  </code></pre>
  
  <h2>I want to be the token saved in the mysql database, so the app do a search later.</h2>