php 获取cookie值与中文乱码解决方法
php中获取cookie值非常的简单只要cookie[]中间是cookie id名就可以获取到了,下面来简单的给大家介绍下php中cookie的一个使用例子。
推荐:《php教程》
给cookie赋值
setcookie (name, value, expire, path, domain)
例如:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#fb7"> <tr> <td width="464" height="27" bgcolor="#ffe7ce"> 代码如下</td> <td width="109" align="center" bgcolor="#ffe7ce" style="cursor:pointer;" onclick="docopy('copy6428')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#ffffff" style="padding:10px;" class="copyclass" id=copy6428><?phpsetcookie(“user”, “alex porter”, time() 3600);?>
如果我们要获取user值如何操作
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#fb7"> <tr> <td width="464" height="27" bgcolor="#ffe7ce"> 代码如下</td> <td width="109" align="center" bgcolor="#ffe7ce" style="cursor:pointer;" onclick="docopy('copy6844')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#ffffff" style="padding:10px;" class="copyclass" id=copy6844><?phpecho $_cookie["user"];print_r($_cookie);?>
如果我们没设置user cookie那么我们执行时会出错了,这样我们可以使用isset函数来加以判断。
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#fb7"> <tr> <td width="464" height="27" bgcolor="#ffe7ce"> 代码如下</td> <td width="109" align="center" bgcolor="#ffe7ce" style="cursor:pointer;" onclick="docopy('copy3666')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#ffffff" style="padding:10px;" class="copyclass" id=copy3666><?phpif(isset($_cookie["user"]))echo"welcome".$_cookie["user"]."!<br>";elseecho"welcomeguest!<br>";?>
中文总是乱码
比如“小伟”获取后是“%u5c0f%u4f1f”
这个其实不是乱码,而是unicode的编码,在php中有一个内置函数叫unicode_encode可以将一个unicode字符串转变为你想要的编码方式,函数原型为:string unicode_encode ( unicode input, string encoding )
这里有一个例子可以参考一下:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#fb7"> <tr> <td width="464" height="27" bgcolor="#ffe7ce"> 代码如下</td> <td width="109" align="center" bgcolor="#ffe7ce" style="cursor:pointer;" onclick="docopy('copy2845')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#ffffff" style="padding:10px;" class="copyclass" id=copy2845><?php header ('content-type: text/plain; charset=iso-8859-2'); $encoded = unicode_encode ('\u0150\u0179', 'iso-8859-2'); echo 'unicode semantics: ', ini_get ('unicode_semantics'), php_eol, 'the string itself: '; printf ($encoded . php_eol, '%s'); echo 'the length of the string: ', strlen ($encoded);?>
例子 结合js php实现页面浏览统计
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#fb7"> <tr> <td width="464" height="27" bgcolor="#ffe7ce"> 代码如下</td> <td width="109" align="center" bgcolor="#ffe7ce" style="cursor:pointer;" onclick="docopy('copy2948')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#ffffff" style="padding:10px;" class="copyclass" id=copy2948>// 浏览页面次数$visited = (int)$_cookie['pagevisits'] 1;setcookie( 'pagevisits', // cookie名$visited, // cookie值time() 7*24*60*60 // 过期时间);
当运行这个页面时服务器端会写入个cookie值,用于保存你访问该页面的次数。这里应用到了php的setcookie方法。
输出这个值:
现在来看如何使用js获取和设置cookie
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#fb7"> <tr> <td width="464" height="27" bgcolor="#ffe7ce"> 代码如下</td> <td width="109" align="center" bgcolor="#ffe7ce" style="cursor:pointer;" onclick="docopy('copy2304')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#ffffff" style="padding:10px;" class="copyclass" id=copy2304>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(); })var cookie = $.cookie(‘democookie’);
获取键名democookie的值(如果不存在返回的是null)。
$.cookie(‘democookie’,text,{expires: 7});
当点击保存链接的时候,将输入框的值写入cookie。
以上就是解决php cookie乱码的问题的详细内容。
