3

I have a jQuery login form that uses $post function to send credentials to the php script that checks the credentials againts the users in the database.

I have been using PHP 5.2 and have been using echo json_encode($data); in my php scripts to send back error messages to the login form witout any issues. The problem is now I have a client that has PHP 4.4.1 installed and it doesn't recognise this command and throw the following error.

Fatal error: Call to undefined function: json_encode()

Is there an equivalent to json_encode() in version PHP 4.4.1?

Any help would be great.

Thanks in advance.

Chill Web Designs
  • 1,311
  • 2
  • 16
  • 31
  • 1
    PHP 4.4.1 was released October 31, 2005. There is no reason why they haven't updated by now. I would suggest telling them to PHP rather than changing your code. –  Dec 17 '11 at 17:08
  • [How to use JSON in PHP 4](http://www.epigroove.com/posts/97/how_to_use_json_in_php_4_or_php_51x) – Clive Dec 17 '11 at 17:09
  • possible duplicate of [php 5.1.6 json_encode and codeigniter](http://stackoverflow.com/questions/2900511/php-5-1-6-json-encode-and-codeigniter) – mario Dec 17 '11 at 17:22
  • http://pecl.php.net/package/json – Andronicus Dec 17 '11 at 17:08

3 Answers3

2

You should use this wrapper function: http://www.boutell.com/scripts/jsonwrapper.html. All you need to do with this solution is to include that json_encode.php and it will do a function_exists check. If it is doesn't exist, it adds the function.

Richard
  • 4,341
  • 5
  • 35
  • 55
1

JSON support has been in PHP core since version 5.2.0. So you need to use PECL::json instead.

Also, consider updating to PHP5 as support for PHP4 has been discontinued since 2007-12-31 and it might have open security issues.

plaes
  • 31,788
  • 11
  • 91
  • 89
0

You can use Services_JSON (part of PEAR), which is php4 compatible. If you don't have access to that either, then I'm afraid you will just have to write your own function:

function my_json_encode($arrayus) {
   $newarr = '{';
   foreach ($arrayus as $key => $val) {
      $newarr .= '"' . $key . '":"' . $val . '",';
   }
   $newarr = substr($newarr, 0, strlen($newarr) - 1);
   $newarr .= '}';
   return $newarr;
}

The above is not compatible with json_encode(), but it may work for your purposes.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405