好吧所以我正在尝试开发一个PHP脚本,它将检测设备并将用户重定向到一个站点,具体取决于他们拥有的设备。我遇到麻烦让它完全工作。我希望用户可以通过单击设置名为“sitestyle”的变量的超链接来访问经典站点或移动站点,并且此脚本使用$ _GET ['sitestyle']检查该变量;如果variable = 0则发送到桌面站点。如果variable = 1则发送到移动站点并相应地设置会话。 这是我的代码:
<?php
session_start();
if ((isset($_GET['sitestyle']) && ($_GET['sitestyle']=0)) {
$_SESSION['sitestyle']=0;
}
else ((isset($_GET['sitestyle']) && ($_GET['sitestyle']=1)) {
$_SESSION['sitestyle']=1;
}
//Separate Statements
if ((isset($_SESSION['sitestyle']) && ($_SESSION['sitestyle']=1)) {
header('Location: http://mobi.site.com/');
}
else if ((isset($_SESSION['sitestyle']) && ($_SESSION['sitestyle']=0)) {
exit();
}
else {
$tablet_browser = 0;
$mobile_browser = 0;
if (preg_match('/(tablet|ipad|playbook)|(android(?!.*(mobi|opera mini)))/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
$tablet_browser++;
}
if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android|iemobile)/i ', strtolower($_SERVER['HTTP_USER_AGENT']))) {
$mobile_browser++;
}
if ((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
$mobile_browser++;
}
$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));
$mobile_agents = array(
'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
'newt','noki','palm','pana','pant','phil','play','port','prox',
'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
'wapr','webc','winw','winw','xda ','xda-');
if (in_array($mobile_ua,$mobile_agents)) {
$mobile_browser++;
}
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'opera mini') > 0) {
$mobile_browser++;
//Check for tablets on opera mini alternative headers
$stock_ua = strtolower(isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA'])? $_SERVER['HTTP_X_OPERAMINI_PHONE_UA']:(isset($_SERVER['HTTP_DEVICE_STOCK_UA'])?$_SERVER['HTTP_DEVICE_STOCK_UA']:''));
if (preg_match('/(tablet|ipad|playbook)|(android(?!.*mobile))/i', $stock_ua)) {
$tablet_browser++;
}
}
if ($tablet_browser > 0) {
// do something for tablet devices
$_SESSION['sitestyle']=1;
// print 'is tablet';
}
else if ($mobile_browser > 0) {
// do something for mobile devices
// print 'is mobile';
$_SESSION['sitestyle']=1;
}
else {
// do something for everything else
//print 'is desktop';
$_SESSION['sitestyle']=0;
exit();
}
}
?>
答案 0 :(得分:0)
您的代码为
$_GET['sitestyle']=0
应该是
$_GET['sitestyle'] == 0
以及
$_GET['sitestyle']=1
应该是
$_GET['sitestyle'] == 1
以下是正确的语法
if ( isset($_GET['sitestyle']) && $_GET['sitestyle']==0 ) {
$_SESSION['sitestyle']=0;
}
elseif ( isset($_GET['sitestyle']) && $_GET['sitestyle']==1) {
$_SESSION['sitestyle']=1;
}
答案 1 :(得分:0)
第4行你有一个开口括号太多了!
if ((isset($_GET['sitestyle']) && ($_GET['sitestyle']=0)) {
---^
$_SESSION['sitestyle']=0;
}
移除支架,错误将消失。
请注意,您的页面中会多次出现此错误。请检查if-elseif-else语句中的括号。
另请注意,比较运算符为==
和===
。您正在使用赋值运算符,在这种情况下,这不是您想要的。