Home >Backend Development >PHP Tutorial >How to solve the problem of invalid session and cookie in thinkphp

How to solve the problem of invalid session and cookie in thinkphp

不言
不言Original
2018-06-11 10:09:082314browse

This article mainly introduces the solution to invalid session and cookie in thinkphp, involving the analysis and deletion method of BOM header. It has certain reference value. Friends in need can refer to the example of this article

Describes the solution to invalid session and cookie in thinkphp. Share it with everyone for your reference. The specific analysis is as follows:

Problem description:

There is no problem with session and cookie when debugging locally. I use session to save the information of the current login account and upload it. After going to the server, I found that the session no longer existed after the jump. Why? The output session on the current page exists.

I have encountered this problem for three days. Because I am self-study, there is no teacher to ask for advice, and there are not many people around me who are doing PHP, so it is really tragic, so... Baidu, and many people on Baidu are talking nonsense, and there is nothing that can solve the problem. After three days of modification, I finally found the root cause. It turns out that the majority of servers that support thinkphp are Linux systems, and our programming is basically the same. Windows, this will cause the BOM header problem, and PHP cannot parse the BOM header, but directly removes it, causing the session and cookie to be unusable.

Solution:

What is a bom? In a UTF-8 encoded file, the BOM is in the header of the file, occupying three bytes, which is used to indicate that the file belongs to UTF-8 encoding. Now there are many software that recognize the BOM header, but there are still some that cannot recognize the BOM header, such as PHP. The BOM header cannot be recognized, which is why an error occurs after editing the UTF-8 encoding with Notepad.

The simple way to remove the BOM header is the following two:

1 , Editplus method to remove BOM header

After the editor is adjusted to UTF8 encoding format, there will be a string of hidden characters (that is, BOM) in front of the saved file, which is used by the editor to identify whether the file is Encode in UTF8.

Run Editplus, click Tools, select Preferences, select the file, select UTF-8 identification to always delete the signature, and then edit and save the PHP file without BOM. .

2. Ultraedit method to remove BOM header

After opening the file, select the encoding format in the Save as option (utf-8 without BOM header), confirm and it will be ok. How about removing it? The bom header is very simple.

Let’s talk about the BOM information of utf8. BOM refers to the storage method of the PHP file itself as UTF-8 with BOM. The Chinese garbled way of ordinary pages is generally not caused by this reason. .

The code is as follows:

header("Content-type: text/html; charset=utf-8");

This sentence controls the encoding method of the html output page. The BOM will only be stored as UTF-8 using "Notepad" under WINDOWS. Yes, you can use WINHEX to delete the first 2 bytes.

In the encoding settings in Dreamweaver, you can set whether to include BOM. Generally, as long as the output of php is not a picture (GDI Stream), BOM will not be included. Causes problems. If the GDI Stream has extra characters at the beginning, it will be displayed as a red cross.

The above is the entire content of this article. I hope it will be helpful to everyone's learning. For more related content, please pay attention to the PHP Chinese website !

Related recommendations:

thinkPHP5.0 framework configuration scope method

thinkPHP5 framework custom validator implementation

The above is the detailed content of How to solve the problem of invalid session and cookie in thinkphp. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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