SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Redirection based on browswer

    I'm new to PHP and I am trying to get a redirection to work based on if the user has Firefox or Internet Explorer. So far I have:

    <?php

    $browser = get_browser( null, true )
    if( $browser['browser'] == 'Firefox' )
    {
    header('location: ff.htm');
    }
    else
    {
    header('location: ie.htm');

    ?>

    But upon loading it, the redirection won't happen. The host I have uses .php4, don't know if it makes a difference. Any help is greatly appreciated!

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Location
    The Netherlands
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you need another semicolon, and another '}'. Like this:

    <?php
    $browser = get_browser( null, true );
    if( $browser['browser'] == 'Firefox' )
    {
    header('location: ff.htm');
    }
    else
    {
    header('location: ie.htm');
    }
    ?>
    There is no signature...

  3. #3
    Dinah-Moe Humm mudshark's Avatar
    Join Date
    Dec 2003
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What does print_r($browser) tell you?
    Are you suppressing php warnings? I just did a test locally, and got this: "Warning: get_browser(): browscap ini directive not set."

  4. #4
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, that code works now, except it doesn't redirect based on broswer properly. It automatically fails the if statement and goes to else. I'm assuming it's a browser name problem. I tried getting browser info and inputing various things for browser name, but didn't have any luck. What should Firefox and MSIE be called so they will be detected properly?

  5. #5
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not echo it as a test and see what it's set to?
    echo $browser['browser'];
    exit;

  6. #6
    SitePoint Zealot
    Join Date
    May 2004
    Location
    Oxford, UK
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've never really liked or trusted the get_browser() function. Normally I'd use something like this instead:

    PHP Code:
    if ((isset($_SERVER['HTTP_USER_AGENT'])) && (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== false)) {
        
    // The user has Firefox


  7. #7
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have this now:


    <?php
    if ((isset($_SERVER['HTTP_USER_AGENT'])) && (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== false)) {
    // The user has Firefox
    {
    header('location: ff.htm');
    }
    else
    {
    header('location: ie.htm');
    }
    ?>
    But that won't work, it just loads my index file as a blank white page, not the file it's suppose to lead to.

  8. #8
    SitePoint Guru defiance's Avatar
    Join Date
    Oct 2004
    Location
    United states
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this

    <?php
    if ((isset($_SERVER['HTTP_USER_AGENT'])) && (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== false)) {
    // The user has Firefox

    header('location: ff.htm');
    }
    else
    {
    header('location: ie.htm');
    }
    ?>

  9. #9
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wooo! It works now, thank you very much!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •