[英]PHP redirect based on HTTP_USER_AGENT
I'm trying to redirect my page based on User Agent. 我正在尝试根据用户代理重定向页面。 I've written a piece of code and included it before
<!doctype html>
我已经编写了一段代码,并将其包含在
<!doctype html>
$SAFARI_URL = "https://example.com/ms/";
$OPERA_URL = "https://example.com/ms/";
$OTHER_URL = "https://example.com/";
$CHROME_URL = "https://example.com/";
// Redirection code
$HTTP_USER_AGENT = $_SERVER['HTTP_USER_AGENT'];
function str_present($str,$substr)
{
$pos = strpos($str,$substr);
if($pos === false) {
return false;
}else {
return true;
}
}
if (str_present($HTTP_USER_AGENT, "Safari")){
Header ("Location: " . $SAFARI_URL);
}else if (str_present($HTTP_USER_AGENT, "Opera")){
Header ("Location: " . $OPERA_URL);
}else if (str_present($HTTP_USER_AGENT, "Chrome")){
Header ("Location: " . $CHROME_URL);
}else{
Header ("Location: " . $OTHER_URL);
}
The issue is that it's not working for Chrome. 问题是它不适用于Chrome。 Am I doing something wrong?
难道我做错了什么? Is it not precise enough?
不够精确吗?
you should try this code to detect user's browsers. 您应该尝试使用此代码来检测用户的浏览器。
$SAFARI_URL = "https://example.com/ms/";
$OPERA_URL = "https://example.com/ms/";
$OTHER_URL = "https://example.com/";
$CHROME_URL = "https://example.com/";
$user_agent = $_SERVER['HTTP_USER_AGENT'];
if (strpos($user_agent, 'Opera') || strpos($user_agent, 'OPR/'))
redirect($OPERA_URL );
elseif (strpos($user_agent, 'Edge'))
//redirect($url);
elseif (strpos($user_agent, 'Chrome'))
redirect($CHROME_URL );
elseif (strpos($user_agent, 'Safari'))
redirect($SAFARI_URL );
elseif (strpos($user_agent, 'Firefox'))
//redirect($url);
elseif (strpos($user_agent, 'MSIE') || strpos($user_agent, 'Trident/7'))
//redirect($url); //internet explorer
redirect($OTHER_URL);
}
function redirect($url) {
ob_start();
header('Location: '.$url);
ob_end_flush();
die();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.