项目的主要功能是利用编写注册好的com
通过php调用com接口进行远程服务器的连接和读取数据
并把数据展示到网页的功能。
$com->connect(server);
$com->read();
现在通过window.setInterval和ajax调用php文件已经可以实现每隔固定时间进行读取
但是使用此方法有个严重的问题,每次read之前都要connect服务器
不知道有没有什么方法可以只connect一次以后只需要read就可以了。
之前尝试使用单例模式也没有作用。
回复讨论(解决方案)
只要你的程序不不终止,就不需要重新连接
只要你的程序不不终止,就不需要重新连接
可能我没说明白
a.php:
$comvar = new COM("aaa.client") or die("create failed");
$comvar->connect(server);
$comvar->read();
?>
ajax在多次调用a.php时每次都connect
因为无法将$comvar变量设置为类似C语言中的全局变量,所以不能在第二次读取的时候直接使用$comvar->read();
mysql永久连接可以不? mysql_pconnect()
mysql永久连接可以不? mysql_pconnect()
连接方法被封装在com中,并不是mysql
每次请求结束,会自动释放当前php执行进程,所以php没有办法的。
对于网络连接,如果你的服务器是nginx,你可以用nginx做一个http长连接,php每次都是连接本地的nginx,这样php服务器连接目前机器,就只有一个网络连接了。
这个没有办法解决。
每次执行完都会断开连接的。除非你使用的是socket来连。
你前端用的是setInterval,不能做到connect一次,以后直接就read
你可以改成前端 websocket 和 php socket
还是你没明白我说的意思:只要你的程序不不终止,就不需要重新连接
但是在 HTTP 会话中,php 程序只运行在会话期间,也就是会话结束,程序就终止
所以重新连接是必然的。也不要错误的理解 C语言程序 的行为,因为在程序关闭前,他一直在运行。所以就不需要重新连接
如果你使用 websocket,那么会话就不是 HTTP 了,而是 websocket 协议(简称 WS)自然表现就不一样了
还是你没明白我说的意思:只要你的程序不不终止,就不需要重新连接
但是在 HTTP 会话中,php 程序只运行在会话期间,也就是会话结束,程序就终止
所以重新连接是必然的。也不要错误的理解 C语言程序 的行为,因为在程序关闭前,他一直在运行。所以就不需要重新连接
如果你使用 websocket,那么会话就不是 HTTP 了,而是 websocket 协议(简称 WS)自然表现就不一样了
刚开始确实没明白,不过现在懂了,已经花了2天时间改用websocket了

PHPsessionscanstorestrings,numbers,arrays,andobjects.1.Strings:textdatalikeusernames.2.Numbers:integersorfloatsforcounters.3.Arrays:listslikeshoppingcarts.4.Objects:complexstructuresthatareserialized.

TostartaPHPsession,usesession_start()atthescript'sbeginning.1)Placeitbeforeanyoutputtosetthesessioncookie.2)Usesessionsforuserdatalikeloginstatusorshoppingcarts.3)RegeneratesessionIDstopreventfixationattacks.4)Considerusingadatabaseforsessionstoragei

Session regeneration refers to generating a new session ID and invalidating the old ID when the user performs sensitive operations in case of session fixed attacks. The implementation steps include: 1. Detect sensitive operations, 2. Generate new session ID, 3. Destroy old session ID, 4. Update user-side session information.

PHP sessions have a significant impact on application performance. Optimization methods include: 1. Use a database to store session data to improve response speed; 2. Reduce the use of session data and only store necessary information; 3. Use a non-blocking session processor to improve concurrency capabilities; 4. Adjust the session expiration time to balance user experience and server burden; 5. Use persistent sessions to reduce the number of data read and write times.

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita


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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
