首页 > php代码 > jquery+php获取和设置Cookies代码

jquery+php获取和设置Cookies代码

// 浏览页面次数

 代码如下 复制代码

$visited = (int)$_cookie['pagevisits'] + 1;

setcookie( 'pagevisits',    // cookie名
     $visited,     // cookie值
   time()+7*24*60*60   // 过期时间
          ); 

?>

<!doctype html public "-//w3c//dtd xhtml 1.0 strict//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>使用jquery+php获取和设置cookies</title>

<link rel="stylesheet" type="text/css教程" href="styles.css" />

<script type="text/网页特效" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery.cookie.js">

jquery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toutcstring)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new date();
                date.settime(date.gettime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toutcstring(); // use expires attribute, max-age is not supported by ie
        }

        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeuricomponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookievalue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jquery.trim(cookies[i]);
                // does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookievalue = decodeuricomponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookievalue;
    }
};
</script>
<script type="text/javascript">

$(document).ready(function(){
 
 var cookie = $.cookie('democookie');
 
 if(cookie) $('.jq-text').text(cookie).show();
 
        
 $('.fields a').click(function(e){
  var text = $('#inputbox').val();
  
  // 设置cookie的值
  $.cookie('democookie',text,{expires: 7});
  
  $('.jq-text').text(text).slidedown('slow');
  
  e.preventdefault();
 });
 
 $('#form1').submit(function(e){ e.preventdefault(); })
})

</script>

</head>

<body>
<h1>使用jquery+php获取和设置cookies</h1>

<div class="section">
 <div class="counter"><?php echo $visited?></div>
    <p>你访问此页面的次数</p>
</div>


<div class="section">
 
    <div class="jq-text"></div>

 <form action="" method="get" id="form1">
     <div class="fields">
         <input type="text" id="inputbox" />
            <a href="">保存</a>
        </div>
    </form>
    <p>你可以在输入框输入任意值,当你重新打开页面时,值依旧存在。</p>
</div>

</body>
</html>


http://www.phprm.com下载
http://www.phprm.com



教程链接:http://www.phprm.com/code/35038.html

随意转载~但请保留教程地址★

标签:none

发表留言