没能理解的一个‘类’,求解释
以下代码是一个程序中的一个类文件(lock.php),先说下他lock.php实际的作用:
在未知(是我不理解)的情况下,会在缓存文件夹 cache下生成一个.lock文件,例如index.php.lock 如果出现这个index.php.lock文件,那网站的首页就打不开了,必须手动删除它,再刷新首页,出现正常的缓存文件index.php 才能正常打开系统。
这个故障是不定时发生的,我现在也不理解在什么情况下才会发生,不理解为什么源码的作者为什么要设置这个功能。因为不知道最终来源,也没办法找源码的作者咨询了。
前几天,我把这个类文件lock.php删除了,网站一直正常,我以为这样就不会再有index.php.lock 文件了,但今天系统又打不开了,发现正常的cache/index.php缓存不见了,把这个类文件还原,然后再刷新系统,出现正常的cache/index.php,系统也就正常了。
lock.php的源码如下:
<?php<br /> <br /> class lock{<br /> private $num,$lock_marker,$lock_file,$timeout;<br /> public $islock;<br /> private function file_timeout(){<br /> if(file_exists($this->lock_file)){<br /> if(time()-filemtime($this->lock_file)>=$this->timeout){<br /> @unlink($this->lock_file);<br /> return true;<br /> }<br /> return false;<br /> }else return true;<br /> }<br /> function __construct($num=1,$timeout=3,$wait=false,$wait_time=0,$marker = ''){<br /> $this->num=$num;<br /> $marker ||$marker = $_SERVER['SCRIPT_FILENAME'];<br /> $marker = md5($marker);<br /> $this->lock_marker = $marker;<br /> $this->timeout=$timeout;<br /> $this->lock_file=d('./cache/lock/'.$this->lock_marker.$this->num.'.lock');<br /> if(file_exists($this->lock_file)){<br /> $this->islock=!$this->file_timeout();<br /> }else $this->islock=false;<br /> if(!$this->islock)touch($this->lock_file);<br /> else {<br /> if($wait){<br /> $wait_start=0;<br /> while(!$this->file_timeout()){<br /> if($wait_time>0){<br /> $wait_start+=100000;<br /> if($wait_start>$wait_time)break;<br /> }<br /> }<br /> file_exists($this->lock_file)&&@unlink($this->lock_file);<br /> touch($this->lock_file);<br /> $this->islock=false;<br /> }<br /> }<br /> }<br /> function __destruct(){<br /> $this->close();<br /> }<br /> public function close(){<br /> !$this->islock&&file_exists($this->lock_file)&&@unlink($this->lock_file);<br /> }<br /> }<br /> <br /> ?>
------解决方案--------------------
我怎么觉得你这个代码不能正常运行呢?
if(!$this->islock) touch($this->lock_file);//修改缓存文件的访问时间
执行的条件是 $this->islock 为假
而 $this->islock 为假的一个情况是:
$this->islock=!$this->file_timeout();
即方法 $this->file_timeout 返回真
而 $this->file_timeout 方法返回真时有
@unlink($this->lock_file);//删除缓存文件
return true;
既然缓存文件被删除了,那么 touch($this->lock_file) 不就要报错了吗?
Warning: touch() [function.touch]: Unable to create file because Invalid argument
另外下面还有
file_exists($this->lock_file)&&@unlink($this->lock_file);//缓存文件存在就删除掉
touch($this->lock_file);//又在修改不存在的文件的访问时间

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.


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

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
