After Harvey Hardware Network switched to a new news release platform last week, due to temporary needs, the original news platform was not completely stopped. Therefore, after the new platform started to be used, a headache was discovered. The homepage of the website It is always updated to the original system content, eventually causing a certain news link to be unable to be accessed normally. The new system has optimized the URL, and the URL of the news page is different from the original system. At first, I thought it was because someone was still using the old system, but the situation still happened the next day, so I thought it might be a problem with the old system. Later, I checked and found that the Empire CMS system had a default task of refreshing the homepage, so I canceled it. After that, the website homepage will be updated normally.
After solving this problem, I became interested in PHP tasks. I had learned about them before, but I had not really practiced them. Perhaps considering the importance of this function to website system development, I decided to study it now and write a set of Mature code for future application.
The first is to search for relevant information online. By reading this information, I know that PHP tasks can be roughly divided into three categories.
Server scheduled tasks
The WIN server can directly use scheduled tasks to execute related applications, and the Linux server can be implemented by adding scheduled tasks in /etc/crontab.
Automatic task execution through infinite loop
Create a program file page, and use ignore_user_abort to set it to be executable after closing the browser page. Then, after running the program page, the task plan you need will always be automatically executed. For ordinary PHP programmers, if they are not confident enough, this operation is not allowed, because an infinite loop can easily cause the server to crash.
Triggered scheduled tasks
Among the several programs I know, such as Discuz, Empire CMS, etc., they all use this method to complete the system's scheduled tasks. Compared with the previous two methods, this is the safest and simplest.
There are so many choices, so when designing a program, you should choose the most appropriate way to program based on actual needs. Let’s give an introduction using examples.
The e-magazine of AiLiterature.com was produced shortly after the website was launched. This is an e-magazine delivered through e-mail. Sending the latest issue of the e-magazine to the mailboxes of subscribers is a huge task, simply batching pages. Sending obviously doesn't work. The main reason is that there is currently no own mail server, such as the mailboxes provided by News, Tencent and 163. Although they support SMTP to send emails, they do not allow large batches of emails to be sent. The conditions are limited, so it can only be solved through programs. This question.
First of all, I chose a triggered scheduled task. In order to prevent the program that sends emails from affecting the user's experience of visiting the website, mainly the speed of opening the website page, I chose to use Ajax to implement the trigger. Add a trigger function to the public JS file, and users can trigger the email sending task when they visit any page.
In order to limit the frequency of sending emails and prevent them from being banned by the mail server due to excessively frequent sending of emails, time limits are also set in the program. RAND is used to randomly generate a waiting time to solve the problem that sending emails is not a fixed interval. In fact, the interval in the trigger mode is The time is inherently uncertain, but when the website has a large number of visits, such problems may still occur, so it is safer to add settings in this regard.
Despite the above measures, in actual applications it is still found that the mail server will be prohibited from sending emails, but it is not permanently prohibited. The way to solve this problem is to create an array to save the SMTP parameters of different mail services. When one mail server cannot send mail normally, it will automatically jump to another mail server.
In the list of collected email addresses, there will be some invalid addresses. During the process of sending emails, if the sending fails once, switch to another email server and send again. If neither succeeds, set this email address to If it is invalid, no more emails will be sent to this address in the future, thereby improving the efficiency of the email address list and saving time for sending emails next time.
Some friends say that I am too stingy and share too little code in this type of blog. In fact, when we search for information on the Internet, we will find that the codes shared by many websites are not necessarily practical, and sometimes we are delayed because of referring to these codes. A lot of time, so when summarizing, I often summarize the methods to solve the problem and write down the key technical parameters so that I can have reference information when encountering similar problems in the future. If you have any questions in this regard, you can contact me via message or email.
The personal space of the author Happy Boy