Set css with php according to browser

3

Earlier today I did this question.

What I need to know is how I can do something like this:

  • Check whether the browser is Safari or not.

  • If it is, section#internal.services aside ul li a should receive this css:

CSS applied:

color:#666666;
display:block;
font:normal normal bold 15px/normal Questrial, Arial;
height:70px;
line-height:4em;

What would be the best way to do this, with php?

    
asked by anonymous 17.07.2015 / 20:50

2 answers

2

You can get the userAgent, and check the browser, and based on that, you can then define your css.

Follow the example:

 if (preg_match('|MSIE ([0-9].[0-9]{1,2})|',$useragent,$matched)) {
    $css = "border: solid 5px blue;";
    $browser = 'IE';
  } elseif (preg_match( '|Opera/([0-9].[0-9]{1,2})|',$useragent,$matched)) {
    $css = "border: solid 5px blue;";
    $browser = 'Opera';
  } elseif(preg_match('|Firefox/([0-9\.]+)|',$useragent,$matched)) {
    $css = "border: solid 5px red;";
    $browser = 'Firefox';
  } elseif(preg_match('|Chrome/([0-9\.]+)|',$useragent,$matched)) {
    $css = "border: solid 5px blue;";
    $browser = 'Chrome';
  } elseif(preg_match('|Safari/([0-9\.]+)|',$useragent,$matched)) {
    $css = "border: solid 5px red;";
    $browser = 'Safari';
  } else {
    $css = "border: solid 5px red;";
    $browser= 'other';
  }

Use as follows:

No $css I include CSS, and then you can put:

<style> 
     section#internal.services aside ul li a { 
       <?php echo $css; ?>
     }
</style>

This will import the css correctly.

    
17.07.2015 / 20:56
3

You can check the user agent like this:

  <?php
    $useragent = $_SERVER['HTTP_USER_AGENT'];

  if (preg_match('|MSIE ([0-9].[0-9]{1,2})|',$useragent,$matched)) {
    $browser_version=$matched[1];
    $browser = 'IE';
  } elseif (preg_match( '|Opera/([0-9].[0-9]{1,2})|',$useragent,$matched)) {
    $browser_version=$matched[1];
    $browser = 'Opera';
  } elseif(preg_match('|Firefox/([0-9\.]+)|',$useragent,$matched)) {
    $browser_version=$matched[1];
    $browser = 'Firefox';
  } elseif(preg_match('|Chrome/([0-9\.]+)|',$useragent,$matched)) {
    $browser_version=$matched[1];
    $browser = 'Chrome';
  } elseif(preg_match('|Safari/([0-9\.]+)|',$useragent,$matched)) {
    $browser_version=$matched[1];
    $browser = 'Safari';
  } else {
    $browser_version = 0;
    $browser= 'other';
  }
?>

<html>
    <head>
        <title></title>
        /*<?php 
            if($browser == 'Safari')
                echo '<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">';
            else
                echo '<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>';?>*/</head><body><?phpif($browser=='Safari'){echo'<spanid="#internal"></span>';
              }else{
                  echo '<span></span>';
              }
         ?>
    </body>
</html>

As a desire of the AP, the id specific to the element was applied if the browser is the safari. It is necessary that the chip that will be applied to the element when in safari is in css.

Addendum: It is still possible with this Super Global $_SERVER['HTTP_USER_AGENT'] to identify which SO

    
17.07.2015 / 20:57