perhaps? 2019-03-03 22:41 采纳率: 100%
浏览 98

PHP和cURL —奇怪的输出

I'm mainly playing with PHP and cURL (the code includes some AJAX and HTML as well).

Architecture:

Front <--> Middle <--> Back <-->MySQL

Description:

  1. In my front section I'm creating an AJAX object and doing a POST request with some data (JSON format) obtain from a Form in the HTML.
  2. Data is sent to my middle server (PHP file). This file receives it and json_decodes it. Determines witch switch case to use and sends it to the back server using cURL.
  3. Back server (PHP file) gets data from POST request and it json_decodes the data. It then proceeds to create MySQL connection checks if passwords match. If the passwords match it echos back a string saying "GRATNED".
  4. Data is passed back to middle server and then to front section where AJAX receives it and displays the string.

So...

All of this works perfect. However, for some reason my data contains a 1 at the end. which messes up my Regular Expression in my JS file.

Can you guys please let me know what option do cURL (if that is the case) do I have to modify or what is it that I'm doing that I get that one(1) and how to remove it.

Thank you guys.

Attached is my code & and images of output...

Front

function whenSubmitt()
{
  //Get the data that I want to pass
  //JS Object
  var parameters = {"case":"login",
  "username":document.getElementById("username").value,
  "password":document.getElementById("password").value
  };

  //Make into JSON object
  parameters = JSON.stringify(parameters);

  //Create AJAX object
  var xobj = new XMLHttpRequest();
  var method = "POST";
  var url = "./front.php";

  //Open Connection
  xobj.open(method,url,true);
  xobj.setRequestHeader("content-type", "application/x-www-form-urlencoded");

  //When Submit button is pressed
  xobj.onreadystatechange = function()
  {
    if (xobj.readyState == 4 && xobj.status == 200)
    {
      var respuestas = xobj.responseText;
      document.getElementById("msrv_answer").innerHTML = respuestas;
      //window.location.replace(respuestas[0]); //REDIRECTS TO NEW PAGE
    }
  };


  xobj.send(parameters);
}

Front PHP

<?php

function contact_middle_man($parameters)
{
 $url = "https://myurl/middle/middle.php";

 $obj = curl_init();
 curl_setopt($obj, CURLOPT_URL, $url);
 curl_setopt($obj, CURLOPT_POST, strlen($parameters));
 curl_setopt($obj, CURLOPT_POSTFIELDS, $parameters);
 curl_setopt($obj, CURLOPT_RETURNTRANSFER, true); //ALLOWS TO GET ANSWER BACK IN STRING FORMAT, AND DOES NOT OUTPUT ANSWER DIRECTLY.

 $ans = curl_exec($obj);

 curl_close($obj);

 return $ans;
}


/*RECEIVE DATA FROM WEB INTERFACE, USER*/
$indata = file_get_contents("php://input");

/*CONTACT MIDDLE MAN, USE CURL*/
$middle_answ = contact_middle_man($indata);

echo $middle_answ;
?>

Middle PHP

<?php

function http_post_back_server($url, $data)
{
    $obj = curl_init();

    curl_setopt($obj, CURLOPT_URL, $url);
    curl_setopt($obj, CURLOPT_POST, strlen($data));
    curl_setopt($obj, CURLOPT_POSTFIELDS, $data);

    $ans = curl_exec($obj);

    curl_close($obj);

    return $ans;
}

/*URL TO BACK SERVER*/
$url_myserver = "https://myurl/loginquery_v2_.php";

/*GLOBAL VARS*/
$back_ans ="";

/*RECEIVE DATA FROM POST REQUEST*/
$indata = file_get_contents("php://input");
$data = json_decode($indata, true);


/*MAKE REQUEST TO SERVERS*/
switch($data["case"]){
    case "login":
        $back_ans = http_post_back_server($url_myserver,$indata);
        break;
    default:
        $back_ans="NADA";
        break;
}

/*ANSWER BACK TO FRON END*/
echo $back_ans;

?>

Back PHP

<?php

/*RECEIVING DATA FROM POST REQUEST */
$indata = file_get_contents("php://input");

/*DATA TO JSON OBJ*/
$indata = json_decode($indata, true);


/*CONNECTION TO DATABASE */
$conn=mysqli_connect(myusername, mypassword);

/*CHECKING DATABASE CONNECTIVITY */

if(mysqli_connect_error())
{ echo "Connection Error: ".mysqli_connect_error; }

/*GOOD CONNECTION ... CONTINUE */

$uname = $indata["username"];

$query="SELECT * FROM alpha WHERE username ='".$indata["username"]."'";

$db_output = mysqli_query($conn,$query);

/* CHECK QUERY RESULT */
if($db_output)
{

 /* FETCH RESULTS */
 while($result = mysqli_fetch_assoc($db_output))
 {

  /* COMPARE STORE PWD VS RECEIVED PWD */
  if($result["password"] == $indata["password"])
  {
    /*JSON OBJECT*/
    echo "ACCESS GRANTED";
  }

  /* PASSWORDS DOES NOT MATCH */
  else
  {
    /*JSON OBJECT*/
    echo "ACCESS DENY";
  }

 }
}

/*CLOSE DATABASE CONNECITON */

mysqli_close($conn);

?>

PAGE WITH OUTPUT

enter image description here

Thank you guys.

  • 写回答

1条回答

  • weixin_33682719 2019-03-04 00:01
    关注

    This is occurring because in your Middle PHP, you are missing the CURLOPT_RETURNTRANSFER option in your curl call. As a result, $ans is assigned the value true (because the curl call is successful) and the output from the curl call (ACCESS GRANTED) is echo'ed into the output from Middle PHP, followed by $back_ans, which being true, when it is echo'ed produces a 1 in the output. Thus the string returned to Front PHP is ACCESS GRANTED1. You can fix this by adding this to Middle PHP:

    curl_setopt($obj, CURLOPT_RETURNTRANSFER, true);
    

    Then $ans will be assigned the value ACCESS GRANTED instead of true and your output will be as expected.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型