Development PHP help - browser dependancy

Discussion in 'Software' started by ellism, 2 Jul 2004.

  1. ellism

    ellism What's a Dremel?

    Joined:
    10 Sep 2003
    Posts:
    348
    Likes Received:
    0
    how is php, would i be able to dectect which browser a user is using so that i can change. i have a picture that i want to change depending on the browser, for example the internet explorer users will see a gif and the mozzila etc will see a png. cheers in advance. martin
     
  2. RTT

    RTT #parp

    Joined:
    12 Mar 2001
    Posts:
    14,120
    Likes Received:
    74
    PHP:
      if(($string strstr($_SERVER['HTTP_USER_AGENT'], 'mozilla')) == TRUE) {
        
    $imgext '.png';
      } else {
        
    $imgext '.gif';
      }
     
      echo 
    '<img src="image'.$imgext.'">';
    You could shorten this to:

    PHP:
    $string strstr($_SERVER['HTTP_USER_AGENT'], 'mozilla') ? $imgext '.png' $imgext '.gif'
    the strstr() is untested, i've not checked if it returns true but i'm sure you can work on that :thumb:
     
  3. ellism

    ellism What's a Dremel?

    Joined:
    10 Sep 2003
    Posts:
    348
    Likes Received:
    0
    tnks
     
  4. RTT

    RTT #parp

    Joined:
    12 Mar 2001
    Posts:
    14,120
    Likes Received:
    74
    No probs, i just added an improvement (the 2nd piece of code). You may want to fiddle with the terms (str needle) too as Opera can probably deal with .pngs as well as mozilla does :)
     
  5. ST8

    ST8 What's a Dremel?

    Joined:
    14 Feb 2003
    Posts:
    596
    Likes Received:
    0
    why assign to $string rather than:
    PHP:
    $imgext strstr($_SERVER['HTTP_USER_AGENT'], 'mozilla') ? '.png' :  '.gif'
    ? :)
     
  6. RTT

    RTT #parp

    Joined:
    12 Mar 2001
    Posts:
    14,120
    Likes Received:
    74
    I thought he may wish to work on exactly which browser is being used rather than just 'mozilla' :)
     
  7. ellism

    ellism What's a Dremel?

    Joined:
    10 Sep 2003
    Posts:
    348
    Likes Received:
    0
    Ok, what i have done is this...

    PHP:
      if(STRPOS($_SERVER['HTTP_USER_AGENT'], 'Moz') == 0) { 
        echo 
    '<link rel="stylesheet" type="text/css" href="bluehaze.moz.css" title="Blue Haze stylesheet" />';
        } 
        else 
        { 
        echo 
    '<link rel="stylesheet" type="text/css" href="bluehaze.ie.css" title="Blue Haze stylesheet" />';
      }
    At http://www.ashfordtownsc.co.uk, ps don't laugh.

    it still doesn't seem to be working, ie comes up as mozzila?!?
     
  8. mookie

    mookie very nawty<br><img src="http://mookie.org/i/avatar

    Joined:
    30 Jun 2002
    Posts:
    639
    Likes Received:
    1
    It's because IE returns something like: "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)"

    You might try:
    PHP:
    if(STRPOS($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
        echo 
    '<link rel="stylesheet" type="text/css" href="bluehaze.ie.css" title="Blue Haze stylesheet" />';
    }
    else
    {
        echo 
    '<link rel="stylesheet" type="text/css" href="bluehaze.moz.css" title="Blue Haze stylesheet" />';
    }
     
  9. ellism

    ellism What's a Dremel?

    Joined:
    10 Sep 2003
    Posts:
    348
    Likes Received:
    0
    so i have changed it again,
    PHP:
     <?php
      
    if(STRPOS($_SERVER['HTTP_USER_AGENT'], 'Gecko') == 0) { 
        echo 
    '<link rel="stylesheet" type="text/css" href="bluehaze.moz.css" title="Blue Haze stylesheet" />';
        } 
        else 
        { 
        echo 
    '<link rel="stylesheet" type="text/css" href="bluehaze.ie.css" title="Blue Haze stylesheet" />';
      }
    ?>
    and now it works in reverse!? why, i can use it as it is but i want to know why it is doing this

    edit: am using that last posting of code that was sugested and that works cheers.
     
    Last edited: 3 Jul 2004
  10. OneSeventeen

    OneSeventeen Oooh Shiny!

    Joined:
    3 Apr 2002
    Posts:
    3,454
    Likes Received:
    2
    Just an off-the-topic post, but if you use strpos to search for something that isn't there, it returns a boolean false. Now if you use it to search for something that is there, and is the at the very beginning of the string, it returns a zero, or, a boolean false. Am I correct?

    I always have to do a <?php $foo=strpos(" " . $bar, $baz);?> to use $foo as a boolean true or false...

    (sorry, I figured all the strpos users here might have a better idea as to how to fix this quicker and less memory intensive)
     
  11. ellism

    ellism What's a Dremel?

    Joined:
    10 Sep 2003
    Posts:
    348
    Likes Received:
    0
    does any one know which browsers suport pngs propperly and which don't? :confused:
     
  12. Joel

    Joel What's a Dremel?

    Joined:
    9 May 2002
    Posts:
    386
    Likes Received:
    0
    Mozilla/Gecko based browsers can use PNG's proerly, IE cannot.

    and wouldent it be quicker to use get_browser(); for detecting the browser
     
  13. ellism

    ellism What's a Dremel?

    Joined:
    10 Sep 2003
    Posts:
    348
    Likes Received:
    0
    I don't because i don't know abaout it, thats why i was posting in the first place. how would you use get_browser(); to detect which broswer is using
     
  14. Hwulex

    Hwulex What's a Dremel?

    Joined:
    1 Feb 2002
    Posts:
    4,007
    Likes Received:
    1

Share This Page