首頁  >  文章  >  後端開發  >  解決php cookie亂碼的問題

解決php cookie亂碼的問題

藏色散人
藏色散人原創
2020-07-21 10:46:493070瀏覽

php cookie亂碼的解決方案:先開啟對應的PHP程式碼檔案;然後透過php中的內建函數「unicode_encode」將一個unicode字串轉變為想要的編碼方式即可解決亂碼問題。

解決php cookie亂碼的問題

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(&#39;copy6428&#39;)">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6428>
<?php
setcookie(“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(&#39;copy6844&#39;)">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6844>
<?php
echo $_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(&#39;copy3666&#39;)">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3666>
<?php
if(isset($_COOKIE["user"]))
echo"Welcome".$_COOKIE["user"]."!<br>";
else
echo"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(&#39;copy2845&#39;)">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2845>
<?php
  header (&#39;Content-Type: text/plain; charset=ISO-8859-2&#39;);
  $encoded = unicode_encode (&#39;\u0150\u0179&#39;, &#39;ISO-8859-2&#39;);
  echo &#39;Unicode semantics: &#39;, ini_get (&#39;unicode_semantics&#39;), PHP_EOL, &#39;The string itself: &#39;;
  printf ($encoded . PHP_EOL, &#39;%s&#39;);
  echo &#39;The length of the string: &#39;, 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(&#39;copy2948&#39;)">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2948>
// 浏览页面次数
$visited = (int)$_COOKIE[&#39;pageVisits&#39;] 1;
setcookie( &#39;pageVisits&#39;,  // 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(&#39;copy2304&#39;)">复制代码</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亂碼的問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn