有没有更好的方式来处理cookie /重定向
问题描述:
我已经为我们组建了一个移动网站,并且将主网站madisonstudios.com重定向到移动网站madisonstudios.mobi,如果该设备是移动设备设备。有没有更好的方式来处理cookie /重定向
我还在移动网站上放了一个完整的网站按钮,当引用者是移动网站时,它设置了一个cookie,但是我仍然遇到了问题,仍然会在第一次点击时将其重定向到完整网站,那么一旦你点击它,第二次它去了整个网站。
要解决此问题,我添加了一个变量$setcookie
并将其设置为1,以便使其跳过重定向。我的代码如下。
我认为这是一种混乱的方式来做到这一点,并认为必须有一个更清洁的方式,有没有人有一个建议,这将有助于我使用。我是否以正确的方式去做这件事?
<?php
if($_SERVER['HTTP_REFERER'] == "http://www.madisonstudios.mobi/" || $_SERVER['HTTP_REFERER'] == "http://madisonstudios.mobi/")
{
setcookie('fromMobi', true, time()+3600*24);
$setcookie = 1;
}
if ($_COOKIE["fromMobi"] == 1 || $setcookie == 1)
{
} else {
$uamatches = array("midp", "j2me", "avantg", "docomo", "novarra", "palmos", "palmsource", "240x320", "opwv", "chtml", "pda", "windows\ ce", "mmp\/", "blackberry", "mib\/", "symbian", "wireless", "nokia", "hand", "mobi", "phone", "cdm", "up\.b", "audio", "SIE\-", "SEC\-", "samsung", "HTC", "mot\-", "mitsu", "sagem", "sony", "alcatel", "lg", "erics", "vx", "NEC", "philips", "mmm", "xx", "panasonic", "sharp", "wap", "sch", "rover", "pocket", "benq", "java", "pt", "pg", "vox", "amoi", "bird", "compal", "kg", "voda", "sany", "kdd", "dbt", "sendo", "sgh", "gradi", "jb", "\d\d\di", "moto","webos");
foreach($uamatches as $uastring){
if(preg_match("/".$uastring."/i",$_SERVER["HTTP_USER_AGENT"]))
{
header('Location: http://www.madisonstudios.mobi');
}
}
}
?>
答
我这样做:
<?php
// Use stripos() for tidiness, case-insensitivity and ignoring subdomains and paths
if (stripos($_SERVER['HTTP_REFERER'],'madisonstudios.mobi') !== FALSE) {
// We came from mobi, set the cookie that says so
setcookie('fromMobi', true, time()+3600*24);
} else if (empty($_COOKIE["fromMobi"])) {
// We only do this if the cookie is not set or it has a value that evaluates
// to FALSE - empty() does this check for us in one go
// Look for mobile browsers and redirect them to mobi
$uamatches = array("midp", "j2me", "avantg", "docomo", "novarra", "palmos", "palmsource", "240x320", "opwv", "chtml", "pda", "windows\ ce", "mmp\/", "blackberry", "mib\/", "symbian", "wireless", "nokia", "hand", "mobi", "phone", "cdm", "up\.b", "audio", "SIE\-", "SEC\-", "samsung", "HTC", "mot\-", "mitsu", "sagem", "sony", "alcatel", "lg", "erics", "vx", "NEC", "philips", "mmm", "xx", "panasonic", "sharp", "wap", "sch", "rover", "pocket", "benq", "java", "pt", "pg", "vox", "amoi", "bird", "compal", "kg", "voda", "sany", "kdd", "dbt", "sendo", "sgh", "gradi", "jb", "\d\d\di", "moto","webos");
foreach ($uamatches as $uastring) {
if (preg_match("/".$uastring."/i",$_SERVER["HTTP_USER_AGENT"])) {
header('Location: http://www.madisonstudios.mobi/');
// If we find one we know we can exit straight away because the user
// is getting redirected
exit;
}
}
}
+0
完美,谢谢。 – BrettAdamsGA 2012-01-03 19:52:26
把'出口();你调用'头之后'右()'跳过执行脚本的其余部分,并立即制定了重定向。 – Treffynnon 2012-01-03 16:55:19