How to Fix “Call to undefined function filter_var” Error on Joomla

A well known client of us came to us yesterday evening and told us that he was getting a blank page when he saves the user’s data on a custom made component. We knew there was a fatal error somewhere so we set the “Error Reporting” to “Maximum” and we tried to save the profile, and we saw the following error:

Fatal error: Call to undefined function filter_var() in /components/com_user/controller.php on line 74

We became perplexed when we saw the above error: Our client was using Joomla 1.5 which does not require the availability of the function filter_var. For those who don’t know, filter_var is a PHP function that sanitizes/validates input (such as checking if a POST field is a valid email). This function is only available as of PHP 5.2.0 – our client had PHP 5.1.6.

After a quick investigation, we discovered that the controller.php file located under the /components/com_user/ has been modified to accommodate our client’s needs – but it was assumed at the time of modification that our client was using a PHP version that is higher or equal to version 5.2.0.

We had 4 options to fix the problem:

  1. Upgrade the PHP version to the latest version on our client’s server

    This option seemed at first glance the best option, but we dismissed it nearly immediately, because upgrading PHP may have repercussions on other areas of the website, especially when taking into consideration that Joomla’s core was heavily modified (there were many files other than the controller.php file that were modified). Our client was in a hurry and we did not want to create more work to solve the problem – albeit that more work is better on the long term.

  2. Implement the function filter_var from scratch and place it in a common PHP file

    This option made sense, but the problem is that the filter_var function is an extremely complicated and versatile function. Re-creating it from scratch might take days (and not hours). So, again, we dismissed this option.

  3. Remove all calls to filter_var from the code

    We thought, since the function never worked in the first place, why leave it there? Removing all instances of the function from the code will solve the problem. The validation was not working anyway (and it wasn’t critically needed).

    But, what if the client decides to upgrade PHP to the latest version in the future (which is something that will happen sooner or later)? He will lose that validation because it would have been already removed. Our third option was also dismissed.

  4. Create a skeleton of the filter_var function that always returns true

    We then pondered: Since we can’t upgrade PHP, we can’t re-create the function, and we can’t remove all instances of the function, then why not create a skeleton of the function and place it in a common include file? The skeleton filter_var function will just return the value (unmodified) in all circumstances – additionally, future compatibility can be maintained by ensuring that the skeleton function is only declared if it doesn’t already exist.

    So here’s what we did:

    • We opened the index.php located under the root of the website. We chose this file because it’s included across the board on the frontend – you can choose another common file if you wish. if you also have the problem on the backend, then you should add the below code to the index.php located under the administrator directory as well.
    • We added the following code to the index.php file (just before define( ‘_JEXEC’, 1 );):

      if (!function_exists('filter_var')){
      	function filter_var($value, $filter_type) {
      		return $value;
      	}
      }

    • We saved the file and uploaded it back and that solved the problem.

    Please note that this is not a long term solution because you will lose the validation/sanitation performed by the filter_var function – but, if you’re in a hurry and you need to address the problem rapidly, then this is the most convenient solution (it’s also the safest).

If you need help implementing the above solution or if you’re looking to upgrade PHP on your server to address this issue, then your best bet is to contact us. We can help you implement the above or ensure that a PHP upgrade on your server has no repercussions on your website. Our prices are affordable, our service is top notch, and our clients really love us! What more could you ask for?

No comments yet.

Leave a comment