0

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...

7
Contributors
26
Replies
29
Views
5 Years
Discussion Span
Last Post by hakeemtunde
0

@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);

0

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.

0

@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.

0

@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

0

@DaveyMoyes

Try to used

$_SERVER['PATH_INFO']

Instead of this:

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

Try this:

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

Edited by LastMitch: grammer

0

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

0

@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

0

@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?

0

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

@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.

Edited by LastMitch: grammer

-1

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..

Votes + Comments
Will Gresham you are rude, I don't know why you down vote me
0

@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?

Edited by LastMitch: grammer

0

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.

0

@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.

0

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.

0

@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.

-1

@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.

Votes + Comments
I'm not sure why you down vote me 3 times?
0

@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.

Edited by LastMitch: grammer

0

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.

Edited by SeanOBrian: I can't spell

0

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.

0

@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

0

@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...

0

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.

0

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

0

@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.

Edited by LastMitch: grammer

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.