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.
If we do not set the user cookie, we will make an error when executing, so we can use the isset function to judge.
代码如下
复制代码
";
else
echo"Welcomeguest! ";
?>
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:
代码如下
复制代码
Copy after login
Example combines js php to implement page browsing statistics
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
代码如下
复制代码
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!
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