-1

I tried searching on the site for "Warning: Cannot modify header information - headers already sent by (output started at /home/zenithda/public_html/www.thanow.com/index.php:12) in /home/zenithda/public_html/www.thanow.com/index.php on line 52.

I looked at other examples but I was not able to find one similar to my problem. I am running a mobile redirect script for the mobile version of my site. I also have a header.php and footer.php running in my includes folder. However, I get the above warning when I try to access my site through my android. Someone tried to check on their iPhone and the same thing occurred. Below the warning, the desktop version of the site displays. In my index.php, here is the code that shows directly above my first command (this forum won't let me display correctly what is before this code so, what's there is the code here "!Doctype Html, aboutthree lines of ! if ie7,8,9 lines, a html head, some meta charset="utf-8" the title and meta descriptions and keywords. ALSO = there are no spaces before the PHP command.)

<?php


  //Load the Mobile Detection library
  include("js/mobiledetect.php");

  //In this simple example, we'll store the alternate home page file names.
  $iphoneTierHomePage = 'mobile/index.html';
  $genericMobileDeviceHomePage = 'mobile/index.html';


  //Instantiate the object to do our testing with.
  $uagent_obj = new uagent_info();


  //This is a common mobile detection scenario...
  // First, detect iPhone tier devices, like iPod Touches, Android, WebOS, etc. 
  //    Send them to the nice touch-optimized page. 
  //    These often have rich CSS and advanced but mobile-friendly JavaScript functionality and no Flash.
  // Second, detect any other mobile device. Send them to the basic mobile pages, with light CSS and no JavaScript.
  //    Some (often older) touch devices might be included in this bunch, which otherwise includes feature phones.
  //    It's a Best Practice to include an alternate web page for less-capable mobile devices. 
  // Finally, assume anything else not caught by these filters is a desktop PC-class device. 
  //    Send them to your regular home page which may include large pictures, lots of JS, Flash, etc.. 
  //
  // NOTE: If you wanted an iPad-class tablet-optimized web site, too, then you should FIRST do a 
  //    device detection using the DetectTierTablet() method. Then detect for iPhone tier, and so on. 


  //In this simple example, we simply re-route depending on which type of device it is.
  //Before we can call the function, we have to define it. 
  function AutoRedirectToProperHomePage()
  {
      global $uagent_obj, $iphoneTierHomePage, $genericMobileDeviceHomePage;

    //We have variables for certain high-usage device variables, like the iPhone Tier.
    //   You might use the device variables to show/hide certain functionality or platform-specific features and ads, etc.
    //   Alternately, you can use the method: DetectTierIphone().
    //   Sometimes, you may wish to include the Tablet Tier devices here, as well. 
      if ($uagent_obj->isTierIphone == $uagent_obj->true) 
      header ('Location: '.$iphoneTierHomePage);

    //We can generally use the Quick Mobile detection method to save a couple of cycles.
      else if ($uagent_obj->DetectMobileQuick() == $uagent_obj->true) 
      header ('Location: '.$genericMobileDeviceHomePage);

    //We'll assume that anything else not caught in the above filters is a desktop-class device. 
    //   (Which can include tablets.)
    }

  //Now, we can call the redirect function.
  AutoRedirectToProperHomePage();
?>

after that is this line:

    <?php include 'includes/header.php' ?>
  • Keep this in mind - "All the output like echo, cookies or session should be state before passing the headers." - This will save your lots of time :) – Hardik Thaker Sep 29 '13 at 18:34

3 Answers3

1

You try to send/modify HTTP headers after other output was already sent to the browser - what doesn't work. You need to modify headers at the beginning of the script or buffer the output before modifying headers. ob_start() starts output buffering, after the code which sends/headers you can send your buffer with ob_end_flush().

http://www.php.net/manual/en/ref.outcontrol.php

edditor
  • 150
  • 1
  • 10
  • I used the ob_start() and end in the php that has the mobile redirect in the index.php. Now it doesn't show the header error, but it doesn't go to the mobile version on my phone at all, it goes to desktop version. I checked all the php includes to get rid of comments and spaces. I also tried it with both php code and with it just on the include header code. – user2828968 Sep 29 '13 at 20:19
0

You can try following steps to find out the issues

  1. Check the includes files ("js/mobiledetect.php"), is there space before opening PHP tag or space after closing PHP tag.
  2. Ensure there is no simple echo or directly you are trying to print any html tags ( without calling any function
  3. Check is there any HTML comment section outside of the function of included files
Shafeeque
  • 2,039
  • 2
  • 13
  • 28
0

Make sure you don't have any html output before header redirection. If you have some output you get that error. I think using meta refresh tag is better for redirection.

ali insan
  • 189
  • 1
  • 5
  • 13