ThinkPHP的cookie和session冲突造成Cookie不能使用的解决方法
这篇文章主要介绍了ThinkPHP的cookie和session冲突造成Cookie不能使用的解决方法,需要的朋友可以参考下
近期做一个网站的投票时,遇到ThinkPHP中的cookie和session冲突导致Cookie不能使用的情况。
该网站采用的是ThinkPHP框架制作,有一页面上面展示很多图片,要求每个图片下面有对应的投票,通过限制ip地址来,限制游客在一定时间范围内,能对多个图片投票,但是每个图片只能投票一次,开始使用ip存储在数据库解决,后来改进为使用session存储,,ip在服务文件里,不过要把生成的sessionid存储在本地的cookie中,按照ThinkPHP手册说明操作session一切顺利,但是在生成cookie之后,却在本地电脑中找不到cookie文本,原本以为是本地电脑限制cookie的生成,可是查看和测试的结果是没有限制,于是自己又单独建立了一个php文件,测试生成cookie,没问题,在本地找到了cookie文本,于是在网上查找到如下内容。顺序解决了问题,使用setcookie()函数设置cookie,得到了想要的结果。
解决办法如下:
找到php.ini 这个配置文件,然后查找一项:output_buffering将其值由原来的off改为on,重新启动Apache就ok了。
另外还有很多相似的问题:
有些时候会发现原在本地运行没有问题的文件,在服务器上测试时,竟然提示本地没有出现的错误: Warning: Cannot modify header information - headers already sent by…
这样的语句,很显然,造成这个原因是因为setcookie造成的,查了一下网上,有如下的解释: cookie本身在使用上有一些限制,例如:
1.呼叫setcookie的叙述必须放在标签之前
2.呼叫setcookie之前,不可使用echo
3.直到网页被重新载入後,cookie才会在程序中出现
4.setcookie函数必须在任何资料输出至浏览器前,就先送出
基於上面这些限制,所以执行setcookie()函数时,常会碰到“Undefined index”、“Cannot modify header information - headers already sent by”…等问题,解决“Cannot modify header information - headers already sent by”这个错误的方法是在产生cookie前,先延缓资料输出至浏览器,因此,您可以在程序的最前方加上ob_start()这个函数。这样问题就可以解决了。 但是如果要加上ob_start()的话,有不大可行,程序都写完了,才来改这个似乎有点郁闷!发现提示这个错误的时候,我就在想我本地怎么没提示这个问题呢,还以为是PHP.ini配置不一样,想想又不对,都是差不多的。于是看看后面跟的那句“output started at…”意思是在setcookie之前已经在另一处有输出了,于是找到output started at后面跟的那个文件,最后发现第一行是空白的。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

Cookies on your computer are stored in specific locations on your browser, depending on the browser and operating system used: 1. Google Chrome, stored in C:\Users\YourUsername\AppData\Local\Google\Chrome\User Data\Default \Cookies etc.

Cookies are usually stored in the cookie folder of the browser. Cookie files in the browser are usually stored in binary or SQLite format. If you open the cookie file directly, you may see some garbled or unreadable content, so it is best to use Use the cookie management interface provided by your browser to view and manage cookies.

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Cookies on the mobile phone are stored in the browser application of the mobile device: 1. On iOS devices, Cookies are stored in Settings -> Safari -> Advanced -> Website Data of the Safari browser; 2. On Android devices, Cookies Stored in Settings -> Site settings -> Cookies of Chrome browser, etc.

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.
