Undefined Index Notice in Joomla Websites: How to Remove

Before we begin, we must stress that the “Undefined Index” message is a notice, and not an error – which means that it will not, by itself, stop your website from functioning, but its presence may be the indication of a serious problem, for example: menu not appearing, form not working, site not displaying information as it should, etc…

But what is a notice?

A notice, in PHP terms (PHP is the scripting language that powers Joomla), is more or less a complaint. For example, if you’re using a deprecated function such as ereg_replace (by the way, we have had quite a few sites with the ereg_replace() is deprecated notice that we needed to fix) then PHP will complain with a notice. A notice may also be displayed if you’re trying to use questionable casting that PHP thinks will not return the result that you want (for example, if you try to forcefully cast an array into a string). As for the “Undefined Index” notice, you may get one if you have something like the below code in one of your extensions:

if ($result == 5)
	$equalsToFive = 1;

PHP will complain (and throw the famous “Undefined Index” notice) because you will most likely use the $equalsToFive variable somewhere in your code, but that variable only exists and can only have a value if $result equals to 5.

A clean way to write the above code would be something like the following:

$equalsToFive = 0;
if ($result == 5)
	$equalsToFive = 1;

In the above case, $equalsToFive always exists, regardless of whether $result is equal to 5 or not.

Another scenario where you may get the “Undefined Index” notice is when you try to get the value of a request variable (a variable from $_GET or $_POST), but that variable doesn’t exist. Take a look at the below code:

$userId = $_POST['userId'];

But what if $_POST[‘userId’] does not exist (because userId is not a field in the form that submits to the page that has the above code)? What will the value of $userId be? So, you need to check first if userId is being submitted. Here’s how:

$userId = "";
if (isset($_POST['userId']))
	$userId = (int)$_POST['userId']; //we are assuming that userId should always be an integer

So how can someone hide the “Undefined Index” notice in Joomla?

Luckily, there are 3 methods to do this:

  • Method #1: Change the Error Reporting in the Global Configuration to “None”.

    All you need to do is to login to the backend of your Joomla website, and go to Site->Global Configuration, and then click on Server, and change the value next to Error Reporting to “None”. Choosing “None” will ensure that notices, along with errors and warnings, will not be displayed on your website. This is the recommended setting for the production version of any Joomla website – since any other setting may reveal some technical information about your website that will open the door for hackers to abuse it.

    Note that you can also do the above in the configuration.php file directly – by setting the value of $error_reporting to “None” (or to “-1” for older Joomla versions – see this post on Error Reporting in Joomla).

  • Method #2: Change PHP’s error reporting in the .htaccess file to hide all errors.

    You can do that by adding the following code to your .htaccess file:

    php_flag display_startup_errors on
    php_flag display_errors on
    php_flag html_errors on

    The above code will ensure that no error whatsoever will be displayed on your website. Note that if you have an Error Reporting setting in your configuration settings other than “Default”, then this setting will override the error reporting defined in your .htaccess. For example, if your Error Reporting is set to “Maximum”, then the above code in your .htaccess file has no effect.

  • Method #3: Fix the problem.

    Yes – we know that fixing code is not an easy task – especially if you’re not a technical person, but as we stated earlier in this post, a notice may be the indication of something bigger that may affect the stability of your whole website. Better hire some Joomla experts (such as us!) to fix the problem for you if you think it’s a bit over your head.

If you’re seeing an annoying notice on your Joomla website, then try to do one of the methods above to remove it – we suggest you use the first method because it’s easy and can be done in less than a minute. However, keep in mind that you have not fixed the issue, you have only hidden the issue. In other words, the issue is still there so it’s recommended that you fix it (keep in mind, that the latest version of Joomla doesn’t display a single notice in its default installation – even when the Error Reporting is set to “Maximum” – which means that any notice you may see on your website is the work of a 3rd party/non-official extension). If you do want to fix the problem rather than just hiding it, then we can help! Just contact us and we’ll analyze the issue and fix it in not time and for a very reasonable fee! Oh, we’re fun and flexible to work with, and we’re also probably the friendliest programmers on this planet!

No comments yet.

Leave a comment