PHP判断手机还是电脑访问

2020-04-03 10:39:30 / 0 评论 / 3516 阅读 / 正在检测是否收录...

做网站的时候总会遇到各种各样的问题,其中一个问题是电脑和手机同时访问到底显示什么?有些效果在电脑上看挺漂亮的,但是到了手机上反而是个累赘,所以说呢我们就需要一个判断,当他电脑的时候显示什么手机的时候显示什么。下面这张代码是我从其他网站复制过来的,自己记录一下,往后可能会用得上

<?php //判断手机参数
function sywap(){ 
$user_agent = $_SERVER['HTTP_USER_AGENT']; 
$mobile_agents = array("240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","android","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio", "au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu","cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ","fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi","htc","huawei","hutchison","inno","ipad","ipaq","iphone","ipod","jbrowser","kddi","kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo","mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia","nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-","playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo","samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank","sony","spice","sprint","spv","symbian","tablet","talkabout","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wap","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte"); 
$sywap = false;foreach($mobile_agents as $device){if(stristr($user_agent, $device)) {$sywap = true;break;}}return $sywap;}?>

在需要放置的地方用如下代码:

<?php if(sywap()){?>
手机显示
<?php }else{?>电脑显示
<?php }?>

评论

取消