我知道有很多关于使用 CRON 运行 php 文件的帖子。但是,在共享主机的世界中,以及易于为用户设置的世界中,我不想搞砸这一点。
我在网上找到了另一个与套接字有关的解决方案。只是想让每个人都接受这个,并告诉我这是一个好主意还是坏主意。听起来效果很好。
想法?
//Open socket connection to cron.php
$socketcon = fsockopen($_SERVER['HTTP_HOST'],80,$errorno,$errorstr,10);
if($socketcon) {
$socketdata = "GET /cron.php HTTP 1.1\r\nHost: ".$_SERVER['HTTP_HOST']."\r\nConnection: Close\r\n\r\n";
fwrite($socketcon,$socketdata);
//Normally you would get all the data back with fgets and wait until $socketcon reaches feof.
//In this case, we just do this:
fclose($socketcon);
} else {
//something went wrong. Put your error handler here.
}
cron.php:
//This script does all the work.
sleep(200);
//To prove that this works we will create an empty file here, after the sleep is done.
//Make sure that the webserver can write in the directory you're testing this file in.
$handle = fopen('test.txt','w');
fclose($handle);
从博客文章中找到脚本:http: //syn.ac/tech/13/creating-php-cronjobs-without-cron-and-php-cli/