1

While printing json encode data from file it print scripts too . what can be solution to the problem

<?php

/*
 * Following code will get single product details
 * A product is identified by product id (pid)
 */

// array for JSON response

$response = array();

$servername = "mysql11.000webhost.com";
$username = "a1978721_test";
$password = "heavenhell1";

$dbhandle = mysql_connect($servername, $username, $password) 
  or die("Unable to connect to MySQL");


$selected = mysql_select_db("a1978721_test",$dbhandle) 
  or die("Could not select examples");

// check for post data
if (isset($_GET["pid"])) {
    $pid = $_GET['pid'];

    // get a product from products table
    $result = mysql_query("SELECT * FROM discount WHERE id = $pid");

    if (!empty($result)) {
        // check for empty result
        if (mysql_num_rows($result) > 0) {
         $student = array();
          while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {



            $student["exam_roll"] = $row["exam_roll"];
            $student["class_no"] = $row["class_no"];
            $student["block_no"] = $row["block_no"];
            $student["name"] = $row["name"];
            // success
        }
            $response["success"] = 1;

            // user node

            $response["student"] = array();

            array_push($response["student"], $student);

            // echoing JSON response


        } else {
            // no product found
            $response["success"] = 0;
            $response["message"] = "No student found";

            // echo no users JSON


        }
    } else {
        // no product found
        $response["success"] = 0;
        $response["message"] = "No student found";

        // echo no users JSON


    }
} else {
    // required field is missing

    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response


}

 header('Content-Type: application/json');
 echo json_encode($response);
?>

Its output comes like this which is exactly what i don't want . i need to remove the script section from the output . Any Help will be appreciated :)

{"success":1,"student":[{"exam_roll":"1212","class_no":"121","block_no":"1221","name":"rohit"}]}    
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->
shrestha rohit
  • 2,920
  • 2
  • 13
  • 20

2 Answers2

1

The Problem

Your hosting provider is injecting ads into the response body you are sending back. There are two ways you can fix this. The first is that you remove it from every response (just remove the first n number of lines when your Javascript parses it), or you can find a way to work around the way the system is implemented.

Removing It From Javascript

I'm assuming you are ingesting the response in Javascript (however the same idea would apply anywhere).

// break the textblock into an array of lines
var lines = jsonResponse.split('\n');
// remove one line, starting at the first position
// the removing the next 4 elements.
lines.splice(0,4);
// join the array back into a single string
var jsonResponse = lines.join('\n');

Turning off Analytics

I'm not sure, but I'm guessing you are using 000webhosts. If you are, any of the methods in here work for you:

Webhoster inserts a javascript which brokes my code how to remove it?

Changing appending rules in .htaccess

<FilesMatch "\.(php)$">
php_value auto_append_file none
</FilesMatch>
Community
  • 1
  • 1
Daymon Schroeder
  • 682
  • 1
  • 4
  • 23
0

comment the line and check the output

// header('Content-Type: application/json');

Vipul sharma
  • 1,245
  • 1
  • 13
  • 33