PHP调用session_start后页面始终加载的问题研究
本文章来给大家介绍一篇PHP调用session_start后页面始终加载的问题研究,有需要的朋友可参考。一个PHP页面的执行时间比较长(15秒左右),而只要这个页面没有执行完毕,其他的页面访问都是长时间加载状态,只有那个页面执行完毕了,剩下的页面才能打开。
这是什么情况呢,经过查看,两个页面都设计到了SESSION操作,示例代码如下:
页面一:
页面二:
访问页面一地时候同时访问页面二,你会发现页面二会一直保持加载状态直到页面一执行完毕。
问题的原因是什么呢?
答案是PHP的SESSION机制在作怪,PHP只会在页面代码执行完毕之后才会把SESSION数据写入到文件中,页面不执行完毕,对应的SESSION文件一直都保存着锁定状态,而其他的页面要访问这个SESSION文件就只能保持等待状态,这就是为什么页面二需要等待页面一执行完毕之后才执行。
知道原因后,我们应该如何解决这个问题呢?
答案很简单,PHP已经在内核中提供了相关接口:函数 session_write_close
函数官方文档的描述如下:
Session data is usually stored after your script terminated without the need to call session_write_close(), but as session data is locked to prevent concurrent writes only one script may operate on a session at any time. When using framesets together with sessions you will experience the frames loading one by one due to this locking. You can reduce the time needed to load all the frames by ending the session as soon as all changes to session variables are done.
Session数据通常在你的脚本结束后自动保存而不需要调用session_write_close函数。但是session为了避免数据被同时写入将文件锁定住以保证每次只有一个脚本可以访问文件。当你在用框架页面同时访问SESSION的时候将会遇到因为文件锁定而出现的框架持续加载情况。你可以在处理完SESSION变量后立即使用这个函数减少多个框架的加载时间。
像前面的问题,我们可以直接在操作完SESSION数据后调用session_write_close函数立即写入session数据,这样就不会影响到其他页面的正常运行。代码如下:

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

AI Hentai Generator
Generate AI Hentai for free.

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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
