skip to Main Content

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; ?>

2

Answers


  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; ?>
    
    Login or Signup to reply.
  2. 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; ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search