I am using PHP for the server side of my android app. The app sends the login parameters rollno and password with POST
method. The code is fetching the result and echoing the response with JSON
. I have hosted the same system on the live server as well as on my localhost. The live server works fine, but the local server is not echoing the response. The code is as below:
// an array to display response
$response = array();
if (isset($_GET['apicall'])) {
switch ($_GET['apicall']) {
case 'login':
// this part will handle the login
// for login we need the email and password
if (isTheseParametersAvailable(array(
'rollno',
'password'
))) {
// getting server ip for building image url
$server_ip = gethostbyname(gethostname());
// image upload path
define('UPLOAD_PATH', 'uploads/');
// getting values
$rollno = $_POST['rollno'];
$user_password = $_POST['password'];
$sql = "SELECT password from users WHERE rollno = \"$rollno\"";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
$pass_hash = $row["password"];
}
if (password_verify($user_password, $pass_hash)) {
// creating the query
$stmt = $conn->prepare("SELECT id, username, email, gender, rollno, image, personal_notice FROM users WHERE rollno = ?");
$stmt->bind_param("s", $rollno);
$stmt->execute();
$stmt->store_result();
// query for fetching fees records
$feesqry = $conn->prepare("SELECT total, paid, remaining FROM fees WHERE rollno = ?");
$feesqry->bind_param("s", $rollno);
$feesqry->execute();
$feesqry->store_result();
// query for fetching attendance records
$attqry = $conn->prepare("SELECT mon, tue, wed, thrs, fri, sat, sun FROM attendance WHERE rollno = ?");
$attqry->bind_param("s", $rollno);
$attqry->execute();
$attqry->store_result();
// query for fetching test records
$tstqry = $conn->prepare("SELECT tst1_date, tst1_marks, tst1_rank, tst2_date, tst2_marks, tst2_rank, tst3_date, tst3_marks, tst3_rank, tst4_date, tst4_marks, tst4_rank, tst5_date, tst5_marks, tst5_rank FROM test WHERE rollno = ?");
$tstqry->bind_param("s", $rollno);
$tstqry->execute();
$tstqry->store_result();
// query for fetching common notice
$noticeqry = $conn->prepare("SELECT common_notice FROM notice");
$noticeqry->execute();
$noticeqry->store_result();
// if the user exist with given credentials
if ($stmt->num_rows > 0) {
$stmt->bind_result($id, $username, $email, $gender, $rollno, $image_temp, $personal_notice);
$stmt->fetch();
$image = 'http://' . $server_ip . '/arsod_dynamic/' . UPLOAD_PATH . $image_temp;
$feesqry->bind_result($total, $paid, $remaining);
$feesqry->fetch();
$attqry->bind_result($mon, $tue, $wed, $thrs, $fri, $sat, $sun);
$attqry->fetch();
$tstqry->bind_result($tst1_date, $tst1_marks, $tst1_rank, $tst2_date, $tst2_marks, $tst2_rank, $tst3_date, $tst3_marks, $tst3_rank, $tst4_date, $tst4_marks, $tst4_rank, $tst5_date, $tst5_marks, $tst5_rank);
$tstqry->fetch();
$noticeqry->bind_result($common_notice);
$noticeqry->fetch();
$user = array(
'id' => $id,
// Various other key/values here
$response['user'] = $user;
}
else {
// if the user not found
$response['error'] = false;
$response['message'] = 'Invalid Roll No. or Password';
}
}
else {
// if the user not found
$response['error'] = false;
$response['message'] = 'Invalid Roll No. or Password';
}
}
break;
else {
// if it is not api call
// pushing appropriate values to response array
$response['error'] = true;
$response['message'] = 'Invalid API Call';
}
// displaying the response in json structure
echo json_encode($response);
// function validating all the paramters are available
// we will pass the required parameters to this function
function isTheseParametersAvailable($params)
{
// traversing through all the parameters
foreach($params as $param) {
// if the paramter is not available
if (!isset($_POST[$param])) {
// return false
return false;
}
}
// return true if every param is available
return true;
}
The above code has line echo json_encode($response);
the also, json
is not returned. Please help.
There is successful JSON output on the live server like this
{"error":false,"message":"Login successfull","user":{"id":498,"username":"Test Entry","email":null,"gender":"NA","rollno":1000,"image":"https:\/\/softglobe.net\/client-projects\/arsodclasses\/dynamic\/uploads\/default-pic.png","personal_notice":"Not available yet","common_notice":"Freedom in Mind;Faith in Words;Pride in our Heart; Memories in our Souls.Let\u2019s salute the Nation on Republic Day.HAPPY REPUBLIC DAY!","total":"-","paid":"-","remaining":"-","mon":"-","tue":"-","wed":"-","thrs":"-","fri":"-","sat":"-","sun":"-","tst1_date":"-","tst1_marks":"-","tst1_rank":"-","tst2_date":"-","tst2_marks":"-","tst2_rank":"-","tst3_date":"-","tst3_marks":"-","tst3_rank":"-","tst4_date":"-","tst4_marks":"-","tst4_rank":"-","tst5_date":"-","tst5_marks":"-","tst5_rank":"-"}}
But on the local server, nothing prints as a response.