ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Mobile detection -- can't do a PHP Header() ??

February 11, 2009 8:09pm

Subscribe [9]
  • #1 / Feb 11, 2009 8:09pm

    kelseyads2

    98 posts

    I’m working on a mobile version of our website and we have PHP functions that check the browser information, which are working great…

    but when I try to do a redirect inside a EE template it just does nothing. I’ve got it set to parse PHP on Input but it still won’t redirect.

    I’ve temporary got it working by outputting a META Refresh tag, but it loads the complete site before redirecting (NOT ideal)

    Is there a way with an EE or PHP tag to redirect server-side?

  • #2 / Feb 11, 2009 9:33pm

    Lisa Wess

    20502 posts

    Are you sure you’re exiting?  Impossible really to say more without seeing more code.

    There is always, of course, the redirect= global variable.

  • #3 / Feb 12, 2009 9:48am

    kelseyads2

    98 posts

    I tried the redirect= and it didn’t work. When I put the header(Location: ) it just does nothing, similar to what happens if it throws a PHP error. The rest of the page loads, but it doesn’t do anything.

    Here’s the code:

    <?php
    require("/home/***/***/classes/detectmobile.php");
    
    if ( checkmobile() || checkiphone() ) {
    $mobile = '<meta http-equiv="refresh" content="0;URL=http://www.kelseyads.com/site.php/mobile/">';
    }
    else {
    $mobile = "";
    }
    ?>

    Instead of setting the $mobile variable, I was trying to do a php header();

    This is the index template and this code is at the very top. There is no output in the detectmobile.php script so unless EE is outputting headers, there are no headers already output by my scripts.

  • #4 / Feb 17, 2009 11:09am

    kelseyads2

    98 posts

    Anyone?

    Is it possible to get a PHP redirect to work inside an EE template?

  • #5 / Feb 17, 2009 11:34am

    Derek Jones

    7561 posts

    Reduce it, beley.  Just a new blank template with the EE redirect variable on it.  Does it work for you?  If not, what version of ExpressionEngine are you on, and what server environment?  Just a note that when you do a location redirect with PHP, you need to exit your script, and any whitespace in the PHP file you’re including could be starting output.  If you are getting PHP errors, please share them.

  • #6 / Feb 17, 2009 11:54am

    kelseyads2

    98 posts

    No. I created a blank web template with only a {redirect} tag and it did nothing (it actually output the tag, so it didn’t even try to parse it).

    I also edited the template to ONLY have a PHP redirect, and it also didn’t work.

    We are running EE 1.6.4
    No errors output.

  • #7 / Feb 17, 2009 11:55am

    Derek Jones

    7561 posts

    That feature was added in 1.6.5.  I’d strongly suggest an update, particularly since some between the version you’re using and the current were security related.

    I also edited the template to ONLY have a PHP redirect, and it also didn’t work.

    Share your code, please?  It’s difficult for people to help when they can only assume what you’ve tried.

  • #8 / Feb 17, 2009 11:57am

    kelseyads2

    98 posts

    LOL, go figure… updating now. Thanks…

  • #9 / Feb 17, 2009 11:59am

    Derek Jones

    7561 posts

    Let me know how it goes, and if you want a quick second eye on your PHP redirect, again, please share the reduced code you tried on a template by itself.

  • #10 / Feb 17, 2009 11:59am

    kelseyads2

    98 posts

    That feature was added in 1.6.5.  I’d strongly suggest an update, particularly since some between the version you’re using and the current were security related.

    I also edited the template to ONLY have a PHP redirect, and it also didn’t work.

    Share your code, please?  It’s difficult for people to help when they can only assume what you’ve tried.

    PHP header redirect is pretty simple… the entire template consisted of one line:

    <?php header('Location: <a href="http://www.google.com">http://www.google.com'</a>); ?>

    Same with the EE redirect:

    {redirect=site/index}

    Of course, the EE one didn’t work because of the version apparently. Not sure why the PHP redirect didn’t work, unless headers are output before the template is parsed by EE.

  • #11 / Feb 17, 2009 12:00pm

    kelseyads2

    98 posts

    Let me know how it goes, and if you want a quick second eye on your PHP redirect, again, please share the reduced code you tried on a template by itself.

    Thanks, after I get the site updated I’ll try again and let you know how it worked out.

    Thanks for your help!

  • #12 / Feb 17, 2009 12:02pm

    Derek Jones

    7561 posts

    PHP header redirect is pretty simple… the entire template consisted of one line:

    <?php header('Location: <a href="http://www.google.com">http://www.google.com'</a>); ?>

    Same with the EE redirect:

    {redirect=site/index}

    Of course, the EE one didn’t work because of the version apparently. Not sure why the PHP redirect didn’t work, unless headers are output before the template is parsed by EE.

    You’ll need to exit the script as mentioned after a PHP header() location redirect, otherwise EE will keep chugging along, and eventually send its own headers when it’s time to send output back to the server.

    <?php header('Location: <a href="http://www.google.com">http://www.google.com'</a>); exit; ?>
  • #13 / Feb 17, 2009 12:08pm

    kelseyads2

    98 posts

    Duh… didn’t really process the “you need to exit your script.” That worked… still upgrading but that will be a lot faster than a meta refresh so I’ve gone ahead and made the change to the template.

    Thanks for the help… really appreciated!

    BTW, it works great now… check it out: http://www.kelseyads.com

    Anyone else powering a mobile site using EE?

  • #14 / Feb 17, 2009 12:09pm

    kelseyads2

    98 posts

    (should go w/out saying but you need to visit on a mobile device like iPhone, blackberry or cell phone)

  • #15 / Feb 17, 2009 12:11pm

    Derek Jones

    7561 posts

    Glad to help Kelsey Brandon - I’ll check it out next time I’m browsing on my iPhone.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases