いつもお世話になっております。
MacのSafariでアクセスした際だけボタンを非表示にしたく、PHPで閲覧ブラウザやOSを取得するソースを書いているのですがうまくいきません。
アドバイスをいただけますと幸いです。
<?php /** * * UserAgentによる判定 * os * デバイス(PC,Tablet,スマホ) * ブラウザ */ /** ユーザーエージェント取得 */ if ( empty( $user_agent ) ) { $user_agent = $_SERVER[ 'HTTP_USER_AGENT' ]; } /** os判定 */ if ( preg_match( '/Windows NT 10.0/', $user_agent ) ) { $os = 'Windows 10'; } elseif ( preg_match( '/Windows NT 6.3/', $user_agent ) ) { $os = 'Windows 8.1'; } elseif ( preg_match( '/Windows NT 6.2/', $user_agent ) ) { $os = 'Windows 8'; } elseif ( preg_match( '/Windows NT 6.1/', $user_agent ) ) { $os = 'Windows 7'; } elseif ( preg_match( '/Windows NT 6.0/', $user_agent ) ) { $os = 'Windows Vista'; } elseif ( preg_match( '/Windows NT 5.2/', $user_agent ) ) { $os = 'Windows Server 2003 / Windows XP x64 Edition'; } elseif ( preg_match( '/Windows NT 5.1/', $user_agent ) ) { $os = 'Windows XP'; } elseif ( preg_match( '/Windows NT 5.0/', $user_agent ) ) { $os = 'Windows 2000'; } elseif ( preg_match( '/Windows NT 4.0/', $user_agent ) ) { $os = 'Microsoft Windows NT 4.0'; } elseif ( preg_match( '/Mac OS X ([0-9._]+)/', $user_agent, $matches ) ) { $os = 'Macintosh Intel';// . str_replace( '_', '.', $matches[ 1 ] ); バージョンは隠しています } elseif ( preg_match( '/Linux ([a-z0-9_]+)/', $user_agent, $matches ) ) { $os = 'Linux ' . $matches[ 1 ]; } elseif ( preg_match( '/OS ([a-z0-9_]+)/', $user_agent, $matches ) ) { $os = 'iOS ' . str_replace( '_', '.', $matches[ 1 ] ); } elseif ( preg_match( '/Android ([a-z0-9.]+)/', $user_agent, $matches ) ) { $os = 'Android ' . $matches[ 1 ]; } else { $os = '不明'; } /** ブラウザ判定(PC以外はスマホと表示) */ // マイナー if ( preg_match( '/(Iron|Sleipnir|Maxthon|Lunascape|SeaMonkey|Camino|PaleMoon|Waterfox|Cyberfox)/([0-9.]+)/', $user_agent, $matches ) ) { $browser = $matches[ 1 ]; $version = $matches[ 2 ]; // 主要 } elseif ( preg_match( '/Edge/([0-9.]+)/', $user_agent, $matches ) ) { $browser = 'Edge'; $version = $matches[ 2 ]; } elseif ( preg_match( '/(MSIE\s|Trident.*rv:)([0-9.]+)/', $user_agent, $matches ) ) { $browser = 'Internet Explorer'; $version = $matches[ 2 ]; } elseif ( preg_match( '/Chrome/([0-9.]+)/', $user_agent, $matches ) ) { $browser = 'Chrome'; $version = $matches[ 1 ]; } elseif ( preg_match( '/Firefox/([0-9.]+)/', $user_agent, $matches ) ) { $browser = 'Firefox'; $version = $matches[ 1 ]; } elseif ( preg_match( '//([0-9.]+)(\sMobile/[A-Z0-9]{6})?\sSafari/', $user_agent, $matches ) ) { $browser = 'Safari'; $version = $matches[ 1 ]; } elseif ( preg_match( '/(^Opera|OPR).*/([0-9.]+)/', $user_agent, $matches ) ) { $browser = 'Opera'; $version = $matches[ 2 ]; // ゲーム機 } elseif ( preg_match( '/Nintendo (3DS|WiiU)/', $user_agent, $matches ) ) { $browser = 'Nintendo'; $version = $matches[ 1 ]; } elseif ( preg_match( '/PLAYSTATION (3|Vita)/', $user_agent, $matches ) ) { $browser = 'PLAYSTATION'; $version = $matches[ 1 ]; // BOT } elseif ( preg_match( '/(Googlebot|bingbot)/([0-9.]+)/', $user_agent, $matches ) ) { $browser = $matches[ 1 ]; $version = $matches[ 2 ]; } else { $browser = '不明'; $version = ''; } /** デバイス判定 */ if ( preg_match( '/iPhone;/', $user_agent ) ) { $device = 'iPhone'; } elseif ( preg_match( '/iPod/', $user_agent ) ) { $device = 'iPod'; } elseif ( preg_match( '/iPad/', $user_agent ) ) { $device = 'iPad'; } elseif ( preg_match( '/Android/', $user_agent ) ) { $device = 'Android'; } elseif ( preg_match( '/Windows Phone/', $user_agent ) ) { $device = 'Windows Phone'; } elseif ( preg_match( '/(BlackBerry|BB)/', $user_agent ) ) { $device = 'BlackBerry'; } elseif ( preg_match( '/PlayStation Vita/', $user_agent ) ) { $device = 'PlayStation Vita'; } elseif ( preg_match( '/PlayStation Portable/', $user_agent ) ) { $device = 'PlayStation Portable'; } elseif ( preg_match( '/(PS2|PLAYSTATION 3|PlayStation 4)/', $user_agent ) ) { $device = 'PlayStation'; } elseif ( preg_match( '/Nintendo 3DS/', $user_agent ) ) { $device = 'Nintendo 3DS'; } elseif ( preg_match( '/Nintendo (Wii|WiiU)/', $user_agent ) ) { $device = 'Nintendo Wii(U)'; } else { $device = 'PC'; } ?> <!-- ボタン非表示のソースをいくつか書いています --> <?php if ((strpos($browser,'Safari') === false) and (strpos($os,'Macintosh Intel') === false)){ ?> <button>ボタン</button> <?php } ?> <?php if (strpos($browser,'Safari') === false) { ?> <button>ボタン</button> <?php } ?> <?php if (strpos($os,'Macintosh Intel') === false) { ?> <button>ボタン</button> 2</button> <?php } ?> <?php if ($browser == 'Safari' && $os == 'Macintosh Intel') { ?> <?php } else { ?> <button>ボタン</button> <?php } ?> <?php if( ! ($browser == 'Safari' && $os == 'Macintosh Intel') ) {?> <button>ボタン</button> <?php } ?>
何個かif文を記述しております。それぞれの結果は上から順に
1 どのブラウザ、OSでも表示されず
2 1を分解した記述です。Safari以外のブラウザで表示されます
3 1を分解した記述です。Macintosh Intel以外のOSで表示されます
4 どのブラウザ、OSでも表示されず
5 どのブラウザ、OSでも表示されず
表示確認用に判別結果出力のtableを組んでいます。
こちらの結果は特に問題はありませんでした。
<table> <tr> <th>UA</th> <td><?php echo $user_agent; ?></td> </tr> <tr> <th>デバイス</th> <td><?php echo $device; ?></td> </tr> <tr> <th>OS</th> <td><?php echo $os; ?></td> </tr> <tr> <th>ブラウザ</th> <td><?php echo $browser; ?></td> </tr> </table>
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー