The example in 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. After uploading to the server, I found that the session no longer exists 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 Baidu Many people on the Internet 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 Windows, so The problem of bom header will occur, and PHP cannot parse the bom header, but directly removes it, causing session and cookie to be unusable.
Solution:
What is bom header? 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 UTF-8 encoding with Notepad.
The simple ways to remove the BOM header are the following two:
1. The method of removing the BOM header with editplus
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 encoded 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. The PHP file will not have a BOM.
2. How to remove the BOM header with ultraedit
After opening the file, Select the encoding format in the save as option (utf-8 without BOM header), confirm and it is ok. How about, it is very simple to remove the BOM header.
Let’s talk about the BOM information of utf8. BOM refers to the PHP file itself. The storage method is UTF-8 with BOM. The garbled Chinese characters on ordinary pages are generally not caused by this reason.
header("Content-type: text/html; charset=utf-8");
This sentence controls the encoding method of the html output page. The BOM can only be stored in "Notepad" under WINDOWS. It only exists in UTF-8. 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 be included. It will not cause problems. If there are extra characters at the beginning of the GDI Stream, it will be displayed as a red cross.
I hope this article will be helpful to everyone’s PHP programming based on the ThinkPHP framework.
For more articles related to solutions to invalid session and cookies in thinkphp, please pay attention to the PHP Chinese website!