Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Facebook login

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Facebook login

Postby shiniie » Sun Apr 14, 2013 10:44 pm

Hi!

Im trying to make a Facebook login for my website and always get errors. I got the files from GitHub and used the example.php and the files from the src.

example.php

Code: Select all
<?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 'facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => 'the id',
  'secret' => 'secret',
  'cookie' => true
));




// Get User ID
$user = $facebook->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.

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl(array(
      'next'   => 'http://unity.mppsaalk.se', // URL to which to redirect the user after logging out
      ));;
} else {
  $loginUrl = $facebook->getLoginUrl(array('scope'=> 'publish_stream',  'redirect_uri'   => 'http://unity.mppsaalk.se',
) );
}

// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');

?>
<!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="<?php echo $logoutUrl; ?>">Logout</a>
    <?php else: ?>
      <div>
        Login using OAuth 2.0 handled by the PHP SDK:
        <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 ?>

    <h3>Public profile of Naitik</h3>
    <img src="https://graph.facebook.com/naitik/picture">
    <?php echo $naitik['name']; ?>
  </body>
</html>


and i get this message

Warning: session_start() [function.session-start]: open(/storage/content/70/173870/unity.mppsaalk.se/sessions/sess_bfk38f6sm7l6o2j6380ai1fvp1, O_RDWR) failed: No such file or directory (2) in /storage/content/70/173870/unity.mppsaalk.se/public_html/examples/facebook.php on line 49
php-sdk

Login using OAuth 2.0 handled by the PHP SDK: Login with Facebook
PHP Session

Array
(
[fb_567618623258914_state] => d9bad0470cfe40c4939d70d686e8d420
)
You are not Connected.
Public profile of Naitik

Naitik Shah
Warning: Unknown: open(/storage/content/70/173870/unity.mppsaalk.se/sessions/sess_bfk38f6sm7l6o2j6380ai1fvp1, O_RDWR) failed: No such file or directory (2) in Unknown on line 0

Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/storage/content/70/173870/unity.mppsaalk.se/sessions) in Unknown on line 0




and when i click on "login with Facebook ", i get this from Facebook:

Error
An error occurred. Please try again later.

API Error Code: 191
API Error Description: The specified URL is not owned by the application
Error Message: Invalid redirect_uri: Given URL is not allowed by the Application configuration.
shiniie
New php-forum User
New php-forum User
 
Posts: 1
Joined: Sun Apr 14, 2013 10:29 pm

Re: Facebook login

Postby johnj » Mon Apr 15, 2013 6:10 am

Please check your session path a).whether it is writable, b).whether the path exists
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Facebook login

Postby seandisanti » Tue Apr 16, 2013 5:29 pm

Make sure you've registered your application with facebook, and listed your url's in the application properties.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.