JS处理COOKIE的函数

没有评论

2011 年 02 月 14 日 at 下午 4:58分类:JavaScript | WEB开发

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>cookie处理函数练习(为我所写,非我所想:改善面向对象)</title>
<script language="JavaScript" type="text/javascript">
function addCookie(objName,objValue,objHours){//添加cookie
var str = objName + "=" + escape(objValue);
if(objHours > 0){//为0时不设定过期时间,浏览器关闭时cookie自动消失
var date = new Date();
var ms = objHours*3600*1000;
date.setTime(date.getTime() + ms);
str += "; expires=" + date.toGMTString();
}
document.cookie = str;
alert(str);
}

function getCookie(objName){//获取指定名称的cookie的值
var arrStr = document.cookie.split("; ");
for(var i = 0;i < arrStr.length;i ++){
var temp = arrStr[i].split("=");
if(temp[0] == objName) return unescape(temp[1]);
}  
}

function delCookie(name){//为了删除指定名称的cookie,可以将其过期时间设定为一个过去的时间
var date = new Date();
date.setTime(date.getTime() - 10000);
document.cookie = name + "=a; expires=" + date.toGMTString();
}

function allCookie(){//读取所有保存的cookie字符串
var str = document.cookie;
if(str == ""){
str = "没有保存任何cookie";
}
alert(str);
}

function $(m,n){
return document.forms[m].elements[n].value;
}

function add_(){
var cookie_name = $("myform","cookie_name");
var cookie_value = $("myform","cookie_value");
var cookie_expireHours = $("myform","cookie_expiresHours");
addCookie(cookie_name,cookie_value,cookie_expireHours);
}

function get_(){
var cookie_name = $("myform","cookie_name");
var cookie_value = getCookie(cookie_name);
alert(cookie_value);
}

function del_(){
var cookie_name = $("myform","cookie_name");
delCookie(cookie_name);
alert("删除成功");
}
function frameReload()
{
parent.show.location.reload();
}
</script>

</head>
<?php
if( $_GET['show'])
{
print_r( $_COOKIE);
exit;
}

?>
<body>
<form name="myform">
<div><label for="cookie_name">名称</label><input type="text" name="cookie_name" /></div>
<div><label for="cookie_value">取值</lable><input type="text" name="cookie_value" /></div>
<div><label for="cookie_expireHours">多少个小时过期</lable><input type="text" name="cookie_expiresHours" /></div>
<div>
<input type="button" value="添加该cookie" onclick="add_();frameReload();" />
<input type="button" value="读取所有cookie" onclick="allCookie();frameReload();" />
<input type="button" value="读取该名称cookie" onclick="get_();frameReload();" />
<input type="button" value="删除该名称cookie" onclick="del_();frameReload();" />
</div>
</form>
<hr />
<iframe name="show" id="show" src="./cookie.php?show=true" style="display:block;width:700px;height:300px;"></iframe>
</body>
</html>

PHP 检测手机浏览器的代码

没有评论

2011 年 02 月 14 日 at 上午 9:00分类:PHP | WEB开发

<?php 
 
function is_mobile(){ 
 
    // returns true if one of the specified mobile browsers is detected 
 
    $regex_match="/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|"; 
    $regex_match.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|"; 
    $regex_match.="blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|";     
    $regex_match.="symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|"; 
    $regex_match.="jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220"; 
    $regex_match.=")/i";         
    return isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE']) or preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT'])); 
} 
 
/* 
allow the user a way to force either the full or mobile versions of the site - use a GET parameter on requests: 
 
include likes to both versions of the site w/ the special force mode parameters, 'mobile' and 'full': 
 
<a href="http://blog.fbbin.com/?mobile">View Mobile Site</a> 
<a href="http://blog.fbbin.com/?full">View Full Site</a> 
 
Always check for 'mobile' or 'full' parameters before accounting for any User-Agent conditions: 
*/ 
 
if ($_GET['mobile']) { 
 $is_mobile = true; 
} 
  
if ($_GET['full']) { 
 $is_mobile = false; 
} 
if($is_mobile) { 
    //it's a mobile browser, do something 
    header("Location: http://blog.fbbin.com/mobile"); 
} else { 
    //it's not a mobile browser, do something else 
    header("Location: http://blog.fbbin.com/desktop"); 
    // or instead of a redirect, simply build html below 
} 
 
?>