Solve the problem of garbled php cookie

藏色散人
Release: 2023-03-02 18:28:01
Original
2956 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:

代码如下 复制代码
Copy after login

What if we want to get the user value Operation

代码如下 复制代码
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.

代码如下 复制代码
"; 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

代码如下 复制代码
// 浏览页面次数 $visited = (int)$_COOKIE['pageVisits'] 1; setcookie( 'pageVisits', // 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

代码如下 复制代码
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!