dongqiang8058 2014-04-04 09:36 采纳率: 100%
浏览 47
已采纳

PHP CSS移动响应

I am currently working through the challenge of making a page responsive to a number of mobile platforms this includes support for Desktop Browsers, iPhone, iPad, BB10 and BB7.

As you know this isn't always the easiest task.

I have come to the point where I would like each device to use a different CSS file as this makes life easier with the different screen sizes.

I found this PHP script which enables me to direct users to a different html pages... It's great although I don't feel its efficient enough.

Instead I would like to be able to have this script serve the users different .css files based on the device.

I have very little PHP understanding and would appreciate some help here.


<?php
    // MOBILE
    $android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
    $blackberry = strpos($_SERVER['HTTP_USER_AGENT'],"BB10");
    $blackberry2 = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
    $iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
    $iPad = strpos($_SERVER['HTTP_USER_AGENT'],"iPad");

    // DESKTOP
    $windows = strpos($_SERVER['HTTP_USER_AGENT'],"Windows");
    $mac = strpos($_SERVER['HTTP_USER_AGENT'],"Mac");

    // REDIRECTS 
    // MOBILE
    if ($android == true) 
    { 
    header('Location: mobile.html');
    } 
    else if ($blackberry == true) 
    { 
    header('Location: mobile.html');
    }
    else if ($blackberry2 == true)
    {
    header('Location: mobile.html');
    }
    else if ($iphone == true) 
    { 
    header('Location: mobile.html');
    }
    else if ($ipad == true) 
    { 
    header('Location: mobile.html');
    }
    // DESKTOP
    else if ($windows == true) 
    { 
    header('Location: desktop.html');
    }
    else if ($mac == true) 
    { 
    header('Location: desktop.html');
    }
    ?>

  • 写回答

2条回答 默认 最新

  • doufen5175 2014-04-04 09:42
    关注

    You need to parse your CSS trough the PHP.

    <link rel="stylesheet" href="css.php">
    

    Then in your php:

    <?php
        // MOBILE
        $android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
        $blackberry = strpos($_SERVER['HTTP_USER_AGENT'],"BB10");
        $blackberry2 = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
        $iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
        $iPad = strpos($_SERVER['HTTP_USER_AGENT'],"iPad");
    
        // DESKTOP
        $windows = strpos($_SERVER['HTTP_USER_AGENT'],"Windows");
        $mac = strpos($_SERVER['HTTP_USER_AGENT'],"Mac");
    
        // REDIRECTS 
        // MOBILE
        if ($android == true) 
        { 
        include('android.css');
        } 
        else if ($blackberry == true) 
        { 
        include('blackberry.css');
        }
        // others
    

    But as the guys in comments wrote - this is not the "modern" approach of responsible css.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!