Home > Article > Backend Development > Solve the problem of garbled php cookie
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.
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)
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('copy6428')">复制代码</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); ?>
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('copy6844')">复制代码</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); ?>
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('copy3666')">复制代码</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>"; ?>
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('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); ?>
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('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 // 过期时间 );
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('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’);
Get the value of the key name demoCookie (if it does not exist, null is returned).
$.cookie(‘demoCookie’,text,{expires: 7});
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!