Hey guys I am having trouble with cURL using PHP. So basically we have front-end on one server, middle on one server, and back-end on another server. They are suppose to communicate with each other using cURL. From the front-end a person enters there username and password, from there front-end uses cURL to send middle the $_POST fields for username and password, after that middle passes them along to back-end who then checks it within the database and echo's whether the credentials are legit. For some reason we keep getting "NULL" after someone tries to login. It should print "success" or "no". I think its a problem with the json variable that back-end is echoing. But I am not sure here are the code fragments. What are we doing wrong?
Front-end
<?PHP
// sends user name and password to the middle end and checks for cookies
$Username = $_POST['Username'];
$Password = $_POST['password'];
$url1= "http://ift.tt/1JkFDAv";
$cookie="cookie.txt";
$postdata = array("UCID" => $Username, "Pass" => $Password);
//authenticate against our database
$ch = curl_init();
//echo $ch;
curl_setopt ($ch, CURLOPT_URL, $url1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec($ch);
curl_close($ch);
// $arr=json_decode($result, true);
$type = $arr['usertype'];
$Username = $arr['username'];
// $arr = var_dump(json_decode($result, true));
//
// echo = $arr;
?>
Middle
<?PHP
//$credentials= file_get_contents('php://input');
$credentials=array("Username"=>$_POST["UCID"], "Password"=>$_POST["Pass"]);
$url1="http://ift.tt/1OpBZx9";
//$url1="http://ift.tt/1JkFBIO";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $credentials);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec($ch);
curl_close($ch);
$json=json_decode($result);
var_dump($json);
echo $json->result;
?>
Back-end
<?php
//initialize variables to connect to DB
$DBuser = "xxxxx";
$DBpass = "xxxxx";
$host = "xxxxx";
$DBname = "vm276";
//database connection
$connect = mysqli_connect($host,$DBuser,$DBpass,$DBname);
//connection doesnt work
if (!$connect)
{
//echo "ERROR I FAILED " . PHP_EOL;
//echo "FAILED ATTEMPT AGAIN " . mysqli_connect_errno() . PHP_EOL;
//echo "SORRY, ITS A NO GO " . mysqli_connect_error() . PHP_EOL;
exit;
}
//connection DOES work
else
{
//echo "CONNECTION SUCCESS!!!";
//$user = "test1";
//$pass = "pass1";
$user = $_POST['Username'];
$pass = $_POST['Password'];
//$check = $_POST['Check'];
$sql = "SELECT * FROM users WHERE username = '$user' AND password = '$pass'";
//check if user exists
$res = mysqli_query($connect,$sql);
$amt = $res->num_rows;
//SQL record IS found
if ($amt == 1)
{
$json =array("result"=>"yes");
}
//SQL record is NOT found
else
{
$json = array("result"=>"no");
}
mysqli_close($connect);
echo json_encode($json);
}
?>
Aucun commentaire:
Enregistrer un commentaire