Home >Backend Development >PHP Tutorial >Solution to invalid session and cookie in thinkphp, thinkphpsession_PHP tutorial
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 find that the session no longer exists after the jump. Why? The output session on the current page exists.
I have been encountering this problem for three days. Because I am self-study and there is no teacher to ask for advice, and there are not many people around me who are good at PHP, so it is really tragic, so I turned to Baidu, and many people on Baidu are talking nonsense. There is nothing that can solve the problem. After three days of modification, I finally found the root cause. It turns out that the vast majority of servers that support thinkphp are Linux systems, and our programming is basically Windows, so the problem of bom will appear, and PHP cannot parse the BOM header, but directly removes it, causing session and cookies to become 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 softwares 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.
There are two simple ways to remove bom:
1. How to remove 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, always delete the signature, and then edit and save the PHP file. The PHP file will not have a BOM.
2. Ultraedit method to remove BOM
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 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.
This sentence controls the encoding method of the html output page. The BOM is only available when "Notepad" is used to store it as UTF-8 under Windows. You can use WINHEX to delete the first 2 bytes.
In the encoding settings in Dreamweaver, you can set whether to include a BOM. Generally, as long as the output of PHP is not a picture (GDI Stream), the BOM will not cause problems. If the GDI Stream has extra characters at the beginning, 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.