html - cURL using php and json -
hey guys having trouble curl using php. have front-end on 1 server, middle on 1 server, , back-end on server. suppose communicate each other using curl. front-end person enters there username , password, there front-end uses curl send middle $_post fields username , password, after middle passes them along back-end checks within database , echo's whether credentials legit. reason keep getting "null" after tries login. should print "success" or "no". think problem json variable back-end echoing. not sure here code fragments. doing wrong?
front-end
<?php // sends user name , password middle end , checks cookies $username = $_post['username']; $password = $_post['password']; $url1= "https://web.njit.edu/~kc328/mid.php"; $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="https://web.njit.edu/~vm276/connect.php"; //$url1="https://www.njit.edu/cp/login.php"; $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 connect 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 failed " . php_eol; //echo "failed attempt again " . mysqli_connect_errno() . php_eol; //echo "sorry, no go " . mysqli_connect_error() . php_eol; exit; } //connection work else { //echo "connection success!!!"; //$user = "test1"; //$pass = "pass1"; $user = $_post['username']; $pass = $_post['password']; //$check = $_post['check']; $sql = "select * users username = '$user' , password = '$pass'"; //check if user exists $res = mysqli_query($connect,$sql); $amt = $res->num_rows; //sql record found if ($amt == 1) { $json =array("result"=>"yes"); } //sql record not found else { $json = array("result"=>"no"); } mysqli_close($connect); echo json_encode($json); } ?>
Comments
Post a Comment