-1

Is my syntax for checking multiple strings (existing of one of both strings) in a user agent correct?

I mean the part 'google|lighthouse'. If not, what should be the correct kind of writing?

<?php if (!isset($_SERVER['HTTP_USER_AGENT']) || stripos($_SERVER['HTTP_USER_AGENT'], 'google|lighthouse') === false): ?>
// code
<?php endif; ?>
Evgeniy
  • 2,337
  • 2
  • 28
  • 68

2 Answers2

1

I think you are not doing what you want to do. you are search for string 'google|lighthouse' inside the $_SERVER['HTTP_USER_AGENT'], I guess you want something like

<?php if (!isset($_SERVER['HTTP_USER_AGENT']) || (stripos($_SERVER['HTTP_USER_AGENT'], 'google') === false) || (stripos($_SERVER['HTTP_USER_AGENT'], 'lighthouse') === false): ?>
// code
<?php endif; ?>
IndexError
  • 81
  • 7
1

You need to use regular expression for that, otherwise you are just looking for literal sequence of google|lighthouse:

<?php if (preg_match('/google|lighthouse/', $_SERVER['HTTP_USER_AGENT'] ?? '') !== 1): ?>
// code
<?php endif; ?>
Justinas
  • 41,402
  • 5
  • 66
  • 96