Undefined index: level in constants

I get an error : “Undefined index: level” in /var/www/html/web/app/plugins/s2member/src/includes/classes/constants.inc.php line 600

This reads
define ('S2MEMBER_CURRENT_USER_PAID_REGISTRATION_DAYS', ($c[] = (($user && (int)@$paid_registration_times['level']) ? (int)floor((strtotime('now') - (int)$paid_registration_times['level']) / 86400) : 0)));

This adds the php-error class to admin body which generates a redundant space between admin menu en admin bar.

Hi Berry.

Thanks for catching that!

Could you try this and let me know if it takes care of it for you?

				define ('S2MEMBER_CURRENT_USER_PAID_REGISTRATION_DAYS', ($c[] = (($user && isset($paid_registration_times['level']) && (int)@$paid_registration_times['level']) ? (int)floor((strtotime('now') - (int)$paid_registration_times['level']) / 86400) : 0)));

I’ll be adding it to the next release.

:slight_smile:

Will try, when I find time, but I have to admit, I don’t like the way your constant is set… it’s not really a constant, imo… it’s a variable, because it can change…

so not really a constant anymore imo…

I have changed it, but it doesn’t solve the error. Still same one…

I think I tackled it, by simplifying the code, but you need to check if everything still works.

I changed line 600 to this:

if ( isset($user) && isset( $paid_registration_times['level'] ) ) {
    $c[] = (int)floor((strtotime('now') - (int)$paid_registration_times['level']) / 86400);
} else {
    $c[] = 0;
}
define ('S2MEMBER_CURRENT_USER_PAID_REGISTRATION_DAYS', $c);

After doing this, another similar error showed, on line 548.

I changed this to:

if ( isset( $user ) && isset( $paid_registration_times['level'] ) ) {
    $c[] = (int)$paid_registration_times['level'];
} else {
    $c[] = 0;
}
define ('S2MEMBER_CURRENT_USER_PAID_REGISTRATION_TIME', $c);

but again, you might need to test if no functionality is compromised/affected.

Thanks for the update.

So with the one I suggested you still got the error, but with the one you posted later you don’t? Gotcha.

Your code won’t give the correct value to the constant. $c is an array.

Could you also try these?

Line 548

				define ('S2MEMBER_CURRENT_USER_PAID_REGISTRATION_TIME', ($c[] = (($user && !empty((int)$paid_registration_times['level'])) ? (int)$paid_registration_times['level'] : 0)));

Line 600

				define ('S2MEMBER_CURRENT_USER_PAID_REGISTRATION_DAYS', ($c[] = (($user && !empty((int)$paid_registration_times['level'])) ? (int)floor((strtotime('now') - (int)$paid_registration_times['level']) / 86400) : 0)));

Let me know how it goes. :slight_smile:

I understand $c is an array, that’s why I defined them as $c[] which is in array afaik. You use it yourself ?

Your code is still giving undefined index ‘level’ errors.

Line numbers differ because I have my code still (commented) in there.

I think you’re making the statement too hard to understand/debug. KISS.
Keep It Stupid Simple.

This is a var_dump of $c (on line 600) which kind of looks like an array…

array(13) {
  [0]=>
  string(6) "200301"
  [1]=>
  int(45)
  [2]=>
  bool(true)
  [3]=>
  bool(true)
  [4]=>
  int(4)
  [5]=>
  string(15) "Platinum Member"
  [6]=>
  string(0) ""
  [7]=>
  string(1) "1"
  [8]=>
  string(0) ""
  [9]=>
  string(0) ""
  [10]=>
  int(1506195919)
  [11]=>
  int(0)
  [12]=>
  int(0)
}

Yes, I see what you mean, but $c in

define ('S2MEMBER_CURRENT_USER_PAID_REGISTRATION_TIME', $c);

will not be

(int)$paid_registration_times['level']

By the way, I noticed something in my code above. I think that trying to cast $paid_registration_times[‘level’] as an integer if it’s not set, will give a notice too… Better this then…

Line 548

				define ('S2MEMBER_CURRENT_USER_PAID_REGISTRATION_TIME', ($c[] = (($user && isset($paid_registration_times['level']) && !empty((int)$paid_registration_times['level'])) ? (int)$paid_registration_times['level'] : 0)));

Line 600

				define ('S2MEMBER_CURRENT_USER_PAID_REGISTRATION_DAYS', ($c[] = (($user && isset($paid_registration_times['level']) && !empty((int)$paid_registration_times['level'])) ? (int)floor((strtotime('now') - (int)$paid_registration_times['level']) / 86400) : 0)));

:slight_smile:

I know what you mean. I also would have written it more like the one you suggested. I inherited this code and am trying to solve your issue, but without refactoring the thing. So I’m looking at fixing the specific problem in the simplest way possible.

To have the code more like the one you posted, it could be done, but if I change those two lines, why not every line that does something similar or worse. I’d have to modify so much code for something that isn’t broken and risk breaking. My preference goes towards spending that time on new features, for example, as you can understand. :slight_smile:

These last 2 don’t seem to give an error…

because step by step… :slight_smile:

These last 2 don’t seem to give an error…

Great!

because step by step… :slight_smile:

:wink: