Home > Backend Development > PHP Problem > Solve the problem of garbled php cookie

Solve the problem of garbled php cookie

藏色散人
Release: 2023-03-02 18:28:01
Original
3168 people have browsed it

php The solution to the garbled cookie: first open the corresponding PHP code file; then use the built-in function "unicode_encode" in PHP to convert a unicode string into the desired encoding method to solve the garbled problem.

Solve the problem of garbled php cookie

PHP Get COOKIE value and Chinese garbled solution

Getting cookie value in php is very simple as long as COOKIE[ ] In the middle is the cookie id name, which can be obtained. Let me briefly introduce you to an example of using cookies in PHP.

Recommended: "PHP Tutorial"

Assign value to cookie

setcookie (name, value, expire, path, domain)
Copy after login

For example:

<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);
?>
Copy after login

What if we want to get the user value Operation

<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);
?>
Copy after login

If we do not set the user cookie, we will make an error when executing, so we can use the isset function to judge.

<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>";
?>
Copy after login

Chinese characters are always garbled

For example, after "Xiaowei" is obtained, it is "%u5C0F%u4F1F"

This is actually not garbled code, but For unicode encoding, there is a built-in function called unicode_encode in PHP that can convert a unicode string into the encoding method you want. The function prototype is: string unicode_encode (unicode input, string encoding)

Here is one You can refer to the example:

<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);
?>
Copy after login

Example combines js php to implement page browsing statistics

<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  // 过期时间
);
Copy after login

When this page is run, the server will write a cookie value to save the number of times you visit the page. . The setcookie method of php is applied here.

Output this value:

Now let’s look at how to use js to get and set cookies

<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’);
Copy after login

Get the value of the key name demoCookie (if it does not exist, null is returned).

$.cookie(‘demoCookie’,text,{expires: 7});
Copy after login

When the save link is clicked, the value of the input box is written to the cookie.

The above is the detailed content of Solve the problem of garbled php cookie. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template