Results 1 to 8 of 8

Thread: Help req. with Browser Sniffer Script

  1. #1
    Join Date
    Jul 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help req. with Browser Sniffer Script

    I was trying to implement the Browser Sniffer Script, but I cant get it to work correctly.

    http://pallab.frihost.net/sniff.php

    sniff.php

    Code:
    <?php
    
    include("headercustom.php");
    
    
    
    if (ie) {
    echo("This is for ie ");
    }
    else {
    echo("This is for Opera");
    }
    
    include("footer.php");
    ?>
    headercustom.php

    Code:
    <html>
    <head>
      <title>Sniff</title>
      
    <script type="text/javascript">
    
    //CS1.1
    
    var exclude=1;
    var agt=navigator.userAgent.toLowerCase();
    var win=0;var mac=0;var lin=1;
    if(agt.indexOf('win')!=-1){win=1;lin=0;}
    if(agt.indexOf('mac')!=-1){mac=1;lin=0;}
    var lnx=0;if(lin){lnx=1;}
    var ice=0;
    var ie=0;var ie4=0;var ie5=0;var ie6=0;var com=0;var dcm;
    var op5=0;var op6=0;var op7=0;
    var ns4=0;var ns6=0;var ns7=0;var mz7=0;var kde=0;var saf=0;
    if(typeof navigator.vendor!="undefined" && navigator.vendor=="KDE"){
    	var thisKDE=agt;
    	var splitKDE=thisKDE.split("konqueror/");
    	var aKDE=splitKDE[1].split("; ");
    	var KDEn=parseFloat(aKDE[0]);
    	if(KDEn>=2.2){
    		kde=1;
    		ns6=1;
    		exclude=0;
    		}
    	}
    else if(agt.indexOf('webtv')!=-1){exclude=1;}
    else if(typeof window.opera!="undefined"){
    	exclude=0;
    	if(/opera[\/ ][5]/.test(agt)){op5=1;}
    	if(/opera[\/ ][6]/.test(agt)){op6=1;}
    	if(/opera[\/ ][7-9]/.test(agt)){op7=1;}
    	}
    else if(typeof document.all!="undefined"&&!kde){
    	exclude=0;
    	ie=1;
    	if(typeof document.getElementById!="undefined"){
    		ie5=1;
    		if(agt.indexOf("msie 6")!=-1){
    			ie6=1;
    			dcm=document.compatMode;
    			if(dcm!="BackCompat"){com=1;}
    			}
    		}
    	else{ie4=1;}
    	}
    else if(typeof document.getElementById!="undefined"){
    	exclude=0;
    	if(agt.indexOf("netscape/6")!=-1||agt.indexOf("netscape6")!=-1){ns6=1;}
    	else if(agt.indexOf("netscape/7")!=-1||agt.indexOf("netscape7")!=-1){ns6=1;ns7=1;}
    	else if(agt.indexOf("gecko")!=-1){ns6=1;mz7=1;}
    	if(agt.indexOf("safari")!=-1 || (typeof document.childNodes!="undefined" && typeof document.all=="undefined" && typeof navigator.taintEnabled=="undefined")){mz7=0;ns6=1;saf=1;}
    	}
    else if((agt.indexOf('mozilla')!=-1)&&(parseInt(navigator.appVersion)>=4)){
    	exclude=0;
    	ns4=1;
    	if(typeof navigator.mimeTypes['*']=="undefined"){
    		exclude=1;
    		ns4=0;
    		}
    	}
    if(agt.indexOf('escape')!=-1){exclude=1;ns4=0;}
    if(typeof navigator.__ice_version!="undefined"){exclude=1;ie4=0;}
    
    </script>
    </head>
    <body>
    footer.php
    Code:
    <br />
    This is footer
    
    </body>
    </html>
    What am I doing wrong?
    Please Help.
    Thanks
    Last edited by Indyan; 07-25-2006 at 08:38 AM.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You're serious, aren't you?

    sniff.php is using PHP, but headercustom.php is a Javascript script. The two don't mix like that.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jul 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    You're serious, aren't you?

    sniff.php is using PHP, but headercustom.php is a Javascript script. The two don't mix like that.
    I am a total novice. So I dont have much of an idea about what I am doing.

    I am not sure why you say it should work this way
    I checked the source, after my browser has rendered it. This is what it looks like.

    Code:
    <html>
    <head>
      <title>Sniff</title>
      
    <script type="text/javascript">
    
    //CS1.1
    
    var exclude=1;
    var agt=navigator.userAgent.toLowerCase();
    var win=0;var mac=0;var lin=1;
    if(agt.indexOf('win')!=-1){win=1;lin=0;}
    if(agt.indexOf('mac')!=-1){mac=1;lin=0;}
    var lnx=0;if(lin){lnx=1;}
    var ice=0;
    var ie=0;var ie4=0;var ie5=0;var ie6=0;var com=0;var dcm;
    var op5=0;var op6=0;var op7=0;
    var ns4=0;var ns6=0;var ns7=0;var mz7=0;var kde=0;var saf=0;
    if(typeof navigator.vendor!="undefined" && navigator.vendor=="KDE"){
    	var thisKDE=agt;
    	var splitKDE=thisKDE.split("konqueror/");
    	var aKDE=splitKDE[1].split("; ");
    	var KDEn=parseFloat(aKDE[0]);
    	if(KDEn>=2.2){
    		kde=1;
    		ns6=1;
    		exclude=0;
    		}
    	}
    else if(agt.indexOf('webtv')!=-1){exclude=1;}
    else if(typeof window.opera!="undefined"){
    	exclude=0;
    	if(/opera[\/ ][5]/.test(agt)){op5=1;}
    	if(/opera[\/ ][6]/.test(agt)){op6=1;}
    	if(/opera[\/ ][7-9]/.test(agt)){op7=1;}
    	}
    else if(typeof document.all!="undefined"&&!kde){
    	exclude=0;
    	ie=1;
    	if(typeof document.getElementById!="undefined"){
    		ie5=1;
    		if(agt.indexOf("msie 6")!=-1){
    			ie6=1;
    			dcm=document.compatMode;
    			if(dcm!="BackCompat"){com=1;}
    			}
    		}
    	else{ie4=1;}
    	}
    else if(typeof document.getElementById!="undefined"){
    	exclude=0;
    	if(agt.indexOf("netscape/6")!=-1||agt.indexOf("netscape6")!=-1){ns6=1;}
    	else if(agt.indexOf("netscape/7")!=-1||agt.indexOf("netscape7")!=-1){ns6=1;ns7=1;}
    	else if(agt.indexOf("gecko")!=-1){ns6=1;mz7=1;}
    	if(agt.indexOf("safari")!=-1 || (typeof document.childNodes!="undefined" && typeof document.all=="undefined" && typeof navigator.taintEnabled=="undefined")){mz7=0;ns6=1;saf=1;}
    	}
    else if((agt.indexOf('mozilla')!=-1)&&(parseInt(navigator.appVersion)>=4)){
    	exclude=0;
    	ns4=1;
    	if(typeof navigator.mimeTypes['*']=="undefined"){
    		exclude=1;
    		ns4=0;
    		}
    	}
    if(agt.indexOf('escape')!=-1){exclude=1;ns4=0;}
    if(typeof navigator.__ice_version!="undefined"){exclude=1;ie4=0;}
    
    </script>
    </head>
    <body>This is for ie <br />
    This is footer
    
    </body>
    </html>
    So, it reads it allright.
    I have done it on another thing previously. It did work out.

  4. #4
    Join Date
    Apr 2006
    Posts
    429
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    So, it reads it allright.
    not really tried ur script in FX it still read "This is for ie"
    i think
    Please don't mind me. I am just posting a lot of nonsense.

  5. #5
    Join Date
    Jul 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jr_yeo
    not really tried ur script in FX it still read "This is for ie"
    i think
    I know. Thats why I created this thread.
    What I meant was that, the script is getting loaded properly unlike what Twey suggested (Check the header section).

    Any suggetion as how I should implement this/ why it isnt working?
    I have doubts about the if-else condition I have put in.

  6. #6
    Join Date
    Apr 2006
    Posts
    429
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Any suggetion as how I should implement this
    i wasn't gonna say anything but since u asked
    HTML Code:
    <script type="text/javascript" src="sniffer.js"></script>
    <script>
    if(ie)
    document.write("this is for ie")
    else
    document.write("this is for opera")
    </script>
    i think this should work

    scrap the idea of changing the sniffer script(s) to PHP
    Last edited by jr_yeo; 07-28-2006 at 05:18 AM.
    Please don't mind me. I am just posting a lot of nonsense.

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Both scripts are running fine. But they don't interact, so when you try to call the Javascript function from PHP, nothing happens. The Javascript is just text at the point at which the PHP is being executed.

    The model is thus:
    • PHP is executed. Output of PHP and the unparsed HTML (and Javascript) is sent to the browser.
    • Browser executes Javascript.
    • Browser displays final output of both.
    For example, if we have the code:
    Code:
    <?php
      echo('<script type="text/javascript">document.write("<b>Hello!</b>");</script>');
    ?>
    The server sees:
    Code:
    <?php
      echo('<script type="text/javascript">document.write("<b>Hello!</b>");</script>');
    ?>
    Then the PHP parser is called and the output sent, and the browser sees:
    Code:
    <script type="text/javascript">document.write("<b>Hello!</b>");</script>
    Then the browser parses the Javascript, resulting in the HTML:
    Code:
    <b>Hello!</b>
    Then the browser renders that HTML, and the user sees:
    Code:
    Hello!
    Last edited by Twey; 07-25-2006 at 02:59 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Jul 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks guys.
    I'll do what jr_yeo suggested.

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
  •