源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

怎样使用php与jquery设置和读取cookies

  • 时间:2020-10-17 09:52 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:怎样使用php与jquery设置和读取cookies
HTTP协议是一种无状态协议,这意味着你对网站的每一个请求都是独立的,而且因此无法通过它自身保存数据。但这种简单性也是它在互联网早期就广泛传播的原因之一。 不过,它仍然有一种方法能让你用cookies的形式来保存请求之间的信息。这种方法使你能够更有效率的进行会话管理和维持数据。 有两种处理cookies的方式—服务端(php,asp等)和客户端(javascript).在这个教程中,我们将学习到以php和javascript这两种方式如何去创建cookies。 [b]Cookies and php [/b]  [b]setting cookies [/b]在php中创建cookie你需要用到setcookie这个方法。它需要些参数(除了第一个参数是必需的,其它参数都是可选的)
[url=styles.css]<mce:script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" mce_src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></mce:script> <mce:script type="text/javascript" src="jquery.cookie.js" mce_src="jquery.cookie.js"></mce:script> <mce:script type="text/javascript"><!-- $(document).ready(function(){  var cookie = $.cookie('demoCookie');  // If the cookie has been set in a previous page load, show it in the div directly:  if(cookie) $('.jq-text').text(cookie).show();           $('.fields a').click(function(e){   var text = $('#inputBox').val();   // Setting a cookie with a seven day validity:   $.cookie('demoCookie',text,{expires: 7, path: '/', domain: 'demo.tutorialzine.com'});   $('.jq-text').text(text).slideDown('slow');   e.preventDefault();  });  $('#form1').submit(function(e){ e.preventDefault(); }) }) // --></mce:script> </head> <body> <h1>MicroTut: Getting And Setting Cookies With jQuery & PHP</h1> <h2>Go Back <a href="http://tutorialzine.com/2010/03/microtut-getting-setting-cookies-jquery-php/" mce_href="http://tutorialzine.com/2010/03/microtut-getting-setting-cookies-jquery-php/">To The Tutorial »</a></h2> <div class="section">  <div class="counter"><?php echo $visited?></div>     <p>The number above indicates how many times you've visited this page (PHP cookie). Reload to test.</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="">Save</a>         </div>     </form>     <p>Write some text in the field above and click Save. It will be saved between page reloads with a jQuery cookie.</p> </div> </body> </html>
[b]jquery.cookie.js [/b]
/**  * Cookie plugin  *  * Copyright (c) 2006 Klaus Hartl (stilbuero.de)  * Dual licensed under the MIT and GPL licenses:  * http://www.opensource.org/licenses/mit-license.php  * http://www.gnu.org/licenses/gpl.html  *  */ /**  * Create a cookie with the given name and value and other optional parameters.  *  * @example $.cookie('the_cookie', 'the_value');  * @desc Set the value of a cookie.  * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });  * @desc Create a cookie with all available options.  * @example $.cookie('the_cookie', 'the_value');  * @desc Create a session cookie.  * @example $.cookie('the_cookie', null);  * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain  *       used when the cookie was set.  *  * @param String name The name of the cookie.  * @param String value The value of the cookie.  * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.  * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.  *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.  *                             If set to null or omitted, the cookie will be a session cookie and will not be retained  *                             when the the browser exits.  * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).  * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).  * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will  *                        require a secure protocol (like HTTPS).  * @type undefined  *  * @name $.cookie  * @cat Plugins/Cookie  * @author Klaus Hartl/klaus.hartl@stilbuero.de  */ /**  * Get the value of a cookie with the given name.  *  * @example $.cookie('the_cookie');  * @desc Get the value of a cookie.  *  * @param String name The name of the cookie.  * @return The value of the cookie.  * @type String  *  * @name $.cookie  * @cat Plugins/Cookie  * @author Klaus Hartl/klaus.hartl@stilbuero.de  */ 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         }         // CAUTION: Needed to parenthesize options.path and options.domain         // in the following expressions, otherwise they evaluate to undefined         // in the packed version for some reason...         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;     } };
[b]styles.css [/b]
[u]复制代码[/u] 代码如下:
*{  margin:0;  padding:0; } body{  /* Setting default text color, background and a font stack */  color:#555555;  font-size:0.825em;  background: #fcfcfc;  font-family:Arial, Helvetica, sans-serif; } .section{  margin:0 auto 60px;  text-align:center;  width:600px; } .counter{  color:#79CEF1;  font-size:180px; } .jq-text{  display:none;  color:#79CEF1;  font-size:80px; } p{  font-size:11px;  padding:0 200px; } form{  margin-bottom:15px; } input[type=text]{  border:1px solid #BBBBBB;  color:#444444;  font-family:Arial,Verdana,Helvetica,sans-serif;  font-size:14px;  letter-spacing:1px;  padding:2px 4px; } /* The styles below are only necessary for the styling of the demo page: */ h1{  background:#F4F4F4;  border-bottom:1px solid #EEEEEE;  font-size:20px;  font-weight:normal;  margin-bottom:15px;  padding:15px;  text-align:center; } h2 {  font-size:12px;  font-weight:normal;  padding-right:40px;  position:relative;  right:0;  text-align:right;  text-transform:uppercase;  top:-48px; } a, a:visited {  color:#0196e3;  text-decoration:none;  outline:none; } a:hover{  text-decoration:underline; } .clear{  clear:both; } h1,h2,p.tutInfo{  font-family:"Myriad Pro",Arial,Helvetica,sans-serif; }
[b]结束语: [/b]关于cookie的使用,你需要注意的是不要把一些敏感信息(例如用户名、密码)保存在cookies中, 为因当每一个页面加载时cookies都会和headers一样传递到页面,很容易被不法份子截取到。 但是,只要有适当的预防措施,你就可以用这个简单的技术实现大量的互动。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部