-2

Possible Duplicate:
Headers already sent by PHP

I have inherited a website that has a mobile detect php script running that should redirect the user to a subdomain if the user is accessing the website via a mobile device.

The website works fine on my desktop, but on my iphone I get the following:

"Warning: Cannot modify header information - headers already sent by (output started at /home/autolox/public_html/index.php:1) in /home/autolox/public_html/common.php on line 16

Warning: Cannot modify header information - headers already sent by (output started at /home/autolox/public_html/index.php:1) in /home/autolox/public_html/common.php on line 17"

I'm not exactly experienced with PHP, so I've no idea what this means, but here is my common.php file in full:

<?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();
}

and this is the top of my index file:

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

Would love some help on this, I'm stumped!

EDIT:

There is also a mobile detect php file running, see below:

/**
 * 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;
    }

}
Community
  • 1
  • 1
tjcss
  • 856
  • 1
  • 12
  • 33
  • Make sure in common.php that there are not empty lines or spaces before at the end of each of those files. They are not needed and only increase the likelihood that an empty space can creep in. – kittycat Dec 15 '12 at 12:55
  • http://stackoverflow.com/questions/9707693/warning-cannot-modify-header-information-headers-already-sent-by-error – Mudassir Hasan Dec 15 '12 at 12:57
  • When I visit the site I don't get any errors. – kittycat Dec 15 '12 at 13:11
  • Have you tried visiting the site from a mobile? I'm using an iphone 4s – tjcss Dec 15 '12 at 13:14
  • You checked index.php as well to make sure it has no empty spaces before – kittycat Dec 15 '12 at 13:21
  • Yep absolutely, there is no whitespace - this script worked fine on the previous webhost. I'm simply moving these files for a client. – tjcss Dec 15 '12 at 13:26
  • Are the host operating systems different? – kittycat Dec 15 '12 at 13:30
  • Probably, I was not given access to the previous host - I was merely sent the files. The previous host will not give me access. – tjcss Dec 15 '12 at 13:41

1 Answers1

2
  • What are the encoding types of these files? If they are encoded in UTF8 with BOM, convert it to UTF8 without BOM.
  • Also remove all whitespaces before <?php and after ?> tags.
  • Checking html page source from the browser can be helpful for whitespace places.
trante
  • 33,518
  • 47
  • 192
  • 272