Project / Support Center
Welcome, %1$s. Please login or register. February 28, 2024, 11:32: AM
Web Global Net Web Application & Web Development Project Center  |  Technical Issues  |  Mobile Devices  |  : Easy way to detect user devices php if then else 0 and 1 Guest are viewing this topic. « previous next »
: [1]
: Easy way to detect user devices php if then else  ( 7519 )
admin
Administrator
******


: 239


« : October 04, 2016, 07:22: PM »

This I think is the easiest way to detect what type of device is being used by a visitor to your site - we used it to display the correct Map app icon but it can be modified easily to fit your needs. this is a If then else statement.

<?php
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Mac OS') !== false) {  
?>


the strpos searches the HTTP_USER_AGENT to determine what device is being used for the Filter you supply 'Mac OS' in this case.

a typical HTTP_USER_AGENT if echoed to your screen would look like this :

Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4

by using the strpos it can search the entire HTTP_USER_AGENT string for what ever filter word you want.

Our examples below looked for Apple, Android & Windows to display the correct Map App icon and link


:
 
Macintosh Filter

<?php
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Mac OS') !== false) {  
?>


------- display what ever you want based on the filter match here ------------
 <a href="maps://?q=<?php echo $row_rsclass_events['event_address']; ?> <?php echo $row_rsclass_events['event_city']; ?>, <?php echo $row_rsclass_events['event_zip']; ?><img src="../../assets/2016/ios-maps-icon.jpg" width="72" height="72" hspace="0" vspace="0" align="middle" /></a>
------- end display area  --------------------------------------

<?php
}
?>


Android Filter

<?php
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Andriod') !== false) {
?>


<a href="geo://0,0?q=<?php echo $row_rsclass_events['event_address']; ?> <?php echo $row_rsclass_events['event_city']; ?>, <?php echo $row_rsclass_events['event_st']; ?> <?php echo $row_rsclass_events['event_zip']; ?>  <img src="../assets/2016/Maps-Android-R.png" width="72" height="72" hspace="5" vspace="5" /></a>

<?php
}
?>


Windows Filter

<?php
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Windows') !== false) {
?>


<a href="http://maps.google.com/?q=<?php echo $row_rsclass_events['event_address']; ?> <?php echo $row_rsclass_events['event_city']; ?>, <?php echo $row_rsclass_events['event_st']; ?> <?php echo $row_rsclass_events['event_zip']; ?>  " target="_blank"><img src="glovebox/code/images/map0-with-pin.png" width="72" height="72" hspace="5" vspace="5" /></a>

<?php
}
?>
« : October 04, 2016, 07:33: PM admin »  

The Unknown Webmaster
: [1]  
Web Global Net Web Application & Web Development Project Center  |  Technical Issues  |  Mobile Devices  |  : Easy way to detect user devices php if then else « previous next »
:  


Powered by MySQL Powered by PHP Valid XHTML 1.0! Valid CSS!
Sorry, the copyright must be in the template.
Please notify this forum's administrator that this site is missing the copyright message for SMF so they can rectify the situation. Display of copyright is a legal requirement. For more information on this please visit the Simple Machines website.