1

Possible Duplicate:
curl_exec() always returns false

Hi I am new in php and I am stuck on this thing for while now. So i have two domains with their own DB. So if a user tries to login on one domain and his credentials are not found in the DB it is curl to other domain to see if his credentials exists or not. So a match means num_of_rows = 1, else 0.

I am able to curl everything after curl works fine but i dont get back any value in return. Here is my code:-

logincheck.php:

<?php
    session_start();
if(isset($_POST['Email'], $_POST['Password'])){
    $email = $_POST["Email"];   
    $password = $_POST["Password"];

        print "email received $email";
        print "<br>password received $password";

    $db_username="root";
    $db_password="root";
    $database="photobook";
    $localhost = "mysql";

    $con = mysql_connect($localhost,$db_username,$db_password);
    mysql_select_db($database,$con) or die( "Unable to select database");

    $query = "select * from photobook.users where email ='$email' and password ='$password';" ;

    $result = mysql_query($query);

    $num=mysql_num_rows($result);

    if($num == 1){
        while($row = mysql_fetch_array($result))
             {
                $_SESSION['email'] = $row['email'];

                $_SESSION['username'] = $row['username'];   
             }

        header("location: home.php");

    }

    else{

        include "Protocol.php";

                print "<br>email after protocol file include is $email";
                print "<br>password after protocol file include is $password";

        $obj=new Protocol();

        $val = $obj->loginCheck($email,$password);

                **print "<br>value of val received is $val";**

        if($val == 0){
            session_destroy();
                        print "<br>user does not exist";
            header("location: login.php");
        }
        else{
            $_SESSION['email'] = $val[0];
            $_SESSION['username'] = $val[1];
            print "<br>user exists";
            header("location: home.php");

        }   
    }

    mysql_close($con);

    }
?>

Protocol.php on first domain:-

<?php

    class Protocol{   
        function loginCheck($p_email,$p_password){

            $sid = 2;

            $msg="sid=$sid&email=$p_email&password=$p_password";

            print "<br>parameter string sent $msg";

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,"http://www.snapreveal.com/PhotoBook/src/Protocol.php");
            curl_setopt($ch, CURLOPT_POST, TRUE);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $msg);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            $p_result = curl_exec($ch);
            curl_close($ch);
            if ($p_result === FALSE) {
                  die("Curl error: " . curl_error($ch));
            }
            return $p_result;

        }

    } //end of protocol class
?>

Protocol.php on second domain:-

<?php

    class Protocol{

        function loginCheckResponse($p_email,$p_password){

            print "<br>inside second domain loginCheckResponse";
            print "<br>email inside second domain loginCheckResponse $p_email";
            print "<br>password inside second domain loginCheckResponse $p_password";

            $db_username="root";
            $db_password="root";
            $database="photobook";
            $localhost = "mysql";

            $con = mysql_connect($localhost,$db_username,$db_password);
            mysql_select_db($database,$con) or die( "Unable to select database");

            $query = "select * from photobook.users where email ='$p_email' and password ='$p_password';" ;

            $result = mysql_query($query);

            $p_num=mysql_num_rows($result);

            print "<br>num found inside teja loginCheckResponse $p_num";

            mysql_close($con);

            if($p_num == 1){
              while($row = mysql_fetch_array($result))
             {

                                $u_email = $row['email'];

                                $u_username = $row['username'];

                                $res = array($u_email,$u_username);
                                print "<br>inside second domain logincheck, after match values in res array are, email $res[0],username $res[1]";
                                print "<br>value in res<br>";
                                print $res;
                                return $res;
             }
            }
            else{
              return 0;
            }

        }

    } //end of protocol class

    $pobj=new Protocol();

    $sid = $_POST['sid'];

    $p_email = $_POST['email'];

    $p_password = $_POST['password'];

    if($_POST['sid'] == 2 ){
        $pobj->loginCheckResponse($p_email,$p_password);

        }
?>

Output of prints:

email received max@gmail.com
password received max
email after protocol file include is max@gmail.com
password after protocol file include is max
parameter string sent sid=2&email=max@gmail.com&password=max
value of val received is 
inside second domain loginCheckResponse
email inside second domain loginCheckResponse max@gmail.com
password inside second domain loginCheckResponse max
num found inside second domain loginCheckResponse 1
inside second domain logincheck, after match values in res array are, email max@gmail.com,username Max
value in res
Array
user does not exist

So "value of val received is" this line should print after curl returns value back but it print prior to that. And also there is no value received back. Any thoughts why?

Community
  • 1
  • 1
user1371033
  • 353
  • 4
  • 7
  • 19

1 Answers1

3

curl returns boolean FALSE on failure, which'd print as an empty string. You should NEVER assume success when dealing with a remote service, and always check for errors:

    $p_result = curl_exec($ch);
    if ($p_result === FALSE) {
        die("Curl error: " . curl_error($ch));
    }
    curl_close($ch);

note the use of the === strict equality test. This is necessary to differentiate between a real boolean false, and a result which naturally typecats to a false (empty string, 0, etc...).

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Thanks for reply but still the problem persists. Well the order in which the prints should be done is also incorrect logically. I just don't understand why the statements which is supposed to print after loginCheck() function call is getting printed prior to it. – user1371033 Nov 29 '12 at 19:06
  • Here is an update, in the output i just figured out that the few lines are the "print" statements written inside the remote method. So its like it is appending "print" statements and returning back but not the actual function return value. Any idea why this is happening? – user1371033 Nov 30 '12 at 23:49