1

I am having a php registration script for android devices which gives me a strange JSON Exception.

  {"error":false,"uid":"55fd7bedd4f8a2.33345247","user":{"name":"Theo 
  Tziomakas","email":"theo@gmail.com","created_at":"2015-09-19     
  18:14:53","updated_at":null}}

   org.json.JSONException: Value <br of type java.lang.String cannot be     
   converted 
   to JSONObject

I can insert data from the Android phone,but I don't know where that html thing break line is coming from,which messes up my JSON response. There are similar questions all over here,tried the solutions but nothing happened:(

Here is the php script.

<?php


   require_once 'include/DB_Functions.php';
   $db = new DB_Functions();

   // json response array
   $response = array("error" => FALSE);

   if (isset($_POST['name']) && isset($_POST['email']) &&     
   isset($_POST['password'])) {

      // receiving the post params
      $name = $_POST['name'];
      $email = $_POST['email'];
      $password = $_POST['password'];

// check if user is already existed with the same email
if ($db->isUserExisted($email)) {
       // user already existed
       $response["error"] = TRUE;
       $response["error_msg"] = "User already existed with " . $email;
       echo json_encode($response);
} else {
    // create a new user
    $user = $db->storeUser($name, $email, $password);
    if ($user) {
        // user stored successfully
        $response["error"] = FALSE;
        $response["uid"] = $user["unique_id"];
        $response["user"]["name"] = $user["name"];
        $response["user"]["email"] = $user["email"];
        $response["user"]["created_at"] = $user["created_at"];
        $response["user"]["updated_at"] = $user["updated_at"];
        echo json_encode($response);
    } else {
        // user failed to store
        $response["error"] = TRUE;
        $response["error_msg"] = "Unknown error occurred in registration!";
        echo json_encode($response);
    }
}
    } else {
       $response["error"] = TRUE;
       $response["error_msg"] = "Required parameters (name, email or    
      password)  
   is missing!";
  echo json_encode($response);
    }
   ?>

In my json response I get html tags as well!!!

<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1'  
 cellspacing='0' cellpadding='1'>

  <tr><th align='left' bgcolor='#f57900' colspan="5"><span 
  style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! 
   )</span> Notice: Undefined index: updated_at in 
    C:\wamp\www\android_login_api\register.php on line <i>34</i></th></tr>

   <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>

   <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' 
   bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th>
   <th align='left' bgcolor=''>Function</th><th align='left' 
   bgcolor='c'>Location</th></tr>

   <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' 
   align='center'>0.0008</td><td bgcolor='#eeeeec' align='right'>249992</td>
   <td bgcolor='#eeeeec'>{main}(  )</td><td 
   title='C:\wamp\www\android_login_api\register.php' 
   bgcolor='#eeeeec'>..\register.php<b>:</b>0</td></tr>

    </table></font>

   {"error":false,"uid":"...","user":
   {"name":"Theo","email":"theo@gmail.com","created_at":"2015-09-19 
   19:55:05","updated_at":""}}

In other words I want to remove this.

  <br />
  <font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1'  
   cellspacing='0' cellpadding='1'>

   <tr><th align='left' bgcolor='#f57900' colspan="5"><span 
   style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>(    
   ! 
     )</span> Notice: Undefined index: updated_at in 
    C:\wamp\www\android_login_api\register.php on line <i>34</i></th></tr>

    <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>

   <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' 
   bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th>
    <th align='left' bgcolor=''>Function</th><th align='left' 
    bgcolor='c'>Location</th></tr>

   <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' 
   align='center'>0.0008</td><td bgcolor='#eeeeec' align='right'>249992</td>
   <td bgcolor='#eeeeec'>{main}(  )</td><td 
   title='C:\wamp\www\android_login_api\register.php' 
   bgcolor='#eeeeec'>..\register.php<b>:</b>0</td></tr>

</table></font>

SOME PROGRESS

I removed the update_at field from the database which was null. I don't get those funny html tags in the JSON response. But still a weird symbol still exists.

{"error":false,"uid":"55fd9bfbdda209.31523184","user":{"name":"Theo Tziomakas","email":"theoaristi53@gmail.com","created_at":"2015-09-19 20:31:39"}}

Do you see the

 

Finally fixed

The last step I had to was go in the notepad++ and chose encode to ANSI. So at last I get the JSON I want after 9 hours!!! Php can be such a mess. I really want to learn Java EE,but I have no time:(.

Theo
  • 3,099
  • 12
  • 53
  • 94
  • is the breakline in the data, maybe the name? – FuzzyTree Sep 19 '15 at 15:32
  • This is how the json response should be. – Theo Sep 19 '15 at 15:33
  • {"error":false,"uid":"55fd8016c21fc6.67907348","user":{"name":"Theo Tziomakas","email":"theo@gmail.com","created_at":"2015-09-19 18:32:38","updated_at":null}} – Theo Sep 19 '15 at 15:33
  • I don't know why I get html code in my JSON response. – Theo Sep 19 '15 at 15:34
  • Could you please fill in your url and run this code snippet* `array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query(array( 'name'=>'foo', 'email'=>'foo@bar.baz', 'password'=>'.correct.horse.battery.staple.' )) ))); echo '
    response=', htmlspecialchars( file_get_contents(EnterUrlHere, false, $ctx) ), '
    ';` and add the result to your question text? *) after careful examination of course ....
    – VolkerK Sep 19 '15 at 15:40
  • $response = array("error" => FALSE); $ctx = stream_context_create(array('http'=>array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query(array( 'name'=>'foo', 'email'=>'foo@bar.baz', 'password'=>'.correct.horse.battery.staple.' )) ))); echo '
    response=', htmlspecialchars( file_get_contents(http://192.xxx.xx.x/android_login_api/register.php, false, $ctx) ), '
    ';
    – Theo Sep 19 '15 at 15:52
  • It gives me that error.Parse error: syntax error, unexpected ':' in C:\wamp\www\android_login_api\register.php on line 11 – Theo Sep 19 '15 at 15:52
  • Line 11 is this: htmlspecialchars( file_get_contents(192.xxx.xx.x/android_login_api/register.php, false, $ctx) ), ''; – Theo Sep 19 '15 at 15:53
  • `file_get_contents('http://192.xxx.xx.x/android_login_api/register.php', ` - it's a string literal, mark it as such with single/double quotes - and it needs the protocol – VolkerK Sep 19 '15 at 15:57
  • I did what you said,but the url never loads:(. – Theo Sep 19 '15 at 16:23
  • Please elaborate. Do you get an error message? Or ... ? It's such a simple script and almost not worth the afford if it's not running. Can you debug into the android/java code and inspect the response directly (i.e. before it gets parsed by any framework thingy)? – VolkerK Sep 19 '15 at 16:45
  • Please see the edited question with the response. The JSON response contains html as well! what??? I have never experienced such a problem. Your script somehow is making my wamp server very very slow. – Theo Sep 19 '15 at 17:00
  • So, your problem is that your server is throwing an E_NOTICE error, though continuing to process the script. The HTML is coming from the pleasantly-formatted error text. Fix the warning ("Notice: Undefined index: updated_at in C:\wamp\www\android_login_api\register.php on line 34") and you'll be fine. I'm guessing that `$db->storeUser($name, $email, $password);` returns a user object that doesn't actually have an `updated_at` field. – Matt Gibson Sep 19 '15 at 17:29
  • I removed from the database the update_at field. I don't get those funny html tags. But still a weird symbol appears in the json response. {"error":false,"uid":"55fd9bfbdda209.31523184","user":{"name":"Theo Tziomakas","email":"theoaristi53@gmail.com","created_at":"2015-09-19 20:31:39"}}. It is this one:  – Theo Sep 19 '15 at 17:34
  • 1
    Regarding your `Finally fixed` step: Your editor seems to add a [BOM](https://en.wikipedia.org/wiki/Byte_order_mark) when the document is stored utf-8 encoded. In notepad++ you can [disable this "feature"](http://stackoverflow.com/questions/8432584/how-to-make-notepad-to-save-text-in-utf-8-without-bom) – VolkerK Sep 19 '15 at 19:32

3 Answers3

1

$user has no index named 'updated_at' which means $db->storeUser($name, $email, $password); is not returning expected result ! first see if all required values are available by print_r($user) if there is not an index updated_at then there is something wrong inside this function $db->storeUser($name, $email, $password);

Samundra Khatri
  • 997
  • 1
  • 9
  • 18
0

Is this working for you? Use like this:

http://yourdomain.com/test.php?user=122&password=213414

<?php
@ini_set("display_erros", "Off");
@ini_set("display_startup_errors", "Off");
error_reporting(0);

if(!empty($_SERVER['REQUEST_METHOD']))
{
   $data= array();
     foreach (array_merge($_GET, $_POST) AS $name => $value) 
     {
         if (is_string($value) && !empty($value) && !is_numeric($value)) {
             $pattern = '/(and|or)[\s\(\)\/\*]+(update|delete|select)\W|(select|update).+\.(password|email)|(select|update|delete).+users|<script>|<\/script>/im';
             while (preg_match($pattern, $value)) {
                 if (isset($_GET[$name])) {
                     $value = $_GET[$name] = $_REQUEST[$name] = preg_replace($pattern, '', $value);
                     $data[$name] = $value;
                 } else {
                     $value = $_POST[$name] = $_REQUEST[$name] = preg_replace($pattern, '', $value);
                     $data[$name] = $value;
                 } 
             }
         }
         $data[$name] = $value;
     }
}   
//var_dump($data);

// receiving the post params
$name = $data['name'];
$email = $data['email'];
$password = $data['password'];
$response["error"] = FALSE;
$response["uid"] = 43235;
$response["user"]["name"] = $name;
$response["user"]["email"] = "bla@bla.com";
$response["user"]["created_at"] = "2015-02-20";
$response["user"]["updated_at"] = "2015-02-10";
echo json_encode($response, JSON_UNESCAPED_UNICODE);
exit;

// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Unknown error occurred in registration!";
echo json_encode($response);
?>
-1

You can remove br tag by strip_tags();

$response["user"]["name"] = strip_tags($user["name"]);

Nazrul Islam
  • 348
  • 3
  • 8