-2

可能重复:
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;
    }

}
4

1 回答 1

2
  • 这些文件的编码类型是什么?如果它们以带有 BOM 的 UTF8编码,则将其转换为不带 BOM 的 UTF8
  • 还要删除标签 <?php前后的所有空格。?>
  • 从浏览器检查 html 页面源代码对空白位置很有帮助。
于 2012-12-15T12:58:24.493 回答