移动检测:“警告:无法修改标头信息”

时间:2012-12-15 12:44:52

标签: php mobile browser-detection

  

可能重复:
  Headers already sent by PHP

我继承了一个运行移动检测php脚本的网站,如果用户通过移动设备访问该网站,该网站应该将用户重定向到子域。

该网站在我的桌面上运行良好,但在我的iphone上我得到以下内容:

  

“警告:无法修改标题信息 - 已在第16行的/home/autolox/public_html/common.php中发送的输出(/home/autolox/public_html/index.php:1中的输出)

     

警告:无法修改标题信息 - 已在第17行的/home/autolox/public_html/common.php中发送的输出(/home/autolox/public_html/index.php:1中的输出)

我对PHP并不熟悉,所以我不知道这意味着什么,但这里是我的common.php文件:

<?php
require_once(realpath(implode(DIRECTORY_SEPARATOR, array(dirname(__FILE__), 'MobileDetect.php'))));
$MobileDetect = new MobileDetect();
if($MobileDetect->IsMobile()){
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: http://mobi.autolox.co.uk/");
    exit();
}

这是我的索引文件的顶部:

<?php require_once(realpath(implode(DIRECTORY_SEPARATOR, array(dirname(__FILE__), 'common.php'))));?>
<!DOCTYPE html>
<html lang="en">

在这方面会有所帮助,我很难过!

编辑:

还有一个移动检测php文件正在运行,见下文:

/**
 * Detect mobile devices
 * @idilico
 * @version 2.7.0
 * @copyright Copyright (c) 2010-2011 Idilico (http://www.idilico.com)
 */
class MobileDetect {

    // Device constants
    const DEVICE_ANDROID    = "android";
    const DEVICE_BLACKBERRY = "blackberry";
    const DEVICE_IPHONE     = "iphone";
    const DEVICE_IPHONE4    = "iphone4";
    const DEVICE_OPERA      = "opera";
    const DEVICE_PALM       = "palm";
    const DEVICE_WINDOWS    = "windows";
    const DEVICE_GENERIC    = "generic";
    const DEVICE_IPAD       = "ipad";
    const DEVICE_NORMAL     = "normal";

    /**
     * Hold the device useragent
     * 
     * @var string
     */
    private $_useragent;

    /**
     * Boolean that is set to true if 
     * the current device is mobile
     * 
     * @var bool
     */
    private $_isMobile      = false;

    /**
     * Device booleans that get set when 
     * the devices matche
     * 
     * @var bool
     */
    private $_isAndroid     = null;
    private $_isBlackberry  = null;
    private $_isIphone      = null;
    private $_isIphone4     = null;
    private $_isOpera       = null;
    private $_isPalm        = null;
    private $_isWindows     = null;
    private $_isGeneric     = null;
    private $_isIpad        = null;

    /**
     * Regular expressions for the different devices
     * 
     * @var array
     */
    private $_devices       = array(
        "android"       => "android",
        "blackberry"    => "blackberry",
        "ipad"          => "ipad",
        "iphone4"        => "(8A293|4_3)",
        "iphone"        => "(iphone|ipod)",
        "opera"         => "(opera mini|opera mobi)",
        "palm"          => "(avantgo|blazer|elaine|hiptop|palm|plucker|xiino|webos)",
        "windows"       => "(iemobile|smartphone|windows phone|htc_hd2)",
        "generic"       => "(kindle|mobile|mmp|midp|o2|pda|pocket|psp|symbian|smartphone|treo|up.browser|up.link|vodafone|wap|u970)"
    );

    /**
     * Constructor
     *
     * @access public
     * @return void
     */
    public function __construct() {
        $this->_useragent = $_SERVER['HTTP_USER_AGENT'];
        foreach ($this->_devices as $device => $regexp) {
            if ($this->IsDevice($device) && $this->_isMobile == FALSE) {
                $this->_isMobile = true;
            }
        }
    }

    /**
     * Check if surfing with a particular device
     *
     * @access private
     * @param string $device
     * @return bool
     */
    private function IsDevice($device) {

        $var    = "_is" . ucfirst($device);
        $this->$var = @$this->$var === null ? (bool) preg_match("/" . $this->_devices[strtolower($device)] . "/i", $this->_useragent) : $this->$var;
        if ($device != 'generic' && $this->$var == true) {
            $this->_isGeneric = false;
        }

        return $this->$var;
    }

    /**
     * Get the device type 
     * 
     * @param public
     * @return string
     */
    public function GetDevice(){
        foreach($this->_devices as $device_string => $regex){
            if( $this->IsDevice($device_string) ){
                return $device_string;
            }
        }
        return self::DEVICE_NORMAL;
    }

    /**
     * Call methods like this IsMobile() | IsAndroid() | IsIphone() | IsBlackberry() | IsOpera() | IsPalm() | IsWindows() | IsGeneric() | IsIpad() through IsDevice()
     *
     * @access public
     * @param string $name
     * @param array $arguments
     * @return bool
     */
    public function __call($name, $arguments) {
        $device = substr($name, 2);
        if ($name == "Is" . ucfirst($device)) {
            return $this->IsDevice($device);
        } else {
            trigger_error("Method $name is not defined", E_USER_ERROR);
        }
    }


    /**
     * Returns true if surfing on a mobile device
     *
     * @access public
     * @return bool
     */
    public function IsMobile() {
        return $this->_isMobile;
    }

}

1 个答案:

答案 0 :(得分:2)

  • 这些文件的编码类型是什么? 如果它们以带有BOM 的 UTF8进行编码,请将其转换为无BOM的UTF8
  • 同时删除<?php之前和?>标记之后的所有空格。
  • 从浏览器中检查html页面源可能对空白区域有帮助。