0

I have problem with the cURL look at my code



www.example1.com/index.php

$Email = $_POST['Email'];
$Password = $_POST['Password'];

$sql = $dbh->prepare('SELECT * FROM users WHERE Email = ? AND Password = ?');
$sql->execute(array($Email,$Password));

if($sql->rowCount() == 1) {
    $data = array('Found'=> 'Great');
    $string = http_build_query($data);

    $ch = curl_init("http://www.example2.com/index.php");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_exec($ch);
    curl_close($ch);
}else{
    $data = array('Found'=> 'Sorry');
    $string = http_build_query($data);

    $ch = curl_init("http://www.example2.com/index.php");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_exec($ch);
    curl_close($ch);  
}

www.example2.com/index.php

$data = array('Email'=>'Email@Example.com', 'Password'=>'Password');
$string = http_build_query($data);

$ch = curl_init("http://www.example1.com/index.php");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);


if(isset($_POST['Found'])) {
   $Found = $_POST['Found'];
   echo $Found;
}

it didn't echo anything in www.example2.com so what can i do so i can echo the Found variable

sorry for my bad English i hope you understand me and thank you :)

Hosam
  • 29
  • 8
  • You're not binding the variables to the prepared statement. Have a look at the docs for mysqli_stmt::bind_param – Josh Nov 09 '17 at 14:19
  • can you give me a code example – Hosam Nov 09 '17 at 14:20
  • None of your curl_exec are assigning the returntransfer data to a variable (nor echoing it). ? – IncredibleHat Nov 09 '17 at 14:40
  • 1
    Also I dont understand your examples, because if you run example1, curl is going to call example2, and thats going to call example1, and loop forever ? – IncredibleHat Nov 09 '17 at 14:42
  • 1
    @frozenjakalope Judging by his code, it looks like he is using PDO, which wont understand `bind_param`. He is doing it right in `execute`. – IncredibleHat Nov 09 '17 at 14:44
  • i want to check if he is an active account so if the account found or not – Hosam Nov 09 '17 at 15:00
  • I agree this looks like it would create an infinite loop of requests. And how do you know that example2 didn't echo anything when you called it from example1? In example1 you ignore any response coming back from the curl operation. – ADyson Nov 09 '17 at 15:29
  • so what should i do?? – Hosam Nov 09 '17 at 15:30
  • to avoid an infinite loop? Stop having both pages make requests to each other. Example1 should call Example2 and wait for a response (at the moment, Example1 ignores the response it gets from Example2). Why does Example2 need to call Example1? That part doesn't make much sense. Especially when that will result in another call to Example2, which will result in another call to Example1, and so on, forever. Maybe you need to take a PHP/cURL tutorial and also have a better understanding of how HTTP requests work. – ADyson Nov 09 '17 at 15:31
  • Example2 need to call Example1 so i can know if the user found it will make the script run – Hosam Nov 09 '17 at 15:33
  • That doesn't make sense. Example1 can find that out by waiting for a response from Example2 (i.e. waiting for the info you send back via the `echo`). Making another curl request to Example1 just triggers a _separate_ execution of Example1 from the beginning anyway, in a new context - it doesn't return info to the original instance of Example1. Again, learn how HTTP and cURL work. `curl_exec()` in Example1 will return a response object - you need to read that response into a variable. It will contain whatever is echoed from Example2. – ADyson Nov 09 '17 at 15:35
  • how? can u give me an code example? – Hosam Nov 09 '17 at 15:37
  • curl_exec is just a function, so you get its output just the same way that you get any variable which is return from a function. See this answer for an example: See https://stackoverflow.com/questions/6516902/how-to-get-response-using-curl-in-php – ADyson Nov 09 '17 at 15:38
  • i appreciate your help <3 – Hosam Nov 09 '17 at 15:41

1 Answers1

1

Try to save the output of the curl to a variable like this:

$Found = curl_exec($ch);

drkostas
  • 517
  • 1
  • 7
  • 29