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.

4
  • 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. Commented Dec 17, 2011 at 17:08
  • pecl.php.net/package/json Commented Dec 17, 2011 at 17:08
  • How to use JSON in PHP 4 Commented Dec 17, 2011 at 17:09
  • possible duplicate of php 5.1.6 json_encode and codeigniter Commented Dec 17, 2011 at 17:22

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.