cookie 实现 代码 和问题

<script type="text/javascript">
$(function(){

<!-- 1.domain表示的是cookie所在的域,默认为请求的地址,如网址为www.jb51.net/test/test.aspx,那么domain默认为www.jb51.net。而跨域访问,如域A为t1.test.com,域B为t2.test.com,那么在域A生产一个令域A和域B都能访问的cookie就要将该cookie的domain设置为.test.com;如果要在域A生产一个令域A不能访问而域B能访问的cookie就要将该cookie的domain设置为t2.test.com。
2.path表示cookie所在的目录,asp.net默认为/,就是根目录。在同一个服务器上有目录如下:/test/,/test/cd/,/test/dd/,现设一个cookie1的path为/test/,cookie2的path为/test/cd/,那么test下的所有页面都可以访问到cookie1,而/test/和/test/dd/的子页面不能访问cookie2。这是因为cookie能让其path路径下的页面访问。
3.浏览器会将domain和path都相同的cookie保存在一个文件里,cookie间用*隔开。
4.含值键值对的cookie:以前一直用的是nam=value单键值对的cookie,一说到含多个子键值对的就蒙了。现在总算弄清楚了。含多个子键值对的cookie格式是name=key1=value1&key2=value2。可以理解为单键值对的值保存一个自定义的多键值字符串,其中的键值对分割符为&,当然可以自定义一个分隔符,但用asp.net获取时是以&为分割符。-->


	var is_news = getCookie('news');
	if(!is_news)
	{
		$('.ads-news').slideDown();
	}else{
		$('.ads-news').remove();
	}

	$('.ads-news .close-button').click(function(){
		$.ajax({
			type:'get',
			url:'/home/index/news',
			success:function(res){
				setCookie('news',res.key,30);
				$('.ads-news').slideUp();
				// location.reload();
			},dataType:'json'
		});
		// $(this).closest('.ads-news').remove();
	});



	var width = $(window).width();

	console.log("width:",width);
	if(width<600 || width<420 || width<=320)
	{
		$('.mt-element-ribbon').css('left','');
	}


	function setCookie(c_name,value,expiredays)
	{
		var exdate=new Date()
		exdate.setDate(exdate.getDate()+expiredays)
		document.cookie=c_name+ "=" +escape(value)+
		((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+";path=/"
	}

	function getCookie(c_name)
	{
		if (document.cookie.length>0)
		  {
		  c_start=document.cookie.indexOf(c_name + "=")
		  if (c_start!=-1)
		    { 
		    c_start=c_start + c_name.length+1 
		    c_end=document.cookie.indexOf(";",c_start)
		    if (c_end==-1) c_end=document.cookie.length
		    return unescape(document.cookie.substring(c_start,c_end))
		    } 
		  }
		return ""
	}
})
	
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的购物车实现代码,使用 cookie 和 session 存储购物车信息: <?php session_start(); // 添加商品到购物车 function add_to_cart($product_id, $quantity) { // 如果购物车中已经有该商品,则增加数量 if (isset($_SESSION['cart'][$product_id])) { $_SESSION['cart'][$product_id] += $quantity; } else { $_SESSION['cart'][$product_id] = $quantity; } } // 从购物车中删除商品 function remove_from_cart($product_id) { unset($_SESSION['cart'][$product_id]); } // 获取购物车中的商品数量 function get_cart_count() { $count = 0; if (isset($_SESSION['cart'])) { foreach ($_SESSION['cart'] as $quantity) { $count += $quantity; } } return $count; } // 获取购物车中的商品列表 function get_cart_items() { $items = array(); if (isset($_SESSION['cart'])) { foreach ($_SESSION['cart'] as $product_id => $quantity) { $items[] = array( 'product_id' => $product_id, 'quantity' => $quantity ); } } return $items; } // 在页面头部设置购物车数量 function set_cart_count_header() { $count = get_cart_count(); header('X-Cart-Count: ' . $count); } // 添加商品到购物车 if (isset($_POST['add_to_cart'])) { $product_id = $_POST['product_id']; $quantity = $_POST['quantity']; add_to_cart($product_id, $quantity); set_cart_count_header(); } // 从购物车中删除商品 if (isset($_POST['remove_from_cart'])) { $product_id = $_POST['product_id']; remove_from_cart($product_id); set_cart_count_header(); } // 显示购物车中的商品列表 if (isset($_GET['view_cart'])) { $items = get_cart_items(); foreach ($items as $item) { echo 'Product ID: ' . $item['product_id'] . ', Quantity: ' . $item['quantity'] . '<br>'; } } // 清空购物车 if (isset($_GET['clear_cart'])) { unset($_SESSION['cart']); set_cart_count_header(); } ?> <form method="post"> <input type="hidden" name="product_id" value="1"> <input type="number" name="quantity" value="1"> <button type="submit" name="add_to_cart">Add to Cart</button> </form> <a href="?view_cart=1">View Cart</a> <form method="post"> <input type="hidden" name="product_id" value="1"> <button type="submit" name="remove_from_cart">Remove from Cart</button> </form> <a href="?clear_cart=1">Clear Cart</a>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值