So, now about our problem. We want make our application working in all browsers, because it’s good to make it work everywhere. So we have to aks the user agent (basically the client program, not the agent Smith from Matrix), what browser are you using? And we want make it so flexible, that we can ask him this every time. For this purpose we introduce a function:
03 | $ua = $_SERVER [ 'HTTP_USER_AGENT' ] |
04 | if ( strstr ( $ua , 'Opera' )) { |
05 | $browser = ereg_replace ( ".+\(.+\) (Opera |v){0,1}([0-9,\.]+)[^0-9]*" , "Opera\\2" , $ua ); |
06 | if ( ereg ( '^Opera/.*' , $ua )) { |
07 | $browser = ereg_replace ( "Opera/([0-9,\.]+).*" , "Opera \\1" , $ua ); |
09 | } elseif ( strstr ( $ua , 'MSIE' )) { |
10 | $browser = ereg_replace ( ".+\(.+MSIE ([0-9,\.]+).+" , "Internet Explorer \\1" , $ua ); |
11 | } elseif ( strstr ( $ua , 'Firefox' )) { |
12 | $browser = ereg_replace ( ".+\(.+rv:.+\).+Firefox/(.*)" , "Firefox \\1" , $ua ); |
13 | } elseif ( strstr ( $ua , 'Mozilla' )) { |
14 | $browser = ereg_replace ( ".+\(.+rv:([0-9,\.]+).+" , "Mozilla \\1" , $ua ); |
If we want our PHP script to show us the browser, we write:
Now we can see, what kind of browser is used and use it in our aplication:
1 | if (getBrowser() == 'Internet Explorer 6.0' ) { |
2 | echo ( "Please install another Browser" ); |
Thats it!
0 komentar:
Posting Komentar