0

I have this piece of code which works well if the ['extensions'] array exists.. but if the array does not exist then it returns errors. How can I fix this code to not return anything if the extensions array does not exist?

-[UPDATE-

Sorry i had previously inserted the wrong code.. here is the proper code i need checked.

        $oid = array('id-ce-subjectAltName');
        $count = count($cert['tbsCertificate']);
        for($i = 0; $i < $count; $i++) {
             if(array_key_exists('extensions', $cert['tbsCertificate']) &&
                in_array($cert['tbsCertificate']['extensions'][$i]['extnId'], $oid)) {
                $value = $cert['tbsCertificate']['extensions'][$i]['extnId'];
                echo "\n",'<b>[SANs]</b>',"\n","\n";
            }
        }

I get this warning when ['extensions'] does not exist - I would like to prevent any warnings from being generated.

Notice: Undefined index: extensions in C:\xampp\htdocs\labs\certdecode\certdecode.php on line 142

user3436467
  • 1,763
  • 1
  • 22
  • 35
  • possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – kero Apr 27 '15 at 11:10
  • what is line 142? you can still suppress this (as long as you know and you're sure that you want to suppress that) by using error_reporting. http://php.net/manual/en/function.error-reporting.php – briosheje Apr 27 '15 at 11:11
  • Please provide the output of `print_r($cert['tbsCertificate']);` ... – Code Lღver Apr 27 '15 at 11:16

2 Answers2

0

AFTER UPDATE:

How is the structure of the array?

You count the number of items in $cert['tbsCertificate'], but in your loop, your $i is for the number of items in $cert['tbsCertificate']['extensions'].

So maybe you try to do something like this?:

    $oid = array('id-ce-subjectAltName');
    if (array_key_exists('extensions', $cert['tbsCertificate']) &&
        is_array($cert['tbsCertificate']['extensions'])
    ) {
        $count = count($cert['tbsCertificate']['extensions']);
        for ($i = 0; $i < $count; $i++) {
            if (in_array($cert['tbsCertificate']['extensions'][$i]['extnId'], $oid)) {
                    $value = $cert['tbsCertificate']['extensions'][$i]['extnId'];
                    echo "\n", '<b>[SANs]</b>', "\n", "\n";
            }
        }
    }
david00f
  • 51
  • 5
0

okay, seems to work by adding an isset() at the start of the code:

if(isset($cert['tbsCertificate']['extensions'])) {

thanks guys!

user3436467
  • 1,763
  • 1
  • 22
  • 35