Hi All, I have the following mobile re direct script working on one website, but not another,
For some reason I am getting the error -
Can not modify headers information - header already sent - etc etc
On line 20

Any ideas why this would work for one website and not the other ?
`

<?php

//Config
$url = "http://m.domain.com";

//detection
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);

//Android
if(strstr($ua,'android')) { // &amp;&amp; stripos($ua,'mobile') !== false) {

    header('Location: '.$url);
    exit();

}
//iOS
if(strstr($ua,'iphone') || strstr($ua,'ipod') || strstr($ua,'ipad'));
{
    header('Location: '.$url);
    exit();
}
//BlackBerry
if(strstr($ua,'lackerry') || strstr($ua,'playbook'))
{

    header('Location: '.$url);
    exit();

}
//WindowsPhone
if(strstr($ua,' windows phone os'))
{

    header('Location: '.$url);
    exit();
}

?> 

Thanks in advance...

Recommended Answers

All 26 Replies

Member Avatar for LastMitch

@DaveyMoyes

Can not modify headers information - header already sent - etc etc
On line 20

It means that you are sending the information twice. You can't used this that many times:

header('Location: '.$url);

Hi, thanks for your reply, I have the same file working on a different website I help manage, I have simply copied and pasted the contents and changed the url.

Confused as to why it works on one website and not on the other.

Member Avatar for LastMitch

@DaveyMoyes

Hi, thanks for your reply, I have the same file working on a different website I help manage, I have simply copied and pasted the contents and changed the url.

Confused as to why it works on one website and not on the other.

I think you put the semi-colon ; on the end of this statement:

This is your old code:

if(strstr($ua,'iphone') || strstr($ua,'ipod') || strstr($ua,'ipad'));

This is your new code:

if(strstr($ua,'iphone') || strstr($ua,'ipod') || strstr($ua,'ipad'))

Try it now it should work now.

@LastMitch - Thanks buddy, I have tried that, but still getting the same error -
Its driving me nuts,
I have even changed the header information to
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
To see if that was the cause, but nope, still the same errors

Member Avatar for LastMitch

@DaveyMoyes

Try to used

$_SERVER['PATH_INFO']

Instead of this:

$ua = strtolower($_SERVER['HTTP_USER_AGENT']);

Try this:

$ua = strtolower($_SERVER['PATH_INFO']);

@LastMitch - Hi, Just tried that but just get a blank screen now, no errors, but It doesnt redirect to the mobile website....

Member Avatar for LastMitch

@DaveyMoyes

Hi, Just tried that but just get a blank screen now, no errors, but It doesnt redirect to the mobile website....

It means it pass your variables but not redirecting to the page

Member Avatar for LastMitch

@DaveyMoyes

For your $url

Change this:

$url = "http://m.domain.com";

to this:

$url = "https://". $_SERVER['SERVER_NAME'] . ":" . $sslport . $_SERVER['REQUEST_URI'];

For your header from this:

header('Location: '.$url);

to this:

header("Location: $url");

Tell me what you get?

@LastMitch - Just made those changes - see below -
The desktop webiste loads on my mobile device, but it still does not redirect to the mobile optimized website.

<?php
//Config
//$url = "http://m.website.com";
$url = "https://". $_SERVER['SERVER_NAME'] . ":" . $sslport . $_SERVER['REQUEST_URI'];
//detection
//$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
$ua = strtolower($_SERVER['PATH_INFO']);
//Android
if(strstr($ua,'android')) { // &amp;&amp; stripos($ua,'mobile') !== false) {
    header("Location: $url");
    exit();
}
//iOS
if(strstr($ua,'iphone') || strstr($ua,'ipod') || strstr($ua,'ipad'))
{
    header("Location: $url");
    exit();
}
//BlackBerry
if(strstr($ua,'lackerry') || strstr($ua,'playbook'))
{
    header("Location: $url");
    exit();
}
//WindowsPhone
if(strstr($ua,'windows phone os'))
{
    header("Location: $url");
    exit();
} 
?>
Member Avatar for LastMitch

@DaveyMoyes

I'm not sure what is wrong is your website because you mention to me it does work in other websites? I mean there is lot of changes were made and it still doesn't work! It seems to me either you are doing something wrong or you are not telling me the truth whether you have used this code before.

Read this

http://www.9lessons.info/2010/09/redirect-mobile-devices-with-php.html

and follow the instructions and most likely it will work.

So much wrong information here, if I were you I'd disregard all the things LastMitch has posted.
1. You can use multiple header() calls.
2. $_SERVER['PATH_INFO'] will not give you the UserAgent, it's to do with the query string.
3. All changing the URL to what LastMitch suggested will do is make the page redirect ho HTTPS
4. There is no reason to use concatenation over a double quoted variable

The problem you are having is caused by some output occuring before the header() function is called, this could be whitespace from files that include this one, an echo, print, etc..

commented: Will Gresham you are rude, I don't know why you down vote me -1
Member Avatar for LastMitch

@Will Gresham

So much wrong information here, if I were you I'd disregard all the things LastMitch has posted.

You don't need to be that rude. The issue is that he used this code before and on the other website it work and now it doesn't so I feel he's not telling the truth. I mean do you have a better solution without changing anything?

The issue is that the error is caused by output being sent to browser, including the default headers, and then the script tries to send more headers.

I would guess that this file gets included, rather than visited directly, and the files that include this one are sending output before the include/require call is being made.

Member Avatar for LastMitch

@Will Gresham

I would guess that this file gets included, rather than visited directly, and the files that include this one are sending output before the include/require call is being made.

It seems you don't know either. You're just guessing.

No, I know what is causing the issue, what I don't know is the environment and when the script is loaded.

What I am guessing is that the file gets included from every page on the website, rather than a user going directly to it, which based on the functionality the script is supposed to take is a reasonable assumption.

Member Avatar for LastMitch

@Will Gresham

No, I know what is causing the issue, what I don't know is the environment and when the script is loaded.

Well, I'll let you answer this question. It seems to me know the answer. So Finish the thread!

@DaveyMoyes

Will Gresham will kindly help you answer this question for you.

@LastMitch
Firstly, Why would I lie about the script working on a different website ? I have better things to do with my time than lie... Maybe you know your own tricks best....

@Will Gresham - thanks for jumping in here mate, you are right in saying the file is included ie.

<?php include("MobileDetection.php"); ?>

The file is included at the top of the page - I have even tried using

<?php ob_start(); ?>

<? ob_flush(); ?>

at the top and bottom of the page, but still getting the same error.
I have been back through the mobile detection .php file to make sure there is no white space.

commented: I'm not sure why you down vote me 3 times? -1
Member Avatar for LastMitch

@Will Gresham
@DaveyMoyes

I'm not quite sure why that much down vote on my comment. It's rude so cut it out before I flag this post. Maybe 1 down voted I can understand but 3 down voted that is intentionally. I don't like that please don't do that.

You've probably checked this out but onething I alwasy scheck when I have this error is whether there are any unseen unicode chars at the top of the file. In Win OSs Phred is a good option.

Your include statement needs to come before pretty much anything else, there should not be any whitespace before any opening php tags, or any after the closing tags. This applies both to the detection script and the file(s) it is included from.
Make sure there are no echo statements or anything that would cause the headers to be send in the files that include the detection script too.

@LastMitch, the reason for the down vote is due to YOU calling me a liar,
That is something I am NOT,

@SeanOBrian, thanks bud, I will double check.
Still scratching my head here tho

@Will, I checked, double checked and then checked again.

I even tried the solution the other (rude) poster had suggested, same problem,
I will have another look over what is going on, the problem is def what you are calling by the script sending out double headers... I just cant stop it...

anything that would cause the headers to be send

Note that this can be caused by a BOM marker generated by your text editor if the file is in UTF/Unicode.

Possibly PHP is configured differently or is a different version on the two websites.

In any case, that "Can not modify headers information - header already sent" stuff is very sensitive. Even a blank line in your source can result in a single carriage return being sent to the browser, which would result in this problem.

I've encountered this a number of times, and each time it was a space or new line in my source code that was sending output to the browser that I wasn't expecting.

View source for the page in your browser and see if even an invisible character has been sent.

HTH

Member Avatar for LastMitch

@DaveyMoyes

@LastMitch, the reason for the down vote is due to YOU calling me a liar,
That is something I am NOT,

I never accused you being a liar. I said I feel you are not telling me the truth whether you have used this code before. There was a lot of changes plus now more changes. I mean if it works on your other websites then there shouldn't be any issue at all?

Regarding about down vote. If you don't like me is one thing but you can't down vote like that it's really rude and un-called for. I don't like to down vote anyone unless that person is being rude and not reasonable. I usually look away when someone down vote me 1 time but 3 times in 1 thread that's intentionally!

Can you refrain yourself of not voting down and taking shots at me now?

I'm not helping you anymore but I'm still waiting for an correct answer for your solution.

I would have suggest to you to get a more way of detecting device because using that set of method to detect device might not work out properly for your conditions. take a look at WURFL Tera-WURFL it might help in detecting actual UA. hope it helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.