Search code examples
phpcssmobilestylesheetalternate

Is there a way to load an alternate stylesheet using PHP?


I'm building a mobile version of my site, and at this point, creating an alternate stylesheet would suffice to make the site adept for mobile devices. I want to use a user agent detection PHP script to detect the platform, and switch stylesheets accordingly. Is there any way to do that?


Solution

  • You might want to try out a librabry to check if its a mobile device then you can try out this library: http://code.google.com/p/php-mobile-detect/ (this also supports detecting specific OSes)

    and then have code like

    if($detect->isiOS()){
        echo <link rel="stylesheet" type="text/css" href="iOSstyle.css" />;
    }
    else if ($detect->isMobile()) {
       echo <link rel="stylesheet" type="text/css" href="mobile.css" />;
    }
    else{
      echo <link rel="stylesheet" type="text/css" href="normal.css" />;
    }
    

    FYI, these libraries are dependent on the User-Agent value in the header and other headers too.