title: How can i navigate to same page after successful google/facebook login?
body: <p>I have create a website using php which is having google and facebook login. This website having 6 pages. For all the 6 pages header and footer are common(included). Assume I am login into the site using google from page 5. After successful/failure login, the page navigate to index.php (page 1) instead of page5. How can i navigate to the same page after successful/failure login of google and facebook. Is this possible without adding all the pages in google/facebook developer console? </p>
  
  <p>Also i have tried to change the header location after successful login. It throws cannot reach, out of time error. Can any one help to solve this problem. Thanks in advance. </p>
  
  <p><strong>Note:</strong> I am using google and facebook Oauth service for login.</p>
  
  <p>Here is my code:
  google login.</p>
  
  <pre><code>&lt;?php
  
  session_start();
  require_once 'dbconnection.php';
  
  //Google API PHP Library includes
  require_once  'gvendor/vendor/autoload.php';
  require_once 'gvendor/vendor/google/apiclient/src/Google/Client.php';
  require_once 'gvendor/vendor/google/apiclient/src/Google/Service/Oauth2.php';
  
  // Fill CLIENT ID, CLIENT SECRET ID, REDIRECT URI from Google Developer Console
   $client_id = 'xxxxxxx';
   $client_secret = 'xxxxxxx';
   $redirect_uri = 'http://localhost:80/tthtml/index.php';
   $simple_api_key = 'xxxxxxx';
  
   global $googleauthUrl;
  
  //Create Client Request to access Google API
  $client = new Google_Client();
  $client-&gt;setApplicationName("PHP Google OAuth Login Example");
  $client-&gt;setClientId($client_id);
  $client-&gt;setClientSecret($client_secret);
  $client-&gt;setRedirectUri($redirect_uri);
  $client-&gt;setDeveloperKey($simple_api_key);
  $client-&gt;addScope("https://www.googleapis.com/auth/userinfo.email");
  $client-&gt;setAccessType('offline');
  
  
  //Send Client Request
  $objOAuthService = new Google_Service_Oauth2($client);
  
  try{
  //Logout
  if (isset($_REQUEST['logout'])) {
    $client-&gt;revokeToken($_SESSION['access_token']);
    unset($_SESSION['access_token']);
    unset($_SESSION['google_user_name']);
    session_unset();
    header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); //redirect user back to page
  }
  
  //Authenticate code from Google OAuth Flow
  //Add Access Token to Session
  if (isset($_GET['code'])) {
    $client-&gt;authenticate($_GET['code']);
    $_SESSION['access_token'] = $client-&gt;getAccessToken();
    header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
  }
  
  //Set Access Token to make Request
  if (isset($_SESSION['access_token']) &amp;&amp; $_SESSION['access_token']) {
    $client-&gt;setAccessToken($_SESSION['access_token']);
  }
  
  //Get User Data from Google Plus
  //If New, Insert to Database
  if ($client-&gt;getAccessToken()) {
    $userData = $objOAuthService-&gt;userinfo-&gt;get();
    $_SESSION['google_user_name']=$userData['given_name'];
    $_SESSION['user_id']=$userData-&gt;id;
  
          if(!empty($userData)) {
              $dbObj=new database();  
              $dbObj-&gt;openconnection();
               $sql='select * from tttbl_user where google_fb_id='.$userData-&gt;id;
                 $existing_member = $dbObj-&gt;existingMember($sql);
                 if(empty($existing_member)) {
                     $sql="insert into tttbl_user (google_fb_id, user_name, gender, email_id, gplus_link, profile_photo, created_date) values('".$userData-&gt;id."','".$userData-&gt;name."','".$userData-&gt;gender."','".$userData-&gt;email."','".$userData-&gt;link."','".$userData-&gt;picture."',now())";
                     $dbObj-&gt;newUser($sql);
                }
                $dbObj-&gt;closeconnection();
           }
  
    } 
  else{
          $googleauthUrl = $client-&gt;createAuthUrl();
      }
  }
   catch(Exception $ee)
   { }
  
  ?&gt;
  </code></pre>
  
  <p>facebook login</p>
  
  <pre><code>&lt;?php //
  //ob_start();
  session_start();
  require_once 'dbconnection.php';
  require_once 'fvendor/vendor/autoload.php';
  require_once 'fvendor/vendor/facebook/php-sdk/src/facebook.php';
  
  // Create our Application instance (replace this with your appId and secret).
  global $facebook_loginUrl;
  
  $appId='xxxxxx';
  $secretkey='xxxxxx';
  
  $facebook = new Facebook(array(
    'appId'  =&gt; 'xxxxxxx',
    'secret' =&gt; 'xxxxxxxx',
  ));
  
  // Get User ID
  $fb_user = $facebook-&gt;getUser();
  
  if(isset($_REQUEST['fb_logout'])){
      //$accessToken=null;
      //$logoutUrl = $helper-&gt;getLogoutUrl($_SESSION['facebook_access_token'], 'http://localhost/fblogin/fblogin.php');
      //unset($_SESSION['facebook_access_token']);
      unset($_SESSION['facebook_user_name']);
      session_unset();
      session_destroy();
      $fb_user = null;
      header('Location: http://localhost:80/tthtml/index.php');  
  }
  
  
  if ($fb_user) {
    try {
          // Proceed knowing you have a logged in user who's authenticated.
         $uid = $facebook-&gt;getUser();
         $user_profile = $facebook-&gt;api('/me?fields=id,name,picture,email,gender');
  
      }
      catch (FacebookApiException $e) {
        error_log($e);
        $fb_user = null;
      }
  }
  else 
  {
      $facebook_loginUrl = $facebook-&gt;getLoginUrl(array('scope' =&gt; 'email,user_birthday,user_photos','req_perms' =&gt; 'user_mobile_phone',));
          $facebookurlstring=$facebook_loginUrl;
          $x=strpos($facebookurlstring,'redirect_uri=');
          $y=strpos($facebookurlstring, 'state');
          $facebookurllength=  strlen($facebookurlstring);
          //substr($str,0,$y+13).'http://localhost/tthtml/index.php'.substr($str, $y+13+$x,$length)
          $facebook_loginUrl=substr($facebookurlstring,0,$x+13).'http://localhost/tthtml/index.php'.substr($facebookurlstring, $y-1,$facebookurllength);
  }
  
  if($fb_user) 
  {
          $userid=$user_profile['id'];
      $username = $user_profile['name'];        
      $useremail = $user_profile['email'];
  
          $userpicture=$user_profile['picture']['data']['url'];
          $usergender=$user_profile['gender'];
          $mobilenumber=$user_profile['user_mobile_phone'];
  
          $_SESSION['facebook_user_name']=$username;
      $_SESSION['user_id']=$userid;
  
          if(isset($_SESSION['facebook_user_name'])&amp;&amp; $_SESSION['facebook_user_name']) {
              $dbObj=new database();  
              $dbObj-&gt;openconnection();
              $sql='select * from tttbl_user where google_fb_id='.$userid.';';
                 $existing_member = $dbObj-&gt;existingMember($sql);
                 if(empty($existing_member)) {
                     $sql="insert into tttbl_user (google_fb_id, user_name, gender, email_id, profile_photo, created_date)"
                             . " values('".$userid."','".$username."','".$usergender."','".$useremail."','".$userpicture."',now())";
                     $dbObj-&gt;newUser($sql);
                }
                $dbObj-&gt;closeconnection();
  
           }
  } 
  ?&gt;
  </code></pre>
  
  <p>If I change the redirect URI, it will search in the URL list in developer console. If it is not available in the list of developer console, then it throws ulr is not available in whitelist(for facebook) and page not found (for google) error is thrown. </p>