Hi,
i want to login with facebook and go back 1 page - window.history.back() - but only when error=1 (login.php?erro=1):

This is the process_facebook.php:

if($UserCount[0]){  
        //User is now connected, log him in
        login_user(true,$me['first_name'].' '.$me['last_name']);
        echo $_GET['erro'];
        /*if(isset($_GET['erro'])){
            echo "<script>window.history.back();</script>";
        }*/
    }
    else{
        // Insert user into Database.
        @mysql_query("INSERT INTO users (userid, fullname, email) VALUES ($uid, '$fullname','$email')");

        //User is now connected, log him in
        login_user(true,$me['first_name'].' '.$me['last_name']);
        echo $_GET['erro'];
        /*if(isset($_GET['erro'])){
            echo "<script>window.history.back();</script>";
        }*/
    }

But it does not work:

Notice: Undefined index: erro in C:\xampp\htdocs\00my_works\furgonet\process_facebook.php on line 66

You are not getting an error, only a notice. That means that there is not necessarily something wrong, it's just something to look at. The notice is probably occurring because of $_GET['erro'] not having been defined before the lines that say echo $_GET['erro'];.

What can i do to make it get the value 'erro' from login.php?erro=1 in the process_facebook.php?

header.php

<!-- CALL JQUERY FACEBOOK CONNECT -->
        <script>
            function AjaxResponse(){
                var myData = 'connect=1'; //For demo, we will pass a post variable, Check process_facebook.php
                jQuery.ajax({
                    type: "POST",
                    url: "process_facebook.php",
                    dataType:"html",
                    data:myData,
                    success:function(response){
                        $("#results").html('<fieldset style="padding:20px">'+response+'</fieldset>'); //Result
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        $("#results").html('<fieldset style="padding:20px;color:red;">'+thrownError+'</fieldset>'); //Error
                    }
                });
            }

            function LodingAnimate(){ //Show loading Image
                $("#LoginButton").hide(); //hide login button once user authorize the application
                $("#results").html('<img src="ajax-loader.gif" /> Please Wait Connecting...'); //show loading image while we process user
            }

            function ResetAnimate(){ //Reset User button
                $("#LoginButton").show(); //Show login button 
                $("#results").html(''); //reset element html
            }
        </script>

process_facebook.php:

<?php
session_start();
/*
check our post variable from index.php, just to insure user isn't accessing this page directly.
You can replace this with strong function, something like HTTP_REFERER
*/
if(isset($_POST["connect"]) && $_POST["connect"]==1)
{       
    include_once("config.php"); //Include configuration file.

    //Call Facebook API
    if (!class_exists('FacebookApiException')) {
    require_once('inc/facebook.php' );
    }
        $facebook = new Facebook(array(
        'appId' => $appId,
        'secret' => $appSecret,
    ));

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

    // redirect user to facebook login page if empty data or fresh login requires
    if (!$fbuser){
        $loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>$return_url, false));
        header('Location: '.$loginUrl);
    }

    //user details
    $fullname = $me['first_name'].' '.$me['last_name'];
    $email = $me['email'];

    /* connect to mysql */
    $connecDB = mysql_connect($hostname, $db_username, $db_password)or die("Unable to connect to MySQL");   
    mysql_select_db($db_name,$connecDB);

    //Check user id in our database
    $result = mysql_query("SELECT COUNT(userid) FROM users WHERE userid=$uid");
    $UserCount = mysql_fetch_array($result); 

    if($UserCount[0]){  
        //User is now connected, log him in
        login_user(true,$me['first_name'].' '.$me['last_name']);

        if(isset($_GET['erro'])){
            echo 'ERRO found';
        }
        else{
            echo 'ERRO not found';
        }
    }
    else{
        // Insert user into Database.
        @mysql_query("INSERT INTO users (userid, fullname, email) VALUES ($uid, '$fullname','$email')");

        //User is now connected, log him in
        login_user(true,$me['first_name'].' '.$me['last_name']);

        if(isset($_GET['erro'])){
            echo 'ERRO found';
        }
        else{
            echo 'ERRO not found';
        }
    }

    mysql_close($connecDB);
}

function login_user($loggedin,$user_name){
    $_SESSION['logged_in']=$loggedin;
    $_SESSION['user_name']=$user_name;
}
?>

If login.php is the page that receives the erro=1, you could check on that page if it has indeed been set. E.g.: You can check if $_GET['erro'] is set by just outputting it and see if anything is displayed on the screen. Something like

<?php
echo '<p>For testing purposes we\'re echo\'ing $_GET[\'erro\']. Its value is: ' . $_GET['erro'] . '</p>';

// Another test:
if(!empty($_GET['erro']))
{
    echo '<p>erro appears to have been set, its value is: ' . $_GET['erro'] . '</p>';
}
else
{
    echo '<p>error appears to be empty.</p>';
}

on your login.php page. Just for testing purposes. If you want to pass it on to the next page, I would suggest you include it in the form that is submitted to go to that next page. Create a hidden input or something. For example:

<input type="hidden" name="erro" value="<?php echo $_GET['erro']; ?>">

Then on the next page, you can check if $_POST['erro'] is set. Or, if you're sending the form through GET, you should check $_GET instead of $_POST, of course.

I changed POST to GET:

<!-- CALL JQUERY FACEBOOK CONNECT -->
        <script>
            function AjaxResponse(){
                var myData = 'connect=1'; //For demo, we will pass a post variable, Check process_facebook.php
                jQuery.ajax({
                    type: "GET",
                    url: "process_facebook.php",
                    dataType:"html",
                    data:myData,
                    success:function(response){
                        $("#results").html('<fieldset style="padding:20px">'+response+'</fieldset>'); //Result
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        $("#results").html('<fieldset style="padding:20px;color:red;">'+thrownError+'</fieldset>'); //Error
                    }
                });
            }

            function LodingAnimate(){ //Show loading Image
                $("#LoginButton").hide(); //hide login button once user authorize the application
                $("#results").html('<img src="ajax-loader.gif" /> Please Wait Connecting...'); //show loading image while we process user
            }

            function ResetAnimate(){ //Reset User button
                $("#LoginButton").show(); //Show login button 
                $("#results").html(''); //reset element html
            }
        </script>

and it gives me 'ERROR not found':

if(isset($_GET["connect"]) && $_GET["connect"]==1){
    if(isset($_GET["erro"]) && $_GET["erro"]==1){
        echo 'ERRO found';
        die();
    }
    else{
        echo 'ERRO not found';
        die();
    }
 }
 else{
     echo 'Not Connected';
 }

Well, you must make sure that your AJAX function is actually sending data to your process_facebook.php page as well. In your example, you are sending the value of your Javascript variable named "myData", which is:

var myData = 'connect=1';

That means that the data you are sending, through either POST or GET (GET in your last example), is only "connect=1". If you want to also send the value of $_GET['erro'], you need to make sure that Javascript receives that value and passes it on to the page that is requested.

Member Avatar for LastMitch

@PF2G

The question I have is what is

$me = $facebook->api('/me'); //user

Does the api connected correctly. There are tutorial that explain how to used api with facebook.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.