我主要是在玩PHP和cURL(代码还包括一些AJAX和HTML)。
架构:
正面<->中间<->背面<-> MySQL
说明:
所以...
所有这些都完美。但是,由于某种原因,我的数据末尾包含1。弄乱了我的JS文件中的正则表达式。
你们能告诉我我要修改 cURL (如果是这种情况)是什么选项,或者我正在做什么,我得到那个(1)以及如何将其删除。
谢谢你们。
附上我的代码和输出图像...
正面
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);
}
前端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;
?>
中级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;
?>
返回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);
?>
具有输出的页面
谢谢你们。
答案 0 :(得分:2)
之所以发生这种情况,是因为在您的中间PHP 中,您缺少curl调用中的CURLOPT_RETURNTRANSFER
选项。结果,$ans
被分配了值true
(因为curl调用成功),并且curl调用(ACCESS GRANTED
)的输出被回显到中间PHP ,后跟$back_ans
,这是正确的,在回显时在输出中生成1
。因此,返回到 Front PHP 的字符串为ACCESS GRANTED1
。您可以通过将其添加到 Middle PHP :
curl_setopt($obj, CURLOPT_RETURNTRANSFER, true);
然后,将为$ans
分配值ACCESS GRANTED
而不是true
,您的输出将达到预期的水平。